fix: 实现空间 TotalReads 统计,修复 selectMention 闭包

- P0-2: posts/show.html selectMention 闭包 var→let 修复
- P0-3: GetUserStats 增加 SUM(views_count) 统计,替换硬编码 int64(0)
- 更新 Fix_List.md 标注 P0-1/2/3 已修复
This commit is contained in:
2026-06-02 15:21:37 +08:00
parent c1ad8a0121
commit 166e01c7c3
6 changed files with 14 additions and 13 deletions

View File

@ -55,7 +55,7 @@ type spaceUseCase interface {
GetSpaceUser(uid uint) (*model.User, error) GetSpaceUser(uid uint) (*model.User, error)
GetPostsByUser(uid uint, page, pageSize int) ([]model.Post, int64, error) GetPostsByUser(uid uint, page, pageSize int) ([]model.Post, int64, error)
CountUserPosts(uid uint) (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 UpdateUser(user *model.User) error
} }

View File

@ -81,7 +81,7 @@ func (ctrl *SpaceController) ShowSpace(c *gin.Context) {
// 构建基础数据(含用户统计) // 构建基础数据(含用户统计)
postCount, _ := ctrl.spaceService.CountUserPosts(uint(uid)) postCount, _ := ctrl.spaceService.CountUserPosts(uint(uid))
totalLikes, totalFavorites, _ := ctrl.spaceService.GetUserStats(uint(uid)) totalLikes, totalFavorites, totalReads, _ := ctrl.spaceService.GetUserStats(uint(uid))
data := gin.H{ data := gin.H{
"Title": spaceUser.Username + " 的空间", "Title": spaceUser.Username + " 的空间",
@ -93,7 +93,7 @@ func (ctrl *SpaceController) ShowSpace(c *gin.Context) {
"PostCount": postCount, "PostCount": postCount,
"TotalLikes": totalLikes, "TotalLikes": totalLikes,
"TotalFavorites": totalFavorites, "TotalFavorites": totalFavorites,
"TotalReads": int64(0), // 暂无阅读量统计 "TotalReads": totalReads,
} }
// ---- 根据 Tab 加载对应数据 ---- // ---- 根据 Tab 加载对应数据 ----

View File

@ -93,18 +93,19 @@ func (r *PostRepo) CountUserPosts(userID uint) (int64, error) {
return count, err return count, err
} }
// GetUserStats 获取用户的文章统计数据(总获赞/总收藏) // GetUserStats 获取用户的文章统计数据(总获赞/总收藏/总阅读
func (r *PostRepo) GetUserStats(userID uint) (likes, favorites int64, err error) { func (r *PostRepo) GetUserStats(userID uint) (likes, favorites, reads int64, err error) {
type stats struct { type stats struct {
TotalLikes int64 TotalLikes int64
TotalFavorites int64 TotalFavorites int64
TotalReads int64
} }
var s stats var s stats
err = r.db.Model(&model.Post{}). 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). Where("user_id = ? AND status = ? AND deleted_at IS NULL", userID, model.PostStatusApproved).
Scan(&s).Error Scan(&s).Error
return s.TotalLikes, s.TotalFavorites, err return s.TotalLikes, s.TotalFavorites, s.TotalReads, err
} }
// FindByUserIDAndStatus 分页查询某用户指定状态的帖子(空 status=全状态) // FindByUserIDAndStatus 分页查询某用户指定状态的帖子(空 status=全状态)

View File

@ -56,7 +56,7 @@ type spaceUserStore interface {
type spacePostStore interface { type spacePostStore interface {
FindByUserID(userID uint, offset, limit int) ([]model.Post, int64, error) FindByUserID(userID uint, offset, limit int) ([]model.Post, int64, error)
CountUserPosts(userID uint) (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 所需的帖子统计接口ISP2 个方法) // postAdminStatStore AdminService 所需的帖子统计接口ISP2 个方法)

View File

@ -29,8 +29,8 @@ func (s *SpaceService) CountUserPosts(uid uint) (int64, error) {
return s.postRepo.CountUserPosts(uid) return s.postRepo.CountUserPosts(uid)
} }
// GetUserStats 获取用户文章统计数据(总获赞、总收藏) // GetUserStats 获取用户文章统计数据(总获赞、总收藏、总阅读
func (s *SpaceService) GetUserStats(uid uint) (likes, favorites int64, err error) { func (s *SpaceService) GetUserStats(uid uint) (likes, favorites, reads int64, err error) {
return s.postRepo.GetUserStats(uid) return s.postRepo.GetUserStats(uid)
} }

View File

@ -934,8 +934,8 @@
dropdown.style.left = (rect.left + window.scrollX) + 'px'; dropdown.style.left = (rect.left + window.scrollX) + 'px';
dropdown.style.width = Math.max(rect.width, 200) + 'px'; dropdown.style.width = Math.max(rect.width, 200) + 'px';
for (var i = 0; i < users.length; i++) { for (let i = 0; i < users.length; i++) {
var u = users[i]; let u = users[i];
var item = document.createElement('div'); var item = document.createElement('div');
item.className = 'mention-item'; item.className = 'mention-item';
item.innerHTML = '<span class="mention-username">' + escapeHtml(u.username) + '</span><span class="mention-uid">' + escapeHtml(u.uid) + '</span>'; item.innerHTML = '<span class="mention-username">' + escapeHtml(u.username) + '</span><span class="mention-uid">' + escapeHtml(u.uid) + '</span>';