label.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package controllers
  2. import (
  3. "math"
  4. "github.com/astaxie/beego"
  5. "github.com/astaxie/beego/orm"
  6. "github.com/lifei6671/mindoc/conf"
  7. "github.com/lifei6671/mindoc/models"
  8. "github.com/lifei6671/mindoc/utils"
  9. "github.com/lifei6671/mindoc/utils/pagination"
  10. )
  11. type LabelController struct {
  12. BaseController
  13. }
  14. func (c *LabelController) Prepare() {
  15. c.BaseController.Prepare()
  16. //如果没有开启你们访问则跳转到登录
  17. if !c.EnableAnonymous && c.Member == nil {
  18. c.Redirect(utils.URLFor("AccountController.Login"), 302)
  19. return
  20. }
  21. }
  22. //查看包含标签的文档列表.
  23. func (c *LabelController) Index() {
  24. c.Prepare()
  25. c.TplName = "label/index.tpl"
  26. labelName := c.Ctx.Input.Param(":key")
  27. pageIndex, _ := c.GetInt("page", 1)
  28. if labelName == "" {
  29. c.Abort("404")
  30. }
  31. _, err := models.NewLabel().FindFirst("label_name", labelName)
  32. if err != nil {
  33. if err == orm.ErrNoRows {
  34. c.Abort("404")
  35. } else {
  36. beego.Error(err)
  37. c.Abort("500")
  38. }
  39. }
  40. member_id := 0
  41. if c.Member != nil {
  42. member_id = c.Member.MemberId
  43. }
  44. searchResult, totalCount, err := models.NewBook().FindForLabelToPager(labelName, pageIndex, conf.PageSize, member_id)
  45. if err != nil {
  46. beego.Error(err)
  47. return
  48. }
  49. if totalCount > 0 {
  50. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize,c.BaseUrl())
  51. c.Data["PageHtml"] = pager.HtmlPages()
  52. } else {
  53. c.Data["PageHtml"] = ""
  54. }
  55. c.Data["Lists"] = searchResult
  56. c.Data["LabelName"] = labelName
  57. }
  58. func (c *LabelController) List() {
  59. c.Prepare()
  60. c.TplName = "label/list.tpl"
  61. pageIndex, _ := c.GetInt("page", 1)
  62. pageSize := 200
  63. labels, totalCount, err := models.NewLabel().FindToPager(pageIndex, pageSize)
  64. if err != nil {
  65. c.ShowErrorPage(50001, err.Error())
  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["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(pageSize)))
  74. c.Data["Labels"] = labels
  75. }