1
0

label.go 1.9 KB

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