蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

316 lines
8.7 KiB

  1. package friend_circle
  2. import (
  3. "applet/app/e"
  4. "applet/app/md/friend_circles"
  5. "applet/app/svc"
  6. svc2 "applet/app/svc/friend_circle"
  7. "applet/app/utils/cache"
  8. "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  9. svc3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc"
  10. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  11. "encoding/json"
  12. "fmt"
  13. "github.com/gin-gonic/gin"
  14. "time"
  15. )
  16. // IsCanComment
  17. // @Summary 朋友圈-是否可以评论
  18. // @Tags 朋友圈
  19. // @Description 是否可以评论
  20. // @Accept json
  21. // @Produce json
  22. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  23. // @Success 200 {string} "success"
  24. // @Failure 400 {object} md.Response "具体错误"
  25. // @Router /api/v1/circleFriends/isCanComment [Get]
  26. func IsCanComment(c *gin.Context) {
  27. isCan, err := svc2.IsCanComment(c)
  28. if err != nil {
  29. fmt.Println("IsCanComment:::::", err.Error())
  30. }
  31. resp := friend_circles.IsCanCommentResp{IsCan: isCan}
  32. e.OutSuc(c, resp, nil)
  33. }
  34. // Comment
  35. // @Summary 朋友圈-评论
  36. // @Tags 朋友圈
  37. // @Description 是否可以评论
  38. // @Accept json
  39. // @Produce json
  40. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  41. // @Success 200 {string} "许可链接"
  42. // @Failure 400 {object} md.Response "具体错误"
  43. // @Router /api/v1/circleFriends/comment [Get]
  44. func Comment(c *gin.Context) {
  45. var req friend_circles.CommentReq
  46. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  47. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  48. return
  49. }
  50. isCan, err := svc2.IsCanComment(c)
  51. if err != nil {
  52. e.OutErr(c, e.ERR, err.Error())
  53. return
  54. }
  55. if !isCan {
  56. e.OutErr(c, e.ERR, "当前不允许评论!")
  57. return
  58. }
  59. //查找朋友圈记录
  60. doc, err := es.FirstDoc(md.EggFriendCircleEsIndex, req.CircleIndexId)
  61. if err != nil {
  62. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  63. return
  64. }
  65. if !doc.Found { // 表示没找到数据
  66. e.OutErr(c, e.ERR_NOT_FAN, "朋友圈文档记录不存在")
  67. return
  68. }
  69. var circle md.EggFriendCircleEs
  70. err = json.Unmarshal(doc.Source, &circle)
  71. if err != nil {
  72. e.OutErr(c, e.ERR, err.Error())
  73. return
  74. }
  75. //查找评论记录
  76. var comment md.EggFriendCircleCommentEs
  77. if req.CommentIndexId != "" {
  78. doc1, err1 := es.FirstDoc(md.EggFriendCircleCommentEsAlias, req.CircleIndexId)
  79. if err1 != nil {
  80. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  81. return
  82. }
  83. if !doc1.Found { // 表示没找到数据
  84. e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
  85. return
  86. }
  87. err = json.Unmarshal(doc1.Source, &comment)
  88. if err != nil {
  89. e.OutErr(c, e.ERR, err.Error())
  90. return
  91. }
  92. }
  93. //新增es记录
  94. user := svc.GetUser(c)
  95. now := time.Now()
  96. imUser, err1 := svc.GetImUser(0, user.Phone)
  97. if err1 != nil {
  98. e.OutErr(c, e.ERR, err1.Error())
  99. return
  100. }
  101. var commentId, replyCommentId string
  102. var commentImUid, replyCommentImUid int64
  103. if &comment != nil {
  104. if comment.CommentId != "" {
  105. replyCommentId = req.CommentIndexId
  106. commentImUid = comment.ImUid
  107. } else {
  108. commentId = req.CommentIndexId
  109. commentImUid = comment.ImUid
  110. }
  111. }
  112. createDocRet, err := es.CreateDoc(svc3.GetEggFriendCircleCommentEsIndex(user.Id), svc3.GetEggFriendCircleCommentEsIndexId(user.Id, req.CircleIndexId), md.EggFriendCircleCommentEs{
  113. Uid: user.Id,
  114. ImUid: imUser.UserId,
  115. Kind: 1,
  116. CircleId: req.CircleIndexId,
  117. CommentId: commentId,
  118. CommentImUid: commentImUid,
  119. ReplyCommentId: replyCommentId,
  120. ReplyCommentImUid: replyCommentImUid,
  121. Content: req.Content,
  122. LikesNums: 0,
  123. CommentNums: 0,
  124. State: 1,
  125. IsPraise: 2,
  126. CreatedAt: now.Format("2006-01-02 15:04:05"),
  127. UpdatedAt: now.Format("2006-01-02 15:04:05"),
  128. })
  129. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  130. if err != nil {
  131. e.OutErr(c, e.ERR, err.Error())
  132. return
  133. }
  134. //更新朋友圈&&评论记录
  135. _, err = es.UpdateDoc(md.EggFriendCircleEsIndex, req.CircleIndexId, map[string]interface{}{
  136. "comment_nums": circle.CommentNums + 1,
  137. })
  138. if err != nil {
  139. e.OutErr(c, e.ERR, err.Error())
  140. return
  141. }
  142. if &comment != nil {
  143. _, err = es.UpdateDoc(md.EggFriendCircleCommentEsAlias, req.CommentIndexId, map[string]interface{}{
  144. "comment_nums": comment.CommentNums + 1,
  145. })
  146. if err != nil {
  147. e.OutErr(c, e.ERR, err.Error())
  148. return
  149. }
  150. }
  151. e.OutSuc(c, "success", nil)
  152. }
  153. // CommentDelete
  154. // @Summary 朋友圈-删除评论
  155. // @Tags 朋友圈
  156. // @Description 评论点赞
  157. // @Accept json
  158. // @Produce json
  159. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  160. // @Success 200 {string} "success"
  161. // @Failure 400 {object} md.Response "具体错误"
  162. // @Router /api/v1/circleFriends/CommentDelete/{$comment_index_id} [DELETE]
  163. func CommentDelete(c *gin.Context) {
  164. commentIndexId := c.Param("comment_index_id")
  165. doc1, err := es.FirstDoc(md.EggFriendCircleCommentEsAlias, commentIndexId)
  166. if err != nil {
  167. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  168. return
  169. }
  170. if !doc1.Found { // 表示没找到数据
  171. e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
  172. return
  173. }
  174. //1、删除es数据
  175. _, err = es.DeleteDoc(md.EggFriendCircleCommentEsAlias, commentIndexId)
  176. if err != nil {
  177. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  178. return
  179. }
  180. //2、删除redis数据
  181. key := fmt.Sprintf(md.CommentLikeCacheKey, commentIndexId)
  182. cache.Del(key)
  183. e.OutSuc(c, "success", nil)
  184. }
  185. // CommentLike
  186. // @Summary 朋友圈-评论点赞
  187. // @Tags 朋友圈
  188. // @Description 评论点赞
  189. // @Accept json
  190. // @Produce json
  191. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  192. // @Param comment_index_id query string true "评论文档记录"
  193. // @Success 200 {string} "success"
  194. // @Failure 400 {object} md.Response "具体错误"
  195. // @Router /api/v1/circleFriends/CommentLike [Get]
  196. func CommentLike(c *gin.Context) {
  197. commentIndexId := c.DefaultQuery("comment_index_id", "")
  198. doc, err := es.FirstDoc(md.EggFriendCircleCommentEsAlias, commentIndexId)
  199. if err != nil {
  200. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  201. return
  202. }
  203. if !doc.Found { // 表示没找到数据
  204. e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
  205. return
  206. }
  207. var comment md.EggFriendCircleCommentEs
  208. err = json.Unmarshal(doc.Source, &comment)
  209. if err != nil {
  210. e.OutErr(c, e.ERR, err.Error())
  211. return
  212. }
  213. //1、判断是否点赞
  214. user := svc.GetUser(c)
  215. isLike, err := svc2.GetUserWithCommentLike(commentIndexId, user.Id)
  216. if err != nil {
  217. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  218. return
  219. }
  220. if isLike {
  221. e.OutErr(c, e.ERR, "重复点赞!")
  222. return
  223. }
  224. //2、修改es数据
  225. _, err = es.UpdateDoc(md.EggFriendCircleCommentEsAlias, commentIndexId, map[string]interface{}{
  226. "like_nums": comment.CommentNums + 1,
  227. })
  228. if err != nil {
  229. e.OutErr(c, e.ERR, err.Error())
  230. return
  231. }
  232. //2、进行点赞
  233. key := fmt.Sprintf(md.CommentLikeCacheKey, commentIndexId)
  234. _, err = cache.SetBit(key, user.Id, 1)
  235. if err != nil {
  236. e.OutErr(c, e.ERR, err.Error())
  237. return
  238. }
  239. //3、设置过期时间
  240. _, err = cache.Expire(key, md.CommentLikeCacheTime)
  241. if err != nil {
  242. e.OutErr(c, e.ERR, err.Error())
  243. return
  244. }
  245. e.OutSuc(c, "success", nil)
  246. }
  247. // CommentCancelLike
  248. // @Summary 朋友圈-评论取消点赞
  249. // @Tags 朋友圈
  250. // @Description 评论取消点赞
  251. // @Accept json
  252. // @Produce json
  253. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  254. // @Param comment_index_id query string true "评论文档记录"
  255. // @Success 200 {string} "success"
  256. // @Failure 400 {object} md.Response "具体错误"
  257. // @Router /api/v1/circleFriends/CommentLike [Get]
  258. func CommentCancelLike(c *gin.Context) {
  259. commentIndexId := c.DefaultQuery("comment_index_id", "")
  260. doc, err := es.FirstDoc(md.EggFriendCircleCommentEsAlias, commentIndexId)
  261. if err != nil {
  262. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  263. return
  264. }
  265. if !doc.Found { // 表示没找到数据
  266. e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
  267. return
  268. }
  269. var comment md.EggFriendCircleCommentEs
  270. err = json.Unmarshal(doc.Source, &comment)
  271. if err != nil {
  272. e.OutErr(c, e.ERR, err.Error())
  273. return
  274. }
  275. //1、修改es数据
  276. _, err = es.UpdateDoc(md.EggFriendCircleCommentEsAlias, commentIndexId, map[string]interface{}{
  277. "like_nums": comment.CommentNums - 1,
  278. })
  279. if err != nil {
  280. e.OutErr(c, e.ERR, err.Error())
  281. return
  282. }
  283. //2、修改redis数据
  284. key := fmt.Sprintf(md.CommentLikeCacheKey, commentIndexId)
  285. user := svc.GetUser(c)
  286. _, err = cache.SetBit(key, user.Id, 0)
  287. if err != nil {
  288. e.OutErr(c, e.ERR, err.Error())
  289. return
  290. }
  291. e.OutSuc(c, "success", nil)
  292. }