更新网站功能

This commit is contained in:
2026-08-28 00:50:08 +08:00
parent ae6b060c25
commit 9319427f67
12 changed files with 129 additions and 52 deletions
+52 -6
View File
@@ -426,11 +426,12 @@ func arclistProvider(tid uint64, params map[string]string, ctx *RenderCtx) ([]ma
if err != nil && err != orm.ErrNoRows {
return nil, err
}
return articleRows(rows, titleLen), nil
cateImgMap := models.CmsCategoryImageMap(tid, collectCateIDs(rows))
return articleRows(rows, titleLen, cateImgMap), nil
}
// articleRows 文章行转标签字段
func articleRows(rows []models.CmsArticle, titleLen int) []map[string]string {
func articleRows(rows []models.CmsArticle, titleLen int, cateImgMap map[uint64]string) []map[string]string {
out := make([]map[string]string, 0, len(rows))
for _, a := range rows {
pub := ""
@@ -443,12 +444,16 @@ func articleRows(rows []models.CmsArticle, titleLen int) []map[string]string {
if titleLen > 0 {
title = truncateTitle(title, titleLen)
}
img := a.Image
if img == "" {
img = cateImgMap[a.CateID]
}
out = append(out, map[string]string{
"id": strconv.FormatUint(a.ID, 10),
"title": title,
"titlefull": a.Title,
"desc": a.Desc,
"image": a.Image,
"image": img,
"arcurl": "/news/" + strconv.FormatUint(a.ID, 10),
"pubdate": pub,
"views": strconv.Itoa(a.Views),
@@ -458,6 +463,19 @@ func articleRows(rows []models.CmsArticle, titleLen int) []map[string]string {
return out
}
// collectCateIDs 从文章列表中收集不重复的分类 ID
func collectCateIDs(rows []models.CmsArticle) []uint64 {
seen := make(map[uint64]bool)
var ids []uint64
for _, a := range rows {
if a.CateID > 0 && !seen[a.CateID] {
seen[a.CateID] = true
ids = append(ids, a.CateID)
}
}
return ids
}
// arcviewProvider 文章详情(详情页模板内),取当前路由文章
func arcviewProvider(tid uint64, params map[string]string, ctx *RenderCtx) ([]map[string]string, error) {
if ctx.ArticleID == 0 {
@@ -478,12 +496,17 @@ func arcviewProvider(tid uint64, params map[string]string, ctx *RenderCtx) ([]ma
} else {
pub = a.CreateTime.Format(datetimeLayout)
}
img := a.Image
if img == "" && a.CateID > 0 {
cateImgMap := models.CmsCategoryImageMap(tid, []uint64{a.CateID})
img = cateImgMap[a.CateID]
}
return []map[string]string{{
"id": strconv.FormatUint(a.ID, 10),
"title": a.Title,
"content": a.Content,
"desc": a.Desc,
"image": a.Image,
"image": img,
"author": a.Author,
"pubdate": pub,
"views": strconv.Itoa(a.Views),
@@ -547,12 +570,34 @@ func newscenterHandler(tid uint64, params map[string]string, ctx *RenderCtx, bod
All(&children)
}
// collectDescendantIDs 递归收集分类及其所有子孙分类 ID
collectDescendantIDs := func(rootID uint64) []uint64 {
ids := []uint64{rootID}
queue := []uint64{rootID}
for len(queue) > 0 {
pid := queue[0]
queue = queue[1:]
var subs []models.CmsArticleCategory
_, _ = models.Orm.QueryTable(new(models.CmsArticleCategory)).
Filter("cid", pid).
Filter("tid__in", []uint64{0, tid}).
Filter("status", 1).
Filter("delete_time__isnull", true).
All(&subs)
for _, s := range subs {
ids = append(ids, s.ID)
queue = append(queue, s.ID)
}
}
return ids
}
renderCards := func(cateID uint64) string {
qs := publishedCond(models.Orm.QueryTable(new(models.CmsArticle)).
Filter("tid", tid).
Filter("delete_time__isnull", true))
if cateID > 0 {
qs = qs.Filter("cate_id", cateID)
qs = qs.Filter("cate_id__in", collectDescendantIDs(cateID))
} else if parent != nil {
ids := []uint64{parent.ID}
for _, c := range children {
@@ -562,8 +607,9 @@ func newscenterHandler(tid uint64, params map[string]string, ctx *RenderCtx, bod
}
var arts []models.CmsArticle
_, _ = qs.OrderBy("-top", "-id").Limit(row).All(&arts)
cateImgMap := models.CmsCategoryImageMap(tid, collectCateIDs(arts))
var sb strings.Builder
for _, a := range articleRows(arts, titleLen) {
for _, a := range articleRows(arts, titleLen, cateImgMap) {
sb.WriteString(applyFields(body, a))
}
return sb.String()