From 99d16190a8d1f022cfec0d69b9070755813c8b78 Mon Sep 17 00:00:00 2001 From: Victor_Jay Date: Tue, 2 Jun 2026 19:26:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20UID=20=E7=B1=BB=E5=9E=8B=E6=B7=B7?= =?UTF-8?q?=E7=94=A8=E7=BB=9F=E4=B8=80=EF=BC=88uint=20=E2=86=94=20string?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Comment.ReplyToUID string→uint, 移除 varchar(36) gorm 约束 - CommentMention.UID string→uint, 移除 varchar(36) gorm 约束 - UserSearchResult.UID string→uint - 移除 SearchUsersByLevel 中 strconv.FormatUint 转换 - 删除 GetUserUIDByID 方法,CreateReply 直接使用 parent.UserID - fillReplyToNames 使用 map[uint]string 替代 map[string]string - 更新 commentStore 接口声明 - 修复前端 mention 下拉 show.html 中 u.id→u.uid --- internal/model/comment.go | 6 +++--- internal/repository/comment_mention_repo.go | 20 ++++---------------- internal/repository/comment_repo.go | 6 +++--- internal/service/comment_service.go | 15 ++++++--------- internal/service/repository.go | 3 +-- templates/MetaLab-2026/html/posts/show.html | 2 +- 6 files changed, 18 insertions(+), 34 deletions(-) diff --git a/internal/model/comment.go b/internal/model/comment.go index ac292cd..6beeebb 100644 --- a/internal/model/comment.go +++ b/internal/model/comment.go @@ -8,7 +8,7 @@ type Comment struct { PostID uint `gorm:"index;not null" json:"post_id"` RootID uint `gorm:"index;not null" json:"root_id"` // 根评论ID,顶级评论指向自身 ParentID *uint `gorm:"index" json:"parent_id"` // 父评论ID,NULL=顶级评论 - ReplyToUID string `gorm:"type:varchar(36);default:''" json:"reply_to_uid"` // 被回复者UID + ReplyToUID uint `gorm:"default:0" json:"reply_to_uid"` // 被回复者UID Body string `gorm:"type:text;not null" json:"body"` // 评论内容(纯文本 + 图片URL) IsDeleted bool `gorm:"default:false;index" json:"is_deleted"` LikesCount int `gorm:"default:0" json:"likes_count"` @@ -31,12 +31,12 @@ type Comment struct { type CommentMention struct { ID uint `gorm:"primarykey" json:"id"` CommentID uint `gorm:"uniqueIndex:idx_comment_uid,priority:1;not null" json:"comment_id"` - UID string `gorm:"type:varchar(36);uniqueIndex:idx_comment_uid,priority:2;not null" json:"uid"` + UID uint `gorm:"uniqueIndex:idx_comment_uid,priority:2;not null" json:"uid"` } // UserSearchResult @搜索用户结果 type UserSearchResult struct { - UID string `json:"uid"` + UID uint `json:"uid"` Username string `json:"username"` Level int `json:"level"` } diff --git a/internal/repository/comment_mention_repo.go b/internal/repository/comment_mention_repo.go index 46a882c..f8ee0ab 100644 --- a/internal/repository/comment_mention_repo.go +++ b/internal/repository/comment_mention_repo.go @@ -1,8 +1,6 @@ package repository import ( - "strconv" - "metazone.cc/metalab/internal/model" "metazone.cc/metalab/internal/service" ) @@ -21,7 +19,7 @@ func (r *CommentRepo) FindMentionsByComment(commentID uint) ([]model.CommentMent // SearchUsersByLevel 搜索指定等级及以上用户(用于@艾特),返回 uid/username/exp func (r *CommentRepo) SearchUsersByLevel(keyword string, minLevel int, limit int) ([]struct { - UID string + UID uint Username string Exp int }, error) { @@ -42,30 +40,20 @@ func (r *CommentRepo) SearchUsersByLevel(keyword string, minLevel int, limit int Find(&list).Error var res []struct { - UID string + UID uint Username string Exp int } for _, u := range list { res = append(res, struct { - UID string + UID uint Username string Exp int - }{UID: strconv.FormatUint(uint64(u.UID), 10), Username: u.Username, Exp: u.Exp}) + }{UID: u.UID, Username: u.Username, Exp: u.Exp}) } return res, err } -// GetUserUIDByID 通过 uint ID 获取 user 的 UID 字符串 -func (r *CommentRepo) GetUserUIDByID(userID uint) (string, error) { - var uid uint - err := r.db.Table("users").Select("id").Where("id = ?", userID).Scan(&uid).Error - if err != nil { - return "", err - } - return strconv.FormatUint(uint64(uid), 10), nil -} - // GetUsernameByID 通过 userID 获取用户名(用于通知内容) func (r *CommentRepo) GetUsernameByID(userID uint) (string, error) { var username string diff --git a/internal/repository/comment_repo.go b/internal/repository/comment_repo.go index 43fd0a5..8a83cf5 100644 --- a/internal/repository/comment_repo.go +++ b/internal/repository/comment_repo.go @@ -96,7 +96,7 @@ func (r *CommentRepo) fillReplyToNames(list []model.Comment) { return } type nameRow struct { - UID string + UID uint Username string } var rows []nameRow @@ -105,12 +105,12 @@ func (r *CommentRepo) fillReplyToNames(list []model.Comment) { Where("id IN (SELECT reply_to_uid FROM comments WHERE root_id IN (?) AND parent_id IS NOT NULL)", r.rootIDsFromList(list)). Find(&rows) - m := make(map[string]string) + m := make(map[uint]string) for _, row := range rows { m[row.UID] = row.Username } for i := range list { - if list[i].ReplyToUID != "" { + if list[i].ReplyToUID != 0 { list[i].ReplyToName = m[list[i].ReplyToUID] } } diff --git a/internal/service/comment_service.go b/internal/service/comment_service.go index 77463e2..7006571 100644 --- a/internal/service/comment_service.go +++ b/internal/service/comment_service.go @@ -88,15 +88,12 @@ func (s *CommentService) CreateReply(userID uint, parentID uint, body string) (* } comment := &model.Comment{ - PostID: parent.PostID, - RootID: parent.RootID, - ParentID: &parentID, - Body: body, - UserID: userID, - } - // 获取被回复者UID - if replyToUID, err := s.repo.GetUserUIDByID(parent.UserID); err == nil { - comment.ReplyToUID = replyToUID + PostID: parent.PostID, + RootID: parent.RootID, + ParentID: &parentID, + Body: body, + UserID: userID, + ReplyToUID: parent.UserID, } if err := s.repo.Create(comment); err != nil { diff --git a/internal/service/repository.go b/internal/service/repository.go index d94290f..d3a3ddf 100644 --- a/internal/service/repository.go +++ b/internal/service/repository.go @@ -154,11 +154,10 @@ type commentStore interface { FindPostIDByComment(commentID uint) (uint, error) FindPostAuthorID(postID uint) (uint, error) SearchUsersByLevel(keyword string, minLevel, limit int) ([]struct { - UID string + UID uint Username string Exp int }, error) - GetUserUIDByID(userID uint) (string, error) GetUsernameByID(userID uint) (string, error) ListAllComments(keyword string, showDeleted bool, offset, limit int) ([]model.AdminCommentRow, int64, error) } diff --git a/templates/MetaLab-2026/html/posts/show.html b/templates/MetaLab-2026/html/posts/show.html index f870edc..0a739e7 100644 --- a/templates/MetaLab-2026/html/posts/show.html +++ b/templates/MetaLab-2026/html/posts/show.html @@ -931,7 +931,7 @@ let u = users[i]; var item = document.createElement('div'); item.className = 'mention-item'; - item.innerHTML = '' + escapeHtml(u.username) + '' + escapeHtml(u.id) + ''; + item.innerHTML = '' + escapeHtml(u.username) + '' + escapeHtml(u.uid) + ''; item.addEventListener('mousedown', function(e) { e.preventDefault(); selectMention(u);