fix: 评论系统全量检查修复(7 项 Gap 全部修复)

- Delete 权限:新增 requesterID/isAdmin 参数,Service 层鉴权(仅本人/管理员可删)
- 通知内容:改用 GetUsernameByID 显示真实用户名,非数字 UID
- SearchUsers:Repository 返回 exp,Service 调用 GetLevelByExp 计算等级
- 通知高亮:Notification 新增 CommentID 字段,消息链接支持 #comment-{id}
- 已删除提示:前端 hash 检测 + 3 秒超时 Toast "该评论已不存在"
- Admin CSS:移除不存在的 comments.css 引用
- 错误哨兵:改用 common.ErrCommentNotFound/ErrCommentEmpty/PermissionDenied
This commit is contained in:
2026-06-01 14:05:47 +08:00
parent 36c571b6bb
commit 43c35ddaf2
12 changed files with 119 additions and 45 deletions

View File

@ -185,23 +185,23 @@ func (r *CommentRepo) FindMentionsByComment(commentID uint) ([]model.CommentMent
return list, err
}
// SearchUsersByLevel 搜索 LV3+ 用户(用于@艾特)
// SearchUsersByLevel 搜索指定等级及以上用户(用于@艾特),返回 uid/username/exp
func (r *CommentRepo) SearchUsersByLevel(keyword string, minLevel int, limit int) ([]struct {
UID string
Username string
Exp int
}, error) {
// LV3 = 1500 exp
type result struct {
UID string
UID uint
Username string
Exp int
}
var list []result
// level 是计算字段,用 exp >= thresholds[minLevel] 判断
threshold := model.LevelThresholds[minLevel]
err := r.db.Table("users").
Select("uid, username").
Select("uid, username, exp").
Where("username LIKE ? AND exp >= ? AND deleted_at IS NULL", "%"+keyword+"%", threshold).
Order("exp DESC").
Limit(limit).
@ -210,12 +210,14 @@ func (r *CommentRepo) SearchUsersByLevel(keyword string, minLevel int, limit int
var res []struct {
UID string
Username string
Exp int
}
for _, u := range list {
res = append(res, struct {
UID string
Username string
}{UID: u.UID, Username: u.Username})
Exp int
}{UID: strconv.FormatUint(uint64(u.UID), 10), Username: u.Username, Exp: u.Exp})
}
return res, err
}
@ -230,6 +232,13 @@ func (r *CommentRepo) GetUserUIDByID(userID uint) (string, error) {
return strconv.FormatUint(uint64(uid), 10), nil
}
// GetUsernameByID 通过 userID 获取用户名(用于通知内容)
func (r *CommentRepo) GetUsernameByID(userID uint) (string, error) {
var username string
err := r.db.Table("users").Select("username").Where("uid = ?", userID).Scan(&username).Error
return username, err
}
// FindPostIDByComment 查询评论所属的文章ID
func (r *CommentRepo) FindPostIDByComment(commentID uint) (uint, error) {
var postID uint