Files
web_huangguo_web/app/api/web/comment.go
T
fish03andClaude Opus 5.5 bef9f8b4ed Initial commit
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 14:43:16 +07:00

283 lines
6.3 KiB
Go

package web
import (
"net/http"
"strconv"
"strings"
"unicode/utf8"
"videoweb/app/service/commentser"
"videoweb/pub/common/log"
"videoweb/pub/common/useragent"
"github.com/gin-gonic/gin"
)
// CommentList 代理上游 GET /api/app/comment/list(加载更多等)。
func CommentList(c *gin.Context) {
objectID, _ := strconv.ParseInt(c.Query("objectId"), 10, 64)
objectType, _ := strconv.Atoi(c.Query("objectType"))
pageNum, _ := strconv.Atoi(c.Query("pageNum"))
pageSize, _ := strconv.Atoi(c.Query("pageSize"))
sortOrder, _ := strconv.Atoi(c.Query("sortOrder"))
q := commentser.ListQuery{
ObjectID: objectID,
ObjectType: objectType,
PageNum: pageNum,
PageSize: pageSize,
SortOrder: sortOrder,
}
if q.ObjectID <= 0 {
c.JSON(http.StatusOK, gin.H{
"code": 1001,
"msg": "参数错误",
"tip": "缺少 objectId",
"data": nil,
})
return
}
data, err := commentser.FetchList(c, q)
if err != nil {
log.Error("comment list upstream failed", log.E(err))
c.JSON(http.StatusOK, gin.H{
"code": 500,
"msg": "comment list failed",
"tip": "获取评论失败,请稍后重试",
"data": nil,
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "success",
"tip": "成功",
"data": data,
})
}
type commentSendBody struct {
ObjectID int64 `json:"objectId"`
ObjectType int `json:"objectType"`
Text string `json:"text"`
ParentsID int64 `json:"parentsId"`
ReplyCommentID int64 `json:"replyCommentId"`
}
// CommentSend 代理上游 POST /api/app/comment/send。
// parentsId=0 一级评论;parentsId>0 二级评论;replyCommentId 为实际被回复评论。
func CommentSend(c *gin.Context) {
if useragent.TokenFromRequest(c.Request) == "" {
c.JSON(http.StatusOK, gin.H{
"code": 401,
"msg": "unauthorized",
"tip": "请先登录后再发表评论",
"data": nil,
})
return
}
var body commentSendBody
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 1001,
"msg": "参数错误",
"tip": "参数错误",
"data": nil,
})
return
}
body.Text = strings.TrimSpace(body.Text)
if body.ObjectID <= 0 {
c.JSON(http.StatusOK, gin.H{
"code": 1001,
"msg": "参数错误",
"tip": "缺少 objectId",
"data": nil,
})
return
}
if body.ObjectType != commentser.ObjectTypeChapter {
body.ObjectType = commentser.ObjectTypePlaylet
}
if body.Text == "" {
c.JSON(http.StatusOK, gin.H{
"code": 1001,
"msg": "参数错误",
"tip": "请输入评论内容",
"data": nil,
})
return
}
if utf8.RuneCountInString(body.Text) > 200 {
c.JSON(http.StatusOK, gin.H{
"code": 1001,
"msg": "参数错误",
"tip": "评论最多 200 字",
"data": nil,
})
return
}
if body.ParentsID < 0 {
body.ParentsID = 0
}
if body.ReplyCommentID < 0 {
body.ReplyCommentID = 0
}
env, err := commentser.Send(c, commentser.CommentSendReq{
ObjectID: body.ObjectID,
ObjectType: body.ObjectType,
Text: body.Text,
ParentsID: body.ParentsID,
ReplyCommentID: body.ReplyCommentID,
})
if err != nil {
log.Error("comment send upstream failed", log.E(err))
c.JSON(http.StatusOK, gin.H{
"code": 500,
"msg": "comment send failed",
"tip": "发表失败,请稍后重试",
"data": nil,
})
return
}
if env != nil && TryJSONSessionRecover(c, env.Code) {
env, err = commentser.Send(c, commentser.CommentSendReq{
ObjectID: body.ObjectID,
ObjectType: body.ObjectType,
Text: body.Text,
ParentsID: body.ParentsID,
ReplyCommentID: body.ReplyCommentID,
})
if err != nil {
log.Error("comment send retry failed", log.E(err))
c.JSON(http.StatusOK, gin.H{
"code": 500,
"msg": "comment send failed",
"tip": "发表失败,请稍后重试",
"data": nil,
})
return
}
}
if env == nil || env.Code != 200 {
tip := "发表失败"
msg := ""
code := 500
if env != nil {
code = env.Code
msg = env.Msg
tip = firstNonEmpty(env.Tip, env.Msg, tip)
}
c.JSON(http.StatusOK, gin.H{
"code": code,
"msg": msg,
"tip": tip,
"data": nil,
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": firstNonEmpty(env.Msg, "success"),
"tip": firstNonEmpty(env.Tip, "发表成功"),
"data": env.Data,
})
}
type commentLikeBody struct {
CommentID int64 `json:"commentId"`
IsLike *bool `json:"isLike"`
}
// CommentLikeSet 代理上游 POST /api/app/comment/like/set。
func CommentLikeSet(c *gin.Context) {
if useragent.TokenFromRequest(c.Request) == "" {
c.JSON(http.StatusOK, gin.H{
"code": 401,
"msg": "unauthorized",
"tip": "请先登录后再点赞",
"data": nil,
})
return
}
var body commentLikeBody
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 1001,
"msg": "参数错误",
"tip": "参数错误",
"data": nil,
})
return
}
if body.CommentID <= 0 || body.IsLike == nil {
c.JSON(http.StatusOK, gin.H{
"code": 1001,
"msg": "参数错误",
"tip": "缺少 commentId 或 isLike",
"data": nil,
})
return
}
env, err := commentser.LikeSet(c, commentser.CommentLikeSetReq{
CommentID: body.CommentID,
IsLike: *body.IsLike,
})
if err != nil {
log.Error("comment like set upstream failed", log.E(err))
c.JSON(http.StatusOK, gin.H{
"code": 500,
"msg": "comment like failed",
"tip": "操作失败,请稍后重试",
"data": nil,
})
return
}
if env != nil && TryJSONSessionRecover(c, env.Code) {
env, err = commentser.LikeSet(c, commentser.CommentLikeSetReq{
CommentID: body.CommentID,
IsLike: *body.IsLike,
})
if err != nil {
log.Error("comment like set retry failed", log.E(err))
c.JSON(http.StatusOK, gin.H{
"code": 500,
"msg": "comment like failed",
"tip": "操作失败,请稍后重试",
"data": nil,
})
return
}
}
if env == nil || env.Code != 200 {
tip := "操作失败"
msg := ""
code := 500
if env != nil {
code = env.Code
msg = env.Msg
tip = firstNonEmpty(env.Tip, env.Msg, tip)
}
c.JSON(http.StatusOK, gin.H{
"code": code,
"msg": msg,
"tip": tip,
"data": nil,
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": firstNonEmpty(env.Msg, "success"),
"tip": firstNonEmpty(env.Tip, "成功"),
"data": env.Data,
})
}