From 9c9f3227deb1775886f9031cdcdff192ec862d25 Mon Sep 17 00:00:00 2001 From: martin Date: Sun, 7 Jun 2026 17:19:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(doc):=E4=BF=AE=E5=A4=8D=E5=B8=AE=E5=8A=A9?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=98=BE=E7=A4=BA=E9=94=99=E8=AF=AF=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/logic/admin/help_center.go | 7 + internal/logic/common/help_center.go | 38 +- web/tenant/src/views/service/DocsPage.vue | 1446 ++++++++++++++++++--- 3 files changed, 1282 insertions(+), 209 deletions(-) diff --git a/internal/logic/admin/help_center.go b/internal/logic/admin/help_center.go index e9dcc14..f940437 100644 --- a/internal/logic/admin/help_center.go +++ b/internal/logic/admin/help_center.go @@ -288,6 +288,11 @@ func (s *sAdmin) UpdateHelpArticle(ctx context.Context, req *v1.HelpArticleUpdat s.refreshCategoryArticleCount(ctx, req.CategoryId) } + // 状态变更(草稿↔已发布)也需要刷新计数 + if article.Status != req.Status { + s.refreshCategoryArticleCount(ctx, req.CategoryId) + } + return &v1.HelpArticleUpdateRes{}, nil } @@ -364,6 +369,8 @@ func (s *sAdmin) refreshCategoryArticleCount(ctx context.Context, categoryId int count, err := dao.SptArticles.Ctx(ctx). Where("category_id", categoryId). Where("status", "published"). + Where("published_at IS NOT NULL"). + Where("published_at <=", gtime.Now()). Count() if err != nil { g.Log().Warningf(ctx, "refreshCategoryArticleCount count failed for category %d: %v", categoryId, err) diff --git a/internal/logic/common/help_center.go b/internal/logic/common/help_center.go index b50de41..cd38bfa 100644 --- a/internal/logic/common/help_center.go +++ b/internal/logic/common/help_center.go @@ -26,14 +26,13 @@ type HelpPublicCategoryItem struct { // ListPublicCategories 返回可见分类的树结构 func ListPublicCategories(ctx context.Context) ([]*HelpPublicCategoryItem, error) { type categoryRow struct { - Id int64 `json:"id" orm:"id"` - ParentId int64 `json:"parent_id" orm:"parent_id"` - Name string `json:"name" orm:"name"` - Slug string `json:"slug" orm:"slug"` - Description string `json:"description" orm:"description"` - Icon string `json:"icon" orm:"icon"` - IsVisible bool `json:"is_visible" orm:"is_visible"` - ArticleCount int `json:"article_count" orm:"article_count"` + Id int64 `json:"id" orm:"id"` + ParentId int64 `json:"parent_id" orm:"parent_id"` + Name string `json:"name" orm:"name"` + Slug string `json:"slug" orm:"slug"` + Description string `json:"description" orm:"description"` + Icon string `json:"icon" orm:"icon"` + IsVisible bool `json:"is_visible" orm:"is_visible"` } var rows []categoryRow @@ -45,6 +44,27 @@ func ListPublicCategories(ctx context.Context) ([]*HelpPublicCategoryItem, error return nil, err } + // 实时统计每个分类的已发布文章数(不依赖冗余字段 article_count) + type countRow struct { + CategoryId int64 `json:"category_id" orm:"category_id"` + ArticleCount int `json:"count" orm:"count"` + } + var countRows []countRow + err = dao.SptArticles.Ctx(ctx). + Where("status", "published"). + Where("published_at IS NOT NULL"). + Where("published_at <=", gtime.Now()). + Fields("category_id, COUNT(*) as count"). + Group("category_id"). + Scan(&countRows) + if err != nil { + return nil, err + } + countMap := make(map[int64]int, len(countRows)) + for _, cr := range countRows { + countMap[cr.CategoryId] = cr.ArticleCount + } + // 构建所有节点 nodeMap := make(map[int64]*HelpPublicCategoryItem) for _, r := range rows { @@ -55,7 +75,7 @@ func ListPublicCategories(ctx context.Context) ([]*HelpPublicCategoryItem, error Slug: r.Slug, Description: r.Description, Icon: r.Icon, - ArticleCount: r.ArticleCount, + ArticleCount: countMap[r.Id], Children: make([]*HelpPublicCategoryItem, 0), } } diff --git a/web/tenant/src/views/service/DocsPage.vue b/web/tenant/src/views/service/DocsPage.vue index 0dd4361..62a19cb 100644 --- a/web/tenant/src/views/service/DocsPage.vue +++ b/web/tenant/src/views/service/DocsPage.vue @@ -1,17 +1,22 @@