蛋蛋星球-客户端
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.
 
 
 
 
 

402 lines
12 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"
  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. "context"
  12. "errors"
  13. "fmt"
  14. "github.com/gin-gonic/gin"
  15. "github.com/olivere/elastic/v7"
  16. "strconv"
  17. "time"
  18. )
  19. // MySelfList
  20. // @Summary 朋友圈-我的朋友圈列表
  21. // @Tags 朋友圈
  22. // @Description 我的朋友圈列表
  23. // @Accept json
  24. // @Produce json
  25. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  26. // @Param req body friend_circles.MySelfListReq true "签名上传url"
  27. // @Success 200 {object} friend_circles.MySelfListResp "返回数据"
  28. // @Failure 400 {object} md.Response "具体错误"
  29. // @Router /api/v1/circleFriends/mySelfList [POST]
  30. func MySelfList(c *gin.Context) {
  31. var args friend_circles.MySelfListReq
  32. err := c.ShouldBindJSON(&args)
  33. if err != nil {
  34. err = svc.HandleValidateErr(err)
  35. err1 := err.(e.E)
  36. e.OutErr(c, err1.Code, err1.Error())
  37. return
  38. }
  39. resp, err := svc2.MySelfList(c, args)
  40. if err != nil {
  41. e.OutErr(c, e.ERR, err.Error())
  42. return
  43. }
  44. e.OutSuc(c, resp, nil)
  45. }
  46. // IsCanPublish
  47. // @Summary 朋友圈-是否可以发布朋友圈
  48. // @Tags 朋友圈
  49. // @Description 是否可以发布朋友圈
  50. // @Accept json
  51. // @Produce json
  52. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  53. // @Success 200 {string} "success"
  54. // @Failure 400 {object} md.Response "具体错误"
  55. // @Router /api/v1/circleFriends/isCanPublish [GET]
  56. func IsCanPublish(c *gin.Context) {
  57. isCan, err := svc2.IsCanPublish(c)
  58. if err != nil {
  59. fmt.Println("IsPublish:::::", err.Error())
  60. }
  61. resp := friend_circles.IsCanCommentResp{IsCan: isCan}
  62. e.OutSuc(c, resp, nil)
  63. }
  64. // Publish
  65. // @Summary 朋友圈-发布朋友圈
  66. // @Tags 朋友圈
  67. // @Description 发布朋友圈
  68. // @Accept json
  69. // @Produce json
  70. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  71. // @Param req body friend_circles.PublishReq true "请求参数"
  72. // @Success 200 {string} "success"
  73. // @Failure 400 {object} md.Response "具体错误"
  74. // @Router /api/v1/circleFriends/public [POST]
  75. func Publish(c *gin.Context) {
  76. var req friend_circles.PublishReq
  77. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  78. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  79. return
  80. }
  81. isCan, err := svc2.IsCanPublish(c)
  82. if err != nil {
  83. e.OutErr(c, e.ERR, err.Error())
  84. return
  85. }
  86. if !isCan {
  87. e.OutErr(c, e.ERR, "当前不允许发朋友圈")
  88. return
  89. }
  90. //插入es记录
  91. user := svc.GetUser(c)
  92. now := time.Now()
  93. createDocRet, err := es.CreateDoc(md.EggFriendCircleEsIndex, strconv.FormatInt(user.Id, 10)+"_"+utils.Int64ToStr(now.Unix()), md.EggFriendCircleEs{
  94. Uid: user.Id,
  95. Kind: 1,
  96. Content: req.Content,
  97. Image: utils.SerializeStr(req.ImageList),
  98. Video: req.Video,
  99. LikesNums: 0,
  100. ShareNums: 0,
  101. CommentNums: 0,
  102. State: 1,
  103. IsTopUp: 2,
  104. IsPraise: 2,
  105. CreatedAt: now.Format("2006-01-02 15:04:05"),
  106. UpdatedAt: now.Format("2006-01-02 15:04:05"),
  107. })
  108. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  109. e.OutSuc(c, "success", nil)
  110. }
  111. // CommentList
  112. // @Summary 朋友圈-评论列表
  113. // @Tags 朋友圈
  114. // @Description 评论列表
  115. // @Accept json
  116. // @Produce json
  117. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  118. // @Param circle_index_id query string true "朋友圈文档记录"
  119. // @Param req body friend_circles.CommentListReq true "请求参数"
  120. // @Success 200 {string} "success"
  121. // @Failure 400 {object} md.Response "具体错误"
  122. // @Router /api/v1/circleFriends/commentList [POST]
  123. func CommentList(c *gin.Context) {
  124. var req friend_circles.CommentListReq
  125. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  126. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  127. return
  128. }
  129. //查找朋友圈记录
  130. doc, err := es.FirstDoc(md.EggFriendCircleEsIndex, req.CircleIndexId)
  131. if err != nil {
  132. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  133. return
  134. }
  135. if !doc.Found { // 表示没找到数据
  136. e.OutErr(c, e.ERR_NOT_FAN, "朋友圈文档记录不存在")
  137. return
  138. }
  139. resp, err := svc2.CommentList(req)
  140. if err != nil {
  141. e.OutErr(c, e.ERR, err.Error())
  142. return
  143. }
  144. e.OutSuc(c, resp, nil)
  145. }
  146. // CommentDetail
  147. // @Summary 朋友圈-评论详情
  148. // @Tags 朋友圈
  149. // @Description 评论详情
  150. // @Accept json
  151. // @Produce json
  152. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  153. // @Param comment_index_id query string true "评论文档记录"
  154. // @Success 200 {string} "success"
  155. // @Failure 400 {object} md.Response "具体错误"
  156. // @Router /api/v1/circleFriends/commentDetail [GET]
  157. func CommentDetail(c *gin.Context) {
  158. var req friend_circles.CommentDetailReq
  159. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  160. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  161. return
  162. }
  163. //查找评论记录
  164. doc1, err1 := es.FirstDoc(md.EggFriendCircleCommentEsAlias, req.CommentIndexId)
  165. if err1 != nil {
  166. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  167. return
  168. }
  169. if !doc1.Found { // 表示没找到数据
  170. e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
  171. return
  172. }
  173. resp, err := svc2.CommentDetail(req)
  174. if err != nil {
  175. e.OutErr(c, e.ERR, err.Error())
  176. return
  177. }
  178. e.OutSuc(c, resp, nil)
  179. }
  180. // Delete
  181. // @Summary 朋友圈-删除朋友圈
  182. // @Tags 朋友圈
  183. // @Description 删除朋友圈
  184. // @Accept json
  185. // @Produce json
  186. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  187. // @Param req body friend_circles.EggFriendCircleDelReq true "请求参数"
  188. // @Success 200 {string} "success"
  189. // @Failure 400 {object} md.Response "具体错误"
  190. // @Router /api/v1/circleFriends/delete [DELETE]
  191. func Delete(c *gin.Context) {
  192. var req friend_circles.EggFriendCircleDelReq
  193. err := c.ShouldBindJSON(&req)
  194. if err != nil {
  195. err = svc.HandleValidateErr(err)
  196. err1 := err.(e.E)
  197. e.OutErr(c, err1.Code, err1.Error())
  198. return
  199. }
  200. user := svc.GetUser(c)
  201. aliasName := md.EggFriendCircleEsAlias
  202. // 1.判断是否是自己文章
  203. doc, err := es.FirstDoc(md.EggFriendCircleEsIndex, req.CircleIndexId)
  204. if err != nil {
  205. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  206. return
  207. }
  208. if !doc.Found { // 表示没找到数据
  209. e.OutErr(c, e.ERR_NOT_FAN, "朋友圈文档记录不存在")
  210. return
  211. }
  212. if doc.Uid != utils.Int64ToStr(user.Id) {
  213. e.OutErr(c, e.ERR, errors.New("不允许删除别人的朋友圈"))
  214. return
  215. }
  216. // 2.删除文章
  217. deleteDocRet, err := es.DeleteDoc(aliasName, req.CircleIndexId)
  218. if err != nil {
  219. e.OutErr(c, e.ERR, err.Error())
  220. return
  221. }
  222. fmt.Printf("CreateDoc ==> %+v \n\n", deleteDocRet)
  223. // 3.将评论id推入mq
  224. err = svc2.DelFriendCircleComment(req.CircleIndexId)
  225. if err != nil {
  226. e.OutErr(c, e.ERR, err.Error())
  227. return
  228. }
  229. e.OutSuc(c, "success", nil)
  230. }
  231. // Share
  232. // @Summary 朋友圈-分享后朋友圈分享数(增加)
  233. // @Tags 朋友圈
  234. // @Description 分享后朋友圈分享数(增加)
  235. // @Accept json
  236. // @Produce json
  237. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  238. // @Param req body friend_circles.EggFriendCircleShareReq true "请求参数"
  239. // @Success 200 {string} "success"
  240. // @Failure 400 {object} md.Response "具体错误"
  241. // @Router /api/v1/circleFriends/share [POST]
  242. func Share(c *gin.Context) {
  243. var req friend_circles.EggFriendCircleShareReq
  244. err := c.ShouldBindJSON(&req)
  245. if err != nil {
  246. err = svc.HandleValidateErr(err)
  247. err1 := err.(e.E)
  248. e.OutErr(c, err1.Code, err1.Error())
  249. return
  250. }
  251. script := elastic.NewScript("ctx._source.share_nums += params.inc").Param("inc", 1)
  252. aliasName := md.EggFriendCircleEsAlias
  253. _, err = es.EsClient.Update().
  254. Index(aliasName).
  255. Id(req.CircleIndexId).
  256. Script(script).
  257. Do(context.Background())
  258. if err != nil {
  259. e.OutErr(c, e.ERR, err.Error())
  260. return
  261. }
  262. e.OutSuc(c, "success", nil)
  263. }
  264. // Like
  265. // @Summary 朋友圈-点赞朋友圈
  266. // @Tags 朋友圈
  267. // @Description 点赞朋友圈
  268. // @Accept json
  269. // @Produce json
  270. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  271. // @Param req body friend_circles.EggFriendCircleLikeReq true "请求参数"
  272. // @Success 200 {string} "success"
  273. // @Failure 400 {object} md.Response "具体错误"
  274. // @Router /api/v1/circleFriends/like [POST]
  275. func Like(c *gin.Context) {
  276. var req friend_circles.EggFriendCircleLikeReq
  277. err := c.ShouldBindJSON(&req)
  278. if err != nil {
  279. err = svc.HandleValidateErr(err)
  280. err1 := err.(e.E)
  281. e.OutErr(c, err1.Code, err1.Error())
  282. return
  283. }
  284. user := svc.GetUser(c)
  285. likeEsIndex := svc3.GetEggFriendCircleLikeEsIndex(user.Id)
  286. likeId := svc3.GetEggFriendCircleLikeEsIndexId(user.Id, req.CircleIndexId)
  287. // 判断是否已经点赞过
  288. query := elastic.NewBoolQuery().
  289. Must(
  290. elastic.NewTermQuery("_id", likeId),
  291. )
  292. searchResult, err := es.EsClient.Search().Index(likeEsIndex).
  293. Query(query).
  294. Size(0). // 只关心总数,不实际返回文档
  295. Do(context.Background())
  296. if err != nil {
  297. return
  298. }
  299. total := searchResult.TotalHits()
  300. if int(total) > 0 {
  301. e.OutSuc(c, "success", nil)
  302. return
  303. }
  304. // 新增点赞信息
  305. now := time.Now()
  306. m := md.EggFriendCircleLikeEs{
  307. Uid: user.Id,
  308. ImUid: 0,
  309. CircleId: req.CircleIndexId,
  310. CreatedAt: now.Format("2006-01-02 15:04:05"),
  311. UpdatedAt: now.Format("2006-01-02 15:04:05"),
  312. }
  313. createDocRet, err := es.CreateDoc(likeEsIndex, likeId, &m)
  314. if err != nil {
  315. e.OutErr(c, e.ERR, err.Error())
  316. return
  317. }
  318. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  319. // 增加文章点赞数
  320. script := elastic.NewScript("ctx._source.likes_nums += params.inc").Param("inc", 1)
  321. aliasName := md.EggFriendCircleEsAlias
  322. _, err = es.EsClient.Update().
  323. Index(aliasName).
  324. Id(req.CircleIndexId).
  325. Script(script).
  326. Do(context.Background())
  327. if err != nil {
  328. e.OutErr(c, e.ERR, err.Error())
  329. return
  330. }
  331. e.OutSuc(c, "success", nil)
  332. }
  333. // CancelLike
  334. // @Summary 朋友圈-取消点赞朋友圈
  335. // @Tags 朋友圈
  336. // @Description 取消点赞朋友圈
  337. // @Accept json
  338. // @Produce json
  339. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  340. // @Param req body friend_circles.EggFriendCircleCancelLikeReq true "请求参数"
  341. // @Success 200 {string} "success"
  342. // @Failure 400 {object} md.Response "具体错误"
  343. // @Router /api/v1/circleFriends/cancelLike [POST]
  344. func CancelLike(c *gin.Context) {
  345. // 减少文章点赞数
  346. var req friend_circles.EggFriendCircleCancelLikeReq
  347. err := c.ShouldBindJSON(&req)
  348. if err != nil {
  349. err = svc.HandleValidateErr(err)
  350. err1 := err.(e.E)
  351. e.OutErr(c, err1.Code, err1.Error())
  352. return
  353. }
  354. user := svc.GetUser(c)
  355. // 删除点赞信息
  356. likeEsIndex := svc3.GetEggFriendCircleLikeEsIndex(user.Id)
  357. likeId := svc3.GetEggFriendCircleLikeEsIndexId(user.Id, req.CircleIndexId)
  358. deleteDocRet, err := es.DeleteDoc(likeEsIndex, likeId)
  359. if err != nil {
  360. e.OutErr(c, e.ERR, err.Error())
  361. return
  362. }
  363. fmt.Printf("DeleteDoc ==> %+v \n\n", deleteDocRet)
  364. // 减少文章点赞数
  365. script := elastic.NewScript("ctx._source.likes_nums -= params.inc").Param("inc", 1)
  366. aliasName := md.EggFriendCircleEsAlias
  367. _, err = es.EsClient.Update().
  368. Index(aliasName).
  369. Id(req.CircleIndexId).
  370. Script(script).
  371. Do(context.Background())
  372. if err != nil {
  373. e.OutErr(c, e.ERR, err.Error())
  374. return
  375. }
  376. e.OutSuc(c, "success", nil)
  377. }