156 lines
3.6 KiB
Go
156 lines
3.6 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"videoweb/app/service/commentser"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// CommentList BFF:代理 GET /api/app/comment/list。
|
|
// 文档:http://172.104.181.99:25319/swagger/index.html#/评论APP/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"))
|
|
if objectID <= 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"msg": "objectId required",
|
|
"tip": "缺少评论对象",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
data := commentser.FetchListOrEmpty(c, commentser.ListQuery{
|
|
ObjectID: objectID,
|
|
ObjectType: objectType,
|
|
PageNum: pageNum,
|
|
PageSize: pageSize,
|
|
SortOrder: sortOrder,
|
|
})
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"msg": "ok",
|
|
"tip": "成功",
|
|
"data": data,
|
|
})
|
|
}
|
|
|
|
// CommentSend BFF:代理 POST /api/app/comment/send。
|
|
// 文档:http://172.104.181.99:25319/swagger/index.html#/评论APP/post_api_app_comment_send
|
|
func CommentSend(c *gin.Context) {
|
|
var req commentser.CommentSendReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"msg": "invalid body",
|
|
"tip": "参数错误",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
env, err := commentser.Send(c, req)
|
|
if err != nil {
|
|
tip := "发布失败"
|
|
msg := err.Error()
|
|
switch {
|
|
case strings.Contains(msg, "empty text"):
|
|
tip = "请输入评论内容"
|
|
case strings.Contains(msg, "too long"):
|
|
tip = "评论不能超过 200 字"
|
|
case strings.Contains(msg, "invalid objectId"):
|
|
tip = "缺少评论对象"
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 400,
|
|
"msg": msg,
|
|
"tip": tip,
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
if env == nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 500,
|
|
"msg": "empty response",
|
|
"tip": "发布失败",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
tip := env.Tip
|
|
if tip == "" {
|
|
tip = env.Msg
|
|
}
|
|
// 上游参数错误兜底提示(例如 objectType 不被支持)
|
|
if env.Code == 1001 && (tip == "" || tip == "参数错误") {
|
|
tip = "评论参数错误,请稍后重试"
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": env.Code,
|
|
"msg": env.Msg,
|
|
"tip": tip,
|
|
"data": env.Data,
|
|
})
|
|
}
|
|
|
|
// CommentLikeSet BFF:代理 POST /api/app/comment/like/set。
|
|
// 文档:http://172.104.181.99:25319/swagger/index.html#/评论APP/post_api_app_comment_like_set
|
|
func CommentLikeSet(c *gin.Context) {
|
|
var body struct {
|
|
CommentID int64 `json:"commentId"`
|
|
IsLike *bool `json:"isLike"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"msg": "invalid body",
|
|
"tip": "参数错误",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
if body.CommentID <= 0 || body.IsLike == nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 400,
|
|
"msg": "commentId/isLike required",
|
|
"tip": "缺少评论 ID 或点赞状态",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
env, err := commentser.LikeSet(c, commentser.CommentLikeSetReq{
|
|
CommentID: body.CommentID,
|
|
IsLike: *body.IsLike,
|
|
})
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 500,
|
|
"msg": err.Error(),
|
|
"tip": "操作失败,请稍后重试",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
if env == nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 500,
|
|
"msg": "empty response",
|
|
"tip": "操作失败,请稍后重试",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": env.Code,
|
|
"msg": env.Msg,
|
|
"tip": env.Tip,
|
|
"data": env.Data,
|
|
})
|
|
}
|