更新website
This commit is contained in:
@@ -155,7 +155,9 @@ func (c *IndexWebsiteController) News() {
|
||||
cond := publishedArticleCond()
|
||||
if cateStr := strings.TrimSpace(c.GetString("category")); cateStr != "" {
|
||||
if cid, err := strconv.ParseUint(cateStr, 10, 64); err == nil && cid > 0 {
|
||||
cond = cond.And("cate_id", cid)
|
||||
// 选中父分类时把它整棵子树的文章一起返回,否则挂在子分类上的内容会查不到
|
||||
ids := append([]uint64{cid}, models.PlatformWebsiteCategoryDescendantMap()[cid]...)
|
||||
cond = cond.And("cate_id__in", ids)
|
||||
}
|
||||
}
|
||||
if kw := strings.TrimSpace(c.GetString("keyword")); kw != "" {
|
||||
@@ -221,20 +223,25 @@ func (c *IndexWebsiteController) NewsCategories() {
|
||||
if _, err := models.Orm.QueryTable(new(models.PlatformWebsiteCategory)).
|
||||
Filter("delete_time__isnull", true).
|
||||
OrderBy("sort", "id").
|
||||
All(&cates, "ID", "Name", "Desc", "Image", "Sort"); err != nil {
|
||||
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", item.ID).
|
||||
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,
|
||||
@@ -390,3 +397,89 @@ func (c *IndexWebsiteController) NewsView() {
|
||||
}
|
||||
c.ok(nil)
|
||||
}
|
||||
|
||||
// =============================================================
|
||||
// 站点公告 yz_platform_website_notice
|
||||
//
|
||||
// GET /site/notices 公告列表(?limit=6&popup=1 只取弹窗公告)
|
||||
// GET /site/notices/:id 公告详情(正文为 HTML,浏览量 +1)
|
||||
//
|
||||
// 仅返回「已发布」且当前时间处于有效期内的公告,条件见 noticeVisibleCond。
|
||||
// =============================================================
|
||||
|
||||
// noticeListItem 公告列表项(不含正文,减小响应体)
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
// Notices GET /site/notices?limit=&popup=
|
||||
func (c *IndexWebsiteController) Notices() {
|
||||
limit, _ := c.GetInt("limit", 6)
|
||||
if limit < 1 {
|
||||
limit = 6
|
||||
}
|
||||
if limit > 50 {
|
||||
limit = 50
|
||||
}
|
||||
|
||||
cond := noticeVisibleCond()
|
||||
// popup 传 1 / true 时只取弹窗公告(首页弹窗、侧栏公告用)
|
||||
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)})
|
||||
}
|
||||
|
||||
// NoticeDetail GET /site/notices/:id
|
||||
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
|
||||
}
|
||||
|
||||
// 浏览量 +1,失败不影响正文展示
|
||||
_, _ = 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user