feat: 添加远程地址字段到评论功能,支持记录用户的远程地址

This commit is contained in:
2025-09-12 15:50:44 +08:00
parent 4c907f0a87
commit 5fab1806bc
5 changed files with 131 additions and 127 deletions

View File

@ -31,6 +31,7 @@ func (cc *CommentController) CreateComment(ctx context.Context, c *app.RequestCo
resps.BadRequest(c, err.Error()) resps.BadRequest(c, err.Error())
return return
} }
req.RemoteAddr = c.RemoteAddr().String()
commentID, err := cc.service.CreateComment(ctx, &req) commentID, err := cc.service.CreateComment(ctx, &req)
if err != nil { if err != nil {
serviceErr := errs.AsServiceError(err) serviceErr := errs.AsServiceError(err)

View File

@ -22,6 +22,7 @@ type CreateCommentReq struct {
Content string `json:"content" binding:"required"` // 评论内容 Content string `json:"content" binding:"required"` // 评论内容
ReplyID uint `json:"reply_id"` // 回复的评论ID ReplyID uint `json:"reply_id"` // 回复的评论ID
IsPrivate bool `json:"is_private"` // 是否私密评论默认false IsPrivate bool `json:"is_private"` // 是否私密评论默认false
RemoteAddr string `json:"remote_addr"` // 远程地址
} }
type UpdateCommentReq struct { type UpdateCommentReq struct {

View File

@ -14,6 +14,7 @@ type Comment struct {
Content string `gorm:"type:text"` // 评论内容 Content string `gorm:"type:text"` // 评论内容
Depth int `gorm:"default:0"` // 评论的层级深度,从0开始计数 Depth int `gorm:"default:0"` // 评论的层级深度,从0开始计数
IsPrivate bool `gorm:"default:false"` // 是否为私密评论,私密评论只有评论者和被评论对象所有者可见 IsPrivate bool `gorm:"default:false"` // 是否为私密评论,私密评论只有评论者和被评论对象所有者可见
RemoteAddr string `gorm:"type:text"` // 远程地址
LikeCount uint64 LikeCount uint64
CommentCount uint64 CommentCount uint64
} }

View File

@ -14,7 +14,7 @@ func registerCommentRoutes(group *route.RouterGroup) {
commentGroup.POST("/c", commentController.CreateComment) commentGroup.POST("/c", commentController.CreateComment)
commentGroup.PUT("/c/:id", commentController.UpdateComment) commentGroup.PUT("/c/:id", commentController.UpdateComment)
commentGroup.DELETE("/c/:id", commentController.DeleteComment) commentGroup.DELETE("/c/:id", commentController.DeleteComment)
commentGroup.PUT("/c/:id/react", commentController.ReactComment) // 暂时先不写 commentGroup.PUT("/c/:id/react", commentController.ReactComment)
commentGroupWithoutAuth.GET("/c/:id", commentController.GetComment) commentGroupWithoutAuth.GET("/c/:id", commentController.GetComment)
commentGroupWithoutAuth.GET("/list", commentController.GetCommentList) commentGroupWithoutAuth.GET("/list", commentController.GetCommentList)
} }

View File

@ -39,6 +39,7 @@ func (cs *CommentService) CreateComment(ctx context.Context, req *dto.CreateComm
TargetType: req.TargetType, TargetType: req.TargetType,
UserID: currentUser.ID, UserID: currentUser.ID,
IsPrivate: req.IsPrivate, IsPrivate: req.IsPrivate,
RemoteAddr: req.RemoteAddr,
} }
commentID, err := repo.Comment.CreateComment(comment) commentID, err := repo.Comment.CreateComment(comment)