ItemsetsController.go 2.2 KB

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