|
- package friend_circle
-
- import (
- "applet/app/e"
- "applet/app/md/friend_circles"
- "applet/app/svc"
- svc2 "applet/app/svc/friend_circle"
- "applet/app/utils/cache"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
- svc3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "time"
- )
-
- // IsCanComment
- // @Summary 朋友圈-是否可以评论
- // @Tags 朋友圈
- // @Description 是否可以评论
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/circleFriends/isCanComment [Get]
- func IsCanComment(c *gin.Context) {
- isCan, err := svc2.IsCanComment(c)
- if err != nil {
- fmt.Println("IsCanComment:::::", err.Error())
- }
- resp := friend_circles.IsCanCommentResp{IsCan: isCan}
- e.OutSuc(c, resp, nil)
- }
-
- // Comment
- // @Summary 朋友圈-评论
- // @Tags 朋友圈
- // @Description 是否可以评论
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Success 200 {string} "许可链接"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/circleFriends/comment [Get]
- func Comment(c *gin.Context) {
- var req friend_circles.CommentReq
- if err1 := c.ShouldBindJSON(&req); err1 != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
- return
- }
- isCan, err := svc2.IsCanComment(c)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- if !isCan {
- e.OutErr(c, e.ERR, "当前不允许评论!")
- return
- }
-
- //查找朋友圈记录
- doc, err := es.FirstDoc(md.EggFriendCircleEsIndex, req.CircleIndexId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if !doc.Found { // 表示没找到数据
- e.OutErr(c, e.ERR_NOT_FAN, "朋友圈文档记录不存在")
- return
- }
- var circle md.EggFriendCircleEs
- err = json.Unmarshal(doc.Source, &circle)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- //查找评论记录
- var comment md.EggFriendCircleCommentEs
- if req.CommentIndexId != "" {
- doc1, err1 := es.FirstDoc(md.EggFriendCircleCommentEsAlias, req.CircleIndexId)
- if err1 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- if !doc1.Found { // 表示没找到数据
- e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
- return
- }
- err = json.Unmarshal(doc1.Source, &comment)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- }
-
- //新增es记录
- user := svc.GetUser(c)
- now := time.Now()
- imUser, err1 := svc.GetImUser(0, user.Phone)
- if err1 != nil {
- e.OutErr(c, e.ERR, err1.Error())
- return
- }
- var commentId, replyCommentId string
- var commentImUid, replyCommentImUid int64
- if &comment != nil {
- if comment.CommentId != "" {
- replyCommentId = req.CommentIndexId
- commentImUid = comment.ImUid
- } else {
- commentId = req.CommentIndexId
- commentImUid = comment.ImUid
- }
- }
- createDocRet, err := es.CreateDoc(svc3.GetEggFriendCircleCommentEsIndex(user.Id), svc3.GetEggFriendCircleCommentEsIndexId(user.Id, req.CircleIndexId), md.EggFriendCircleCommentEs{
- Uid: user.Id,
- ImUid: imUser.UserId,
- Kind: 1,
- CircleId: req.CircleIndexId,
- CommentId: commentId,
- CommentImUid: commentImUid,
- ReplyCommentId: replyCommentId,
- ReplyCommentImUid: replyCommentImUid,
- Content: req.Content,
- LikesNums: 0,
- CommentNums: 0,
- State: 1,
- IsPraise: 2,
- CreatedAt: now.Format("2006-01-02 15:04:05"),
- UpdatedAt: now.Format("2006-01-02 15:04:05"),
- })
-
- fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- //更新朋友圈&&评论记录
- _, err = es.UpdateDoc(md.EggFriendCircleEsIndex, req.CircleIndexId, map[string]interface{}{
- "comment_nums": circle.CommentNums + 1,
- })
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- if &comment != nil {
- _, err = es.UpdateDoc(md.EggFriendCircleCommentEsAlias, req.CommentIndexId, map[string]interface{}{
- "comment_nums": comment.CommentNums + 1,
- })
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- }
-
- e.OutSuc(c, "success", nil)
- }
-
- // CommentDelete
- // @Summary 朋友圈-删除评论
- // @Tags 朋友圈
- // @Description 评论点赞
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/circleFriends/CommentDelete/{$comment_index_id} [DELETE]
- func CommentDelete(c *gin.Context) {
- commentIndexId := c.Param("comment_index_id")
- doc1, err := es.FirstDoc(md.EggFriendCircleCommentEsAlias, commentIndexId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if !doc1.Found { // 表示没找到数据
- e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
- return
- }
-
- //1、删除es数据
- _, err = es.DeleteDoc(md.EggFriendCircleCommentEsAlias, commentIndexId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- //2、删除redis数据
- key := fmt.Sprintf(md.CommentLikeCacheKey, commentIndexId)
- cache.Del(key)
-
- e.OutSuc(c, "success", nil)
- }
-
- // CommentLike
- // @Summary 朋友圈-评论点赞
- // @Tags 朋友圈
- // @Description 评论点赞
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param comment_index_id query string true "评论文档记录"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/circleFriends/CommentLike [Get]
- func CommentLike(c *gin.Context) {
- commentIndexId := c.DefaultQuery("comment_index_id", "")
- doc, err := es.FirstDoc(md.EggFriendCircleCommentEsAlias, commentIndexId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if !doc.Found { // 表示没找到数据
- e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
- return
- }
- var comment md.EggFriendCircleCommentEs
- err = json.Unmarshal(doc.Source, &comment)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- //1、判断是否点赞
- user := svc.GetUser(c)
- isLike, err := svc2.GetUserWithCommentLike(commentIndexId, user.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if isLike {
- e.OutErr(c, e.ERR, "重复点赞!")
- return
- }
-
- //2、修改es数据
- _, err = es.UpdateDoc(md.EggFriendCircleCommentEsAlias, commentIndexId, map[string]interface{}{
- "like_nums": comment.CommentNums + 1,
- })
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- //2、进行点赞
- key := fmt.Sprintf(md.CommentLikeCacheKey, commentIndexId)
- _, err = cache.SetBit(key, user.Id, 1)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- //3、设置过期时间
- _, err = cache.Expire(key, md.CommentLikeCacheTime)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- e.OutSuc(c, "success", nil)
- }
-
- // CommentCancelLike
- // @Summary 朋友圈-评论取消点赞
- // @Tags 朋友圈
- // @Description 评论取消点赞
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param comment_index_id query string true "评论文档记录"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/circleFriends/CommentLike [Get]
- func CommentCancelLike(c *gin.Context) {
- commentIndexId := c.DefaultQuery("comment_index_id", "")
-
- doc, err := es.FirstDoc(md.EggFriendCircleCommentEsAlias, commentIndexId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if !doc.Found { // 表示没找到数据
- e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
- return
- }
- var comment md.EggFriendCircleCommentEs
- err = json.Unmarshal(doc.Source, &comment)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- //1、修改es数据
- _, err = es.UpdateDoc(md.EggFriendCircleCommentEsAlias, commentIndexId, map[string]interface{}{
- "like_nums": comment.CommentNums - 1,
- })
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- //2、修改redis数据
- key := fmt.Sprintf(md.CommentLikeCacheKey, commentIndexId)
- user := svc.GetUser(c)
- _, err = cache.SetBit(key, user.Id, 0)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- }
|