2017-04-19 18:19:27 +08:00
|
|
|
package controllers
|
|
|
|
|
2017-04-30 22:13:12 +08:00
|
|
|
import (
|
2018-03-13 14:14:56 +08:00
|
|
|
"math"
|
2018-09-19 11:32:29 +08:00
|
|
|
"net/url"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
"github.com/beego/beego/v2/adapter/logs"
|
2021-03-23 15:09:17 +08:00
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
|
|
|
"github.com/mindoc-org/mindoc/models"
|
|
|
|
"github.com/mindoc-org/mindoc/utils/pagination"
|
2017-04-30 22:13:12 +08:00
|
|
|
)
|
|
|
|
|
2017-04-19 18:19:27 +08:00
|
|
|
type HomeController struct {
|
|
|
|
BaseController
|
|
|
|
}
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
func (c *HomeController) Prepare() {
|
|
|
|
c.BaseController.Prepare()
|
2017-04-30 22:13:12 +08:00
|
|
|
//如果没有开启匿名访问,则跳转到登录页面
|
|
|
|
if !c.EnableAnonymous && c.Member == nil {
|
2018-03-13 19:20:50 +08:00
|
|
|
c.Redirect(conf.URLFor("AccountController.Login")+"?url="+url.PathEscape(conf.BaseUrl+c.Ctx.Request.URL.RequestURI()), 302)
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HomeController) Index() {
|
|
|
|
c.Prepare()
|
|
|
|
c.TplName = "home/index.tpl"
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
pageIndex, _ := c.GetInt("page", 1)
|
2017-04-30 22:13:12 +08:00
|
|
|
pageSize := 18
|
|
|
|
|
2018-11-28 09:56:20 +08:00
|
|
|
memberId := 0
|
2017-04-30 22:13:12 +08:00
|
|
|
|
|
|
|
if c.Member != nil {
|
2018-11-28 09:56:20 +08:00
|
|
|
memberId = c.Member.MemberId
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
2018-11-28 09:56:20 +08:00
|
|
|
books, totalCount, err := models.NewBook().FindForHomeToPager(pageIndex, pageSize, memberId)
|
2017-04-30 22:13:12 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
logs.Error(err)
|
2017-04-30 22:13:12 +08:00
|
|
|
c.Abort("500")
|
|
|
|
}
|
|
|
|
if totalCount > 0 {
|
2018-03-13 14:14:56 +08:00
|
|
|
pager := pagination.NewPagination(c.Ctx.Request, totalCount, pageSize, c.BaseUrl())
|
2018-01-26 18:07:55 +08:00
|
|
|
c.Data["PageHtml"] = pager.HtmlPages()
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
2017-04-30 22:13:12 +08:00
|
|
|
c.Data["PageHtml"] = ""
|
|
|
|
}
|
2017-05-31 10:15:04 +08:00
|
|
|
c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(pageSize)))
|
2017-04-30 22:13:12 +08:00
|
|
|
|
|
|
|
c.Data["Lists"] = books
|
2017-04-19 18:19:27 +08:00
|
|
|
}
|