ItemsetsController.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package controllers
  2. import (
  3. "github.com/lifei6671/mindoc/conf"
  4. "github.com/lifei6671/mindoc/models"
  5. "github.com/astaxie/beego/orm"
  6. "github.com/lifei6671/mindoc/utils/pagination"
  7. "github.com/astaxie/beego"
  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. pageIndex, _ := c.GetInt("page", 0)
  24. items, totalCount, err := models.NewItemsets().FindToPager(pageIndex, conf.PageSize)
  25. if err != nil && err != orm.ErrNoRows {
  26. c.ShowErrorPage(500, err.Error())
  27. }
  28. c.Data["TotalPages"] = pageIndex
  29. if err == orm.ErrNoRows || len(items) <= 0 {
  30. c.Data["Lists"] = items
  31. c.Data["PageHtml"] = ""
  32. return
  33. }
  34. if totalCount > 0 {
  35. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  36. c.Data["PageHtml"] = pager.HtmlPages()
  37. } else {
  38. c.Data["PageHtml"] = ""
  39. }
  40. c.Data["Lists"] = items
  41. }
  42. func (c *ItemsetsController) List() {
  43. c.Prepare()
  44. c.TplName = "items/list.tpl"
  45. itemKey := c.Ctx.Input.Param(":key")
  46. pageIndex, _ := c.GetInt("page", 1)
  47. if itemKey == "" {
  48. c.Abort("404")
  49. }
  50. item, err := models.NewItemsets().FindFirst(itemKey)
  51. if err != nil {
  52. if err == orm.ErrNoRows {
  53. c.Abort("404")
  54. } else {
  55. beego.Error(err)
  56. c.Abort("500")
  57. }
  58. }
  59. memberId := 0
  60. if c.Member != nil {
  61. memberId = c.Member.MemberId
  62. }
  63. searchResult, totalCount, err := models.NewItemsets().FindItemsetsByItemKey(itemKey, pageIndex, conf.PageSize, memberId)
  64. if err != nil && err != orm.ErrNoRows {
  65. c.ShowErrorPage(500, "查询文档列表时出错")
  66. }
  67. if totalCount > 0 {
  68. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  69. c.Data["PageHtml"] = pager.HtmlPages()
  70. } else {
  71. c.Data["PageHtml"] = ""
  72. }
  73. c.Data["Lists"] = searchResult
  74. c.Data["Model"] = item
  75. }