home.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package controllers
  2. import (
  3. "math"
  4. "net/url"
  5. "github.com/astaxie/beego"
  6. "github.com/lifei6671/mindoc/conf"
  7. "github.com/lifei6671/mindoc/models"
  8. "github.com/lifei6671/mindoc/utils/pagination"
  9. )
  10. type HomeController struct {
  11. BaseController
  12. }
  13. func (c *HomeController) Index() {
  14. c.Prepare()
  15. c.TplName = "home/index.tpl"
  16. //如果没有开启匿名访问,则跳转到登录页面
  17. if !c.EnableAnonymous && c.Member == nil {
  18. c.Redirect(conf.URLFor("AccountController.Login")+"?url="+url.PathEscape(conf.BaseUrl+c.Ctx.Request.URL.RequestURI()), 302)
  19. }
  20. pageIndex, _ := c.GetInt("page", 1)
  21. pageSize := 18
  22. member_id := 0
  23. if c.Member != nil {
  24. member_id = c.Member.MemberId
  25. }
  26. books, totalCount, err := models.NewBook().FindForHomeToPager(pageIndex, pageSize, member_id)
  27. if err != nil {
  28. beego.Error(err)
  29. c.Abort("500")
  30. }
  31. if totalCount > 0 {
  32. pager := pagination.NewPagination(c.Ctx.Request, totalCount, pageSize, c.BaseUrl())
  33. c.Data["PageHtml"] = pager.HtmlPages()
  34. } else {
  35. c.Data["PageHtml"] = ""
  36. }
  37. c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(pageSize)))
  38. c.Data["Lists"] = books
  39. labels, totalCount, err := models.NewLabel().FindToPager(1, 10)
  40. if err != nil {
  41. c.Data["Labels"] = make([]*models.Label, 0)
  42. } else {
  43. c.Data["Labels"] = labels
  44. }
  45. }