2017-07-07 16:20:55 +08:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2018-03-13 14:14:56 +08:00
|
|
|
"math"
|
|
|
|
|
2017-07-07 16:20:55 +08:00
|
|
|
"github.com/astaxie/beego"
|
2018-01-26 17:17:38 +08:00
|
|
|
"github.com/astaxie/beego/orm"
|
2017-07-07 16:20:55 +08:00
|
|
|
"github.com/lifei6671/mindoc/conf"
|
2018-01-26 17:17:38 +08:00
|
|
|
"github.com/lifei6671/mindoc/models"
|
2018-01-26 18:07:55 +08:00
|
|
|
"github.com/lifei6671/mindoc/utils/pagination"
|
2017-07-07 16:20:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type LabelController struct {
|
|
|
|
BaseController
|
|
|
|
}
|
|
|
|
|
2017-07-10 14:54:59 +08:00
|
|
|
func (c *LabelController) Prepare() {
|
|
|
|
c.BaseController.Prepare()
|
2017-07-07 16:20:55 +08:00
|
|
|
|
|
|
|
//如果没有开启你们访问则跳转到登录
|
|
|
|
if !c.EnableAnonymous && c.Member == nil {
|
2018-03-13 19:20:50 +08:00
|
|
|
c.Redirect(conf.URLFor("AccountController.Login"), 302)
|
2017-07-07 16:20:55 +08:00
|
|
|
return
|
|
|
|
}
|
2017-07-10 14:54:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//查看包含标签的文档列表.
|
|
|
|
func (c *LabelController) Index() {
|
|
|
|
c.Prepare()
|
|
|
|
c.TplName = "label/index.tpl"
|
2017-07-07 16:20:55 +08:00
|
|
|
|
|
|
|
labelName := c.Ctx.Input.Param(":key")
|
2018-01-26 17:17:38 +08:00
|
|
|
pageIndex, _ := c.GetInt("page", 1)
|
2017-07-07 16:20:55 +08:00
|
|
|
if labelName == "" {
|
|
|
|
c.Abort("404")
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
_, err := models.NewLabel().FindFirst("label_name", labelName)
|
2017-07-07 16:20:55 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
c.Abort("404")
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
2017-07-07 16:20:55 +08:00
|
|
|
beego.Error(err)
|
|
|
|
c.Abort("500")
|
|
|
|
}
|
|
|
|
}
|
2018-08-07 17:19:56 +08:00
|
|
|
memberId := 0
|
2017-07-07 16:20:55 +08:00
|
|
|
if c.Member != nil {
|
2018-08-07 17:19:56 +08:00
|
|
|
memberId = c.Member.MemberId
|
2017-07-07 16:20:55 +08:00
|
|
|
}
|
2018-08-07 17:19:56 +08:00
|
|
|
searchResult, totalCount, err := models.NewBook().FindForLabelToPager(labelName, pageIndex, conf.PageSize, memberId)
|
2017-07-07 16:20:55 +08:00
|
|
|
|
2018-08-07 17:19:56 +08:00
|
|
|
if err != nil && err != orm.ErrNoRows {
|
|
|
|
beego.Error("查询标签时出错 ->", err)
|
|
|
|
c.ShowErrorPage(500, "查询文档列表时出错")
|
2017-07-07 16:20:55 +08:00
|
|
|
}
|
|
|
|
if totalCount > 0 {
|
2018-08-07 17:19:56 +08:00
|
|
|
pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.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-07-07 16:20:55 +08:00
|
|
|
c.Data["PageHtml"] = ""
|
|
|
|
}
|
2018-02-28 15:47:00 +08:00
|
|
|
c.Data["Lists"] = searchResult
|
2017-07-07 16:20:55 +08:00
|
|
|
|
|
|
|
c.Data["LabelName"] = labelName
|
|
|
|
}
|
2017-07-10 14:54:59 +08:00
|
|
|
|
|
|
|
func (c *LabelController) List() {
|
|
|
|
c.Prepare()
|
|
|
|
c.TplName = "label/list.tpl"
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
pageIndex, _ := c.GetInt("page", 1)
|
2017-07-10 14:54:59 +08:00
|
|
|
pageSize := 200
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
labels, totalCount, err := models.NewLabel().FindToPager(pageIndex, pageSize)
|
2017-07-10 14:54:59 +08:00
|
|
|
|
2018-08-07 17:19:56 +08:00
|
|
|
if err != nil && err != orm.ErrNoRows {
|
|
|
|
c.ShowErrorPage(500, err.Error())
|
2017-07-10 14:54:59 +08:00
|
|
|
}
|
|
|
|
if totalCount > 0 {
|
2018-03-13 14:14:56 +08:00
|
|
|
pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.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-07-10 14:54:59 +08:00
|
|
|
c.Data["PageHtml"] = ""
|
|
|
|
}
|
|
|
|
c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(pageSize)))
|
|
|
|
|
|
|
|
c.Data["Labels"] = labels
|
|
|
|
}
|