436 lines
12 KiB
Go
436 lines
12 KiB
Go
package controllers
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"server/models"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
"github.com/beego/beego/v2/client/orm"
|
|
)
|
|
|
|
const (
|
|
articleStatusPublished int8 = 2
|
|
)
|
|
|
|
const (
|
|
navTypeExternal int8 = 3
|
|
)
|
|
|
|
type IndexWebsiteController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
func (c *IndexWebsiteController) ok(data interface{}) {
|
|
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": data}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func (c *IndexWebsiteController) fail(httpStatus, bizCode int, msg string) {
|
|
c.Ctx.Output.SetStatus(httpStatus)
|
|
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func publishedArticleCond() *orm.Condition {
|
|
return orm.NewCondition().
|
|
And("delete_time__isnull", true).
|
|
And("status", articleStatusPublished)
|
|
}
|
|
|
|
func articleListItem(row models.PlatformWebsiteArticle, cate models.PlatformWebsiteCategory, tags []string) map[string]interface{} {
|
|
return map[string]interface{}{
|
|
"id": row.ID,
|
|
"title": row.Title,
|
|
"summary": row.Desc,
|
|
"image": row.Image,
|
|
"author": row.Author,
|
|
"views": row.Views,
|
|
"top": row.Top,
|
|
"recommend": row.Recommend,
|
|
"date": row.PublishTime,
|
|
"category_id": row.CateID,
|
|
"category_name": cate.Name,
|
|
"category_desc": cate.Desc,
|
|
"category_image": cate.Image,
|
|
"tags": tags,
|
|
}
|
|
}
|
|
|
|
type navNode struct {
|
|
ID uint64 `json:"id"`
|
|
Pid uint64 `json:"pid"`
|
|
Title string `json:"title"`
|
|
Type int8 `json:"type"`
|
|
Path string `json:"path"`
|
|
Icon string `json:"icon"`
|
|
External bool `json:"external"`
|
|
Children []*navNode `json:"children"`
|
|
}
|
|
|
|
func (c *IndexWebsiteController) Nav() {
|
|
var rows []models.PlatformWebsiteNav
|
|
if _, err := models.Orm.QueryTable(new(models.PlatformWebsiteNav)).
|
|
Filter("delete_time__isnull", true).
|
|
Filter("status", 1).
|
|
Filter("is_visible", 1).
|
|
OrderBy("sort", "id").
|
|
All(&rows, "ID", "Pid", "Title", "Type", "Path", "Icon"); err != nil {
|
|
c.fail(500, 500, "获取导航菜单失败: "+err.Error())
|
|
return
|
|
}
|
|
|
|
nodes := make(map[uint64]*navNode, len(rows))
|
|
order := make([]uint64, 0, len(rows))
|
|
for _, r := range rows {
|
|
nodes[r.ID] = &navNode{
|
|
ID: r.ID,
|
|
Pid: r.Pid,
|
|
Title: r.Title,
|
|
Type: r.Type,
|
|
Path: r.Path,
|
|
Icon: r.Icon,
|
|
External: r.Type == navTypeExternal || strings.HasPrefix(r.Path, "http://") || strings.HasPrefix(r.Path, "https://"),
|
|
Children: []*navNode{},
|
|
}
|
|
order = append(order, r.ID)
|
|
}
|
|
|
|
roots := make([]*navNode, 0, len(rows))
|
|
for _, id := range order {
|
|
node := nodes[id]
|
|
if node.Pid == 0 {
|
|
roots = append(roots, node)
|
|
continue
|
|
}
|
|
parent, ok := nodes[node.Pid]
|
|
if !ok {
|
|
continue
|
|
}
|
|
parent.Children = append(parent.Children, node)
|
|
}
|
|
|
|
c.ok(map[string]interface{}{"list": roots})
|
|
}
|
|
|
|
func (c *IndexWebsiteController) News() {
|
|
page, _ := c.GetInt("page", 1)
|
|
pageSize, _ := c.GetInt("pageSize", 6)
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if pageSize < 1 {
|
|
pageSize = 6
|
|
}
|
|
if pageSize > 50 {
|
|
pageSize = 50
|
|
}
|
|
|
|
cond := publishedArticleCond()
|
|
if cateStr := strings.TrimSpace(c.GetString("category")); cateStr != "" {
|
|
if cid, err := strconv.ParseUint(cateStr, 10, 64); err == nil && cid > 0 {
|
|
ids := append([]uint64{cid}, models.PlatformWebsiteCategoryDescendantMap()[cid]...)
|
|
cond = cond.And("cate_id__in", ids)
|
|
}
|
|
}
|
|
if kw := strings.TrimSpace(c.GetString("keyword")); kw != "" {
|
|
cond = cond.AndCond(orm.NewCondition().
|
|
Or("title__icontains", kw).
|
|
Or("desc__icontains", kw))
|
|
}
|
|
|
|
qs := models.Orm.QueryTable(new(models.PlatformWebsiteArticle)).SetCond(cond)
|
|
total, err := qs.Count()
|
|
if err != nil {
|
|
c.fail(500, 500, "获取新闻列表失败: "+err.Error())
|
|
return
|
|
}
|
|
|
|
var rows []models.PlatformWebsiteArticle
|
|
if _, err = qs.OrderBy("-top", "-recommend", "-publish_time", "-id").
|
|
Limit(pageSize, (page-1)*pageSize).
|
|
All(&rows, "ID", "CateID", "Title", "Author", "Desc", "Image",
|
|
"Status", "Top", "Recommend", "Views", "PublishTime"); err != nil {
|
|
c.fail(500, 500, "获取新闻列表失败: "+err.Error())
|
|
return
|
|
}
|
|
|
|
articleIDs := make([]uint64, 0, len(rows))
|
|
cateIDs := make([]uint64, 0, len(rows))
|
|
for _, r := range rows {
|
|
articleIDs = append(articleIDs, r.ID)
|
|
if r.CateID > 0 {
|
|
cateIDs = append(cateIDs, r.CateID)
|
|
}
|
|
}
|
|
cateMap := models.PlatformWebsiteCategoryMap(cateIDs)
|
|
tagMap := models.PlatformWebsiteArticleTagNameMap(articleIDs)
|
|
|
|
list := make([]map[string]interface{}, 0, len(rows))
|
|
for _, r := range rows {
|
|
tags := tagMap[r.ID]
|
|
if tags == nil {
|
|
tags = []string{}
|
|
}
|
|
list = append(list, articleListItem(r, cateMap[r.CateID], tags))
|
|
}
|
|
|
|
totalPages := int((total + int64(pageSize) - 1) / int64(pageSize))
|
|
if totalPages < 1 {
|
|
totalPages = 1
|
|
}
|
|
|
|
c.ok(map[string]interface{}{
|
|
"list": list,
|
|
"total": total,
|
|
"page": page,
|
|
"pageSize": pageSize,
|
|
"totalPages": totalPages,
|
|
})
|
|
}
|
|
|
|
func (c *IndexWebsiteController) NewsCategories() {
|
|
var cates []models.PlatformWebsiteCategory
|
|
if _, err := models.Orm.QueryTable(new(models.PlatformWebsiteCategory)).
|
|
Filter("delete_time__isnull", true).
|
|
OrderBy("sort", "id").
|
|
All(&cates, "ID", "Cid", "Name", "Desc", "Image", "Sort"); err != nil {
|
|
c.fail(500, 500, "获取分类失败: "+err.Error())
|
|
return
|
|
}
|
|
|
|
descMap := models.PlatformWebsiteCategoryDescendantMap()
|
|
|
|
list := make([]map[string]interface{}, 0, len(cates))
|
|
for _, item := range cates {
|
|
ids := append([]uint64{item.ID}, descMap[item.ID]...)
|
|
cnt, _ := models.Orm.QueryTable(new(models.PlatformWebsiteArticle)).
|
|
Filter("delete_time__isnull", true).
|
|
Filter("status", articleStatusPublished).
|
|
Filter("cate_id__in", ids).
|
|
Count()
|
|
list = append(list, map[string]interface{}{
|
|
"id": item.ID,
|
|
"cid": item.Cid,
|
|
"name": item.Name,
|
|
"desc": item.Desc,
|
|
"image": item.Image,
|
|
"count": cnt,
|
|
})
|
|
}
|
|
|
|
total, _ := models.Orm.QueryTable(new(models.PlatformWebsiteArticle)).
|
|
SetCond(publishedArticleCond()).
|
|
Count()
|
|
|
|
c.ok(map[string]interface{}{"list": list, "total": total})
|
|
}
|
|
|
|
func (c *IndexWebsiteController) NewsDetail() {
|
|
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
c.fail(400, 400, "无效的内容ID")
|
|
return
|
|
}
|
|
|
|
var row models.PlatformWebsiteArticle
|
|
if err = models.Orm.QueryTable(new(models.PlatformWebsiteArticle)).
|
|
SetCond(publishedArticleCond()).
|
|
Filter("id", id).
|
|
One(&row); err != nil {
|
|
c.fail(404, 404, "内容不存在或已下线")
|
|
return
|
|
}
|
|
|
|
cate := models.PlatformWebsiteCategoryMap([]uint64{row.CateID})[row.CateID]
|
|
tags := models.PlatformWebsiteArticleTagNameMap([]uint64{row.ID})[row.ID]
|
|
if tags == nil {
|
|
tags = []string{}
|
|
}
|
|
|
|
c.ok(map[string]interface{}{
|
|
"id": row.ID,
|
|
"title": row.Title,
|
|
"summary": row.Desc,
|
|
"image": row.Image,
|
|
"content": row.Content,
|
|
"author": row.Author,
|
|
"views": row.Views,
|
|
"date": row.PublishTime,
|
|
"category_id": row.CateID,
|
|
"category_name": cate.Name,
|
|
"category_desc": cate.Desc,
|
|
"category_image": cate.Image,
|
|
"tags": tags,
|
|
})
|
|
}
|
|
|
|
func (c *IndexWebsiteController) NewsRelated() {
|
|
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
c.fail(400, 400, "无效的内容ID")
|
|
return
|
|
}
|
|
limit, _ := c.GetInt("limit", 3)
|
|
if limit < 1 || limit > 12 {
|
|
limit = 3
|
|
}
|
|
|
|
var current models.PlatformWebsiteArticle
|
|
if err = models.Orm.QueryTable(new(models.PlatformWebsiteArticle)).
|
|
SetCond(publishedArticleCond()).
|
|
Filter("id", id).
|
|
One(¤t); err != nil {
|
|
c.ok(map[string]interface{}{"list": []interface{}{}})
|
|
return
|
|
}
|
|
|
|
pick := make([]models.PlatformWebsiteArticle, 0, limit)
|
|
seen := map[uint64]bool{id: true}
|
|
|
|
if current.CateID > 0 {
|
|
var same []models.PlatformWebsiteArticle
|
|
_, _ = models.Orm.QueryTable(new(models.PlatformWebsiteArticle)).
|
|
SetCond(publishedArticleCond()).
|
|
Filter("cate_id", current.CateID).
|
|
Exclude("id", id).
|
|
OrderBy("-publish_time", "-id").
|
|
Limit(limit).
|
|
All(&same, "ID", "CateID", "Title", "Author", "Desc", "Image",
|
|
"Top", "Recommend", "Views", "PublishTime")
|
|
for _, r := range same {
|
|
if len(pick) >= limit {
|
|
break
|
|
}
|
|
pick = append(pick, r)
|
|
seen[r.ID] = true
|
|
}
|
|
}
|
|
|
|
if len(pick) < limit {
|
|
ids := make([]uint64, 0, len(seen))
|
|
for k := range seen {
|
|
ids = append(ids, k)
|
|
}
|
|
var others []models.PlatformWebsiteArticle
|
|
_, _ = models.Orm.QueryTable(new(models.PlatformWebsiteArticle)).
|
|
SetCond(publishedArticleCond()).
|
|
Exclude("id__in", ids).
|
|
OrderBy("-publish_time", "-id").
|
|
Limit(limit - len(pick)).
|
|
All(&others, "ID", "CateID", "Title", "Author", "Desc", "Image",
|
|
"Top", "Recommend", "Views", "PublishTime")
|
|
pick = append(pick, others...)
|
|
}
|
|
|
|
articleIDs := make([]uint64, 0, len(pick))
|
|
cateIDs := make([]uint64, 0, len(pick))
|
|
for _, r := range pick {
|
|
articleIDs = append(articleIDs, r.ID)
|
|
if r.CateID > 0 {
|
|
cateIDs = append(cateIDs, r.CateID)
|
|
}
|
|
}
|
|
cateMap := models.PlatformWebsiteCategoryMap(cateIDs)
|
|
tagMap := models.PlatformWebsiteArticleTagNameMap(articleIDs)
|
|
|
|
list := make([]map[string]interface{}, 0, len(pick))
|
|
for _, r := range pick {
|
|
tags := tagMap[r.ID]
|
|
if tags == nil {
|
|
tags = []string{}
|
|
}
|
|
list = append(list, articleListItem(r, cateMap[r.CateID], tags))
|
|
}
|
|
c.ok(map[string]interface{}{"list": list})
|
|
}
|
|
|
|
func (c *IndexWebsiteController) NewsView() {
|
|
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
c.fail(400, 400, "无效的内容ID")
|
|
return
|
|
}
|
|
if _, err = models.Orm.Raw(
|
|
"UPDATE yz_platform_website_article SET views = views + 1 WHERE id = ? AND status = ? AND delete_time IS NULL",
|
|
id, articleStatusPublished,
|
|
).Exec(); err != nil {
|
|
c.fail(500, 500, "更新浏览量失败")
|
|
return
|
|
}
|
|
c.ok(nil)
|
|
}
|
|
|
|
func noticeListItem(r models.PlatformWebsiteNotice) map[string]interface{} {
|
|
return map[string]interface{}{
|
|
"id": r.ID,
|
|
"title": r.Title,
|
|
"summary": r.Summary,
|
|
"type": r.Type,
|
|
"link_url": r.LinkURL,
|
|
"is_popup": r.IsPopup,
|
|
"top": r.Top,
|
|
"date": r.PublishTime,
|
|
}
|
|
}
|
|
|
|
func (c *IndexWebsiteController) Notices() {
|
|
limit, _ := c.GetInt("limit", 6)
|
|
if limit < 1 {
|
|
limit = 6
|
|
}
|
|
if limit > 50 {
|
|
limit = 50
|
|
}
|
|
|
|
cond := noticeVisibleCond()
|
|
if pop := strings.TrimSpace(c.GetString("popup")); pop != "" && pop != "0" {
|
|
cond = cond.And("is_popup", 1)
|
|
}
|
|
|
|
var rows []models.PlatformWebsiteNotice
|
|
if _, err := models.Orm.QueryTable(new(models.PlatformWebsiteNotice)).
|
|
SetCond(cond).
|
|
OrderBy("-top", "sort", "-publish_time", "-id").
|
|
Limit(limit).
|
|
All(&rows, "ID", "Title", "Summary", "Type", "LinkURL", "IsPopup", "Top",
|
|
"Sort", "PublishTime", "CreateTime"); err != nil {
|
|
c.fail(500, 500, "获取公告失败: "+err.Error())
|
|
return
|
|
}
|
|
|
|
list := make([]map[string]interface{}, 0, len(rows))
|
|
for _, r := range rows {
|
|
list = append(list, noticeListItem(r))
|
|
}
|
|
c.ok(map[string]interface{}{"list": list, "total": len(list)})
|
|
}
|
|
|
|
func (c *IndexWebsiteController) NoticeDetail() {
|
|
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
c.fail(400, 400, "无效的公告ID")
|
|
return
|
|
}
|
|
|
|
var row models.PlatformWebsiteNotice
|
|
if err = models.Orm.QueryTable(new(models.PlatformWebsiteNotice)).
|
|
SetCond(noticeVisibleCond()).
|
|
Filter("id", id).
|
|
One(&row); err != nil {
|
|
c.fail(404, 404, "公告不存在或已下线")
|
|
return
|
|
}
|
|
|
|
_, _ = models.Orm.Raw(
|
|
"UPDATE yz_platform_website_notice SET views = views + 1 WHERE id = ?",
|
|
id,
|
|
).Exec()
|
|
|
|
item := noticeListItem(row)
|
|
item["content"] = row.Content
|
|
item["views"] = row.Views + 1
|
|
c.ok(item)
|
|
}
|