ItemsetsController.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package controllers
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "github.com/beego/beego/v2/core/logs"
  5. "github.com/mindoc-org/mindoc/conf"
  6. "github.com/mindoc-org/mindoc/models"
  7. "github.com/mindoc-org/mindoc/utils/pagination"
  8. )
  9. type ItemsetsController struct {
  10. BaseController
  11. }
  12. func (c *ItemsetsController) Prepare() {
  13. c.BaseController.Prepare()
  14. //如果没有开启你们访问则跳转到登录
  15. if !c.EnableAnonymous && c.Member == nil {
  16. c.Redirect(conf.URLFor("AccountController.Login"), 302)
  17. return
  18. }
  19. }
  20. func (c *ItemsetsController) Index() {
  21. c.Prepare()
  22. c.TplName = "items/index.tpl"
  23. pageSize := 18
  24. pageIndex, _ := c.GetInt("page", 0)
  25. items, totalCount, err := models.NewItemsets().FindToPager(pageIndex, pageSize)
  26. if err != nil && err != orm.ErrNoRows {
  27. c.ShowErrorPage(500, err.Error())
  28. }
  29. c.Data["TotalPages"] = pageIndex
  30. if err == orm.ErrNoRows || len(items) <= 0 {
  31. c.Data["Lists"] = items
  32. c.Data["PageHtml"] = ""
  33. return
  34. }
  35. if totalCount > 0 {
  36. pager := pagination.NewPagination(c.Ctx.Request, totalCount, pageSize, c.BaseUrl())
  37. c.Data["PageHtml"] = pager.HtmlPages()
  38. } else {
  39. c.Data["PageHtml"] = ""
  40. }
  41. c.Data["Lists"] = items
  42. }
  43. func (c *ItemsetsController) List() {
  44. c.Prepare()
  45. c.TplName = "items/list.tpl"
  46. pageSize := 18
  47. itemKey := c.Ctx.Input.Param(":key")
  48. pageIndex, _ := c.GetInt("page", 1)
  49. if itemKey == "" {
  50. c.Abort("404")
  51. }
  52. item, err := models.NewItemsets().FindFirst(itemKey)
  53. if err != nil {
  54. if err == orm.ErrNoRows {
  55. c.Abort("404")
  56. } else {
  57. logs.Error(err)
  58. c.Abort("500")
  59. }
  60. }
  61. memberId := 0
  62. if c.Member != nil {
  63. memberId = c.Member.MemberId
  64. }
  65. searchResult, totalCount, err := models.NewItemsets().FindItemsetsByItemKey(itemKey, pageIndex, pageSize, memberId)
  66. if err != nil && err != orm.ErrNoRows {
  67. c.ShowErrorPage(500, "查询文档列表时出错")
  68. }
  69. if totalCount > 0 {
  70. pager := pagination.NewPagination(c.Ctx.Request, totalCount, pageSize, c.BaseUrl())
  71. c.Data["PageHtml"] = pager.HtmlPages()
  72. } else {
  73. c.Data["PageHtml"] = ""
  74. }
  75. c.Data["Lists"] = searchResult
  76. c.Data["Model"] = item
  77. }