From 166e01c7c3e925d22b00d60f7c12b60982278044 Mon Sep 17 00:00:00 2001 From: Victor_Jay Date: Tue, 2 Jun 2026 15:21:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=9E=E7=8E=B0=E7=A9=BA=E9=97=B4=20T?= =?UTF-8?q?otalReads=20=E7=BB=9F=E8=AE=A1=EF=BC=8C=E4=BF=AE=E5=A4=8D=20sel?= =?UTF-8?q?ectMention=20=E9=97=AD=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - P0-2: posts/show.html selectMention 闭包 var→let 修复 - P0-3: GetUserStats 增加 SUM(views_count) 统计,替换硬编码 int64(0) - 更新 Fix_List.md 标注 P0-1/2/3 已修复 --- internal/controller/interfaces.go | 2 +- internal/controller/space_controller.go | 4 ++-- internal/repository/post_repo.go | 11 ++++++----- internal/service/repository.go | 2 +- internal/service/space_service.go | 4 ++-- templates/MetaLab-2026/html/posts/show.html | 4 ++-- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/internal/controller/interfaces.go b/internal/controller/interfaces.go index cfb083a..b154cc7 100644 --- a/internal/controller/interfaces.go +++ b/internal/controller/interfaces.go @@ -55,7 +55,7 @@ type spaceUseCase interface { GetSpaceUser(uid uint) (*model.User, error) GetPostsByUser(uid uint, page, pageSize int) ([]model.Post, int64, error) CountUserPosts(uid uint) (int64, error) - GetUserStats(uid uint) (likes, favorites int64, err error) + GetUserStats(uid uint) (likes, favorites, reads int64, err error) UpdateUser(user *model.User) error } diff --git a/internal/controller/space_controller.go b/internal/controller/space_controller.go index 7ec1015..a3e9ad2 100644 --- a/internal/controller/space_controller.go +++ b/internal/controller/space_controller.go @@ -81,7 +81,7 @@ func (ctrl *SpaceController) ShowSpace(c *gin.Context) { // 构建基础数据(含用户统计) postCount, _ := ctrl.spaceService.CountUserPosts(uint(uid)) - totalLikes, totalFavorites, _ := ctrl.spaceService.GetUserStats(uint(uid)) + totalLikes, totalFavorites, totalReads, _ := ctrl.spaceService.GetUserStats(uint(uid)) data := gin.H{ "Title": spaceUser.Username + " 的空间", @@ -93,7 +93,7 @@ func (ctrl *SpaceController) ShowSpace(c *gin.Context) { "PostCount": postCount, "TotalLikes": totalLikes, "TotalFavorites": totalFavorites, - "TotalReads": int64(0), // 暂无阅读量统计 + "TotalReads": totalReads, } // ---- 根据 Tab 加载对应数据 ---- diff --git a/internal/repository/post_repo.go b/internal/repository/post_repo.go index 4b1a19f..507b75b 100644 --- a/internal/repository/post_repo.go +++ b/internal/repository/post_repo.go @@ -93,18 +93,19 @@ func (r *PostRepo) CountUserPosts(userID uint) (int64, error) { return count, err } -// GetUserStats 获取用户的文章统计数据(总获赞/总收藏) -func (r *PostRepo) GetUserStats(userID uint) (likes, favorites int64, err error) { +// GetUserStats 获取用户的文章统计数据(总获赞/总收藏/总阅读) +func (r *PostRepo) GetUserStats(userID uint) (likes, favorites, reads int64, err error) { type stats struct { - TotalLikes int64 + TotalLikes int64 TotalFavorites int64 + TotalReads int64 } var s stats err = r.db.Model(&model.Post{}). - Select("COALESCE(SUM(likes_count), 0) AS total_likes, COALESCE(SUM(favorites_count), 0) AS total_favorites"). + Select("COALESCE(SUM(likes_count), 0) AS total_likes, COALESCE(SUM(favorites_count), 0) AS total_favorites, COALESCE(SUM(views_count), 0) AS total_reads"). Where("user_id = ? AND status = ? AND deleted_at IS NULL", userID, model.PostStatusApproved). Scan(&s).Error - return s.TotalLikes, s.TotalFavorites, err + return s.TotalLikes, s.TotalFavorites, s.TotalReads, err } // FindByUserIDAndStatus 分页查询某用户指定状态的帖子(空 status=全状态) diff --git a/internal/service/repository.go b/internal/service/repository.go index ca6a73c..c67bf1d 100644 --- a/internal/service/repository.go +++ b/internal/service/repository.go @@ -56,7 +56,7 @@ type spaceUserStore interface { type spacePostStore interface { FindByUserID(userID uint, offset, limit int) ([]model.Post, int64, error) CountUserPosts(userID uint) (int64, error) - GetUserStats(userID uint) (likes, favorites int64, err error) + GetUserStats(userID uint) (likes, favorites, reads int64, err error) } // postAdminStatStore AdminService 所需的帖子统计接口(ISP:2 个方法) diff --git a/internal/service/space_service.go b/internal/service/space_service.go index 3011250..de3c79f 100644 --- a/internal/service/space_service.go +++ b/internal/service/space_service.go @@ -29,8 +29,8 @@ func (s *SpaceService) CountUserPosts(uid uint) (int64, error) { return s.postRepo.CountUserPosts(uid) } -// GetUserStats 获取用户文章统计数据(总获赞、总收藏) -func (s *SpaceService) GetUserStats(uid uint) (likes, favorites int64, err error) { +// GetUserStats 获取用户文章统计数据(总获赞、总收藏、总阅读) +func (s *SpaceService) GetUserStats(uid uint) (likes, favorites, reads int64, err error) { return s.postRepo.GetUserStats(uid) } diff --git a/templates/MetaLab-2026/html/posts/show.html b/templates/MetaLab-2026/html/posts/show.html index 36e6262..bbf7566 100644 --- a/templates/MetaLab-2026/html/posts/show.html +++ b/templates/MetaLab-2026/html/posts/show.html @@ -934,8 +934,8 @@ dropdown.style.left = (rect.left + window.scrollX) + 'px'; dropdown.style.width = Math.max(rect.width, 200) + 'px'; - for (var i = 0; i < users.length; i++) { - var u = users[i]; + for (let i = 0; i < users.length; i++) { + let u = users[i]; var item = document.createElement('div'); item.className = 'mention-item'; item.innerHTML = '' + escapeHtml(u.username) + '' + escapeHtml(u.uid) + '';