diff --git a/.mimocode/.cron-lock b/.mimocode/.cron-lock index bf7d9c6..b73c72d 100644 --- a/.mimocode/.cron-lock +++ b/.mimocode/.cron-lock @@ -1 +1 @@ -{"pid":4264,"startedAt":1784510683025} \ No newline at end of file +{"pid":26648,"startedAt":1787847660038} \ No newline at end of file diff --git a/go/controllers/index_portal.go b/go/controllers/index_portal.go index 1fe42a9..c23ad1b 100644 --- a/go/controllers/index_portal.go +++ b/go/controllers/index_portal.go @@ -3,6 +3,9 @@ package controllers import ( "fmt" "net/url" + "os" + "path/filepath" + "regexp" "strconv" "strings" @@ -17,6 +20,31 @@ type IndexPortalController struct { beego.Controller } +// reNewsRow 从模板标签 {yz:newscenter row="N" ...} 中提取 row 值 +var reNewsRow = regexp.MustCompile(`\{yz:newscenter[^}]*\s+row\s*=\s*"(\d+)"`) + +// templateNewsRow 读取当前租户首页模板中 newscenter 标签的 row 参数, +// 作为 API 默认条数,使接口与前端模板保持一致。 +func templateNewsRow(tid uint64) int { + code := strings.TrimSpace(cmsTenantTemplateCode(tid)) + if code == "" || code == "default" { + code = "default" + } + if !cmsTemplateDirExists(code) { + code = "default" + } + data, err := os.ReadFile(filepath.Join(cmsThemesRoot(), code, "index.html")) + if err != nil { + return 10 + } + if m := reNewsRow.FindSubmatch(data); len(m) == 2 { + if n, e := strconv.Atoi(string(m[1])); e == nil && n > 0 && n <= 50 { + return n + } + } + return 10 +} + // getTidByHost 根据访问域名识别租户 ID,默认返回 1 func (c *IndexPortalController) getTidByHost() uint64 { host := c.Ctx.Request.Host @@ -267,13 +295,15 @@ func (c *IndexPortalController) queryCmsArticles(categoryName string, categoryID tid := c.getTidByHost() _ = models.EnsureCmsArticleTables() + defaultLimit := templateNewsRow(tid) + page, _ := c.GetInt("page", 1) - limit, _ := c.GetInt("limit", 10) + limit, _ := c.GetInt("limit", defaultLimit) if page < 1 { page = 1 } if limit < 1 { - limit = 10 + limit = defaultLimit } qs := models.Orm.QueryTable(new(models.CmsArticle)). diff --git a/go/controllers/tenant_site.go b/go/controllers/tenant_site.go index 692acc9..0100590 100644 --- a/go/controllers/tenant_site.go +++ b/go/controllers/tenant_site.go @@ -136,7 +136,7 @@ func (c *TenantSiteController) rewriteStorageURLs(html string) string { if err != nil || strings.TrimSpace(cfg.QiniuDomain) == "" { return html } - return rewriteLegacyQiniuURLs(html, storagePublicBaseURL(cfg.QiniuDomain)) + return rewriteLegacyQiniuURLs(html, storagePublicBaseURL(cfg.QiniuDomain), cfg.QiniuDomain) } func storagePublicBaseURL(domain string) string { @@ -147,13 +147,18 @@ func storagePublicBaseURL(domain string) string { return "https://" + base } -func rewriteLegacyQiniuURLs(html, targetBase string) string { +func rewriteLegacyQiniuURLs(html, targetBase, qiniuDomain string) string { if targetBase == "" { return html } - for _, domain := range legacyQiniuDomains { + domains := append(legacyQiniuDomains, qiniuDomain) + for _, domain := range domains { + d := strings.TrimSpace(domain) + if d == "" { + continue + } for _, scheme := range []string{"http://", "https://"} { - html = strings.ReplaceAll(html, scheme+domain+"/", targetBase+"/") + html = strings.ReplaceAll(html, scheme+d+"/", targetBase+"/") } } return html diff --git a/go/models/cms_article.go b/go/models/cms_article.go index 15eb4d5..eda7be1 100644 --- a/go/models/cms_article.go +++ b/go/models/cms_article.go @@ -126,6 +126,23 @@ func CmsCategoryNameMap(tid uint64, ids []uint64) map[uint64]string { return out } +func CmsCategoryImageMap(tid uint64, ids []uint64) map[uint64]string { + out := make(map[uint64]string) + if len(ids) == 0 { + return out + } + var rows []CmsArticleCategory + qs := Orm.QueryTable(new(CmsArticleCategory)). + Filter("id__in", ids). + Filter("delete_time__isnull", true) + cond := orm.NewCondition().Or("tid", 0).Or("tid", tid) + _, _ = qs.SetCond(cond).All(&rows, "ID", "Image") + for _, r := range rows { + out[r.ID] = r.Image + } + return out +} + func CmsFormatTime(t *time.Time) string { if t == nil { return "" diff --git a/go/pkg/tagengine/provider.go b/go/pkg/tagengine/provider.go index 6d31229..9c6f465 100644 --- a/go/pkg/tagengine/provider.go +++ b/go/pkg/tagengine/provider.go @@ -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() diff --git a/go/themes/default/index.html b/go/themes/default/index.html index d83a9ab..b551934 100644 --- a/go/themes/default/index.html +++ b/go/themes/default/index.html @@ -24,7 +24,7 @@ {yz:arclist row="4" titlelen="24"}
  • - [field:titlefull/] + [field:titlefull/]

    [field:title/]

    [field:desc/]

    [field:pubdate/] diff --git a/go/themes/theme1/about.html b/go/themes/theme1/about.html index a3cf16f..b5c1808 100644 --- a/go/themes/theme1/about.html +++ b/go/themes/theme1/about.html @@ -23,7 +23,7 @@