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

675 lines
20 KiB

  1. package friend_circle
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/im/model"
  5. "applet/app/e"
  6. "applet/app/md/friend_circles"
  7. "applet/app/svc"
  8. svc2 "applet/app/svc/friend_circle"
  9. "applet/app/utils"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  11. svc3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc"
  12. es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
  13. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  14. "context"
  15. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "github.com/gin-gonic/gin"
  19. "github.com/olivere/elastic/v7"
  20. "strconv"
  21. "strings"
  22. "time"
  23. )
  24. // PersonalSendingList
  25. // @Summary 朋友圈-个人发送朋友圈列表
  26. // @Tags 朋友圈
  27. // @Description 个人发送朋友圈列表
  28. // @Accept json
  29. // @Produce json
  30. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  31. // @Param req body friend_circles.PersonalSendingListReq true "签名上传url"
  32. // @Success 200 {object} friend_circles.PersonalSendingListResp "返回数据"
  33. // @Failure 400 {object} md.Response "具体错误"
  34. // @Router /api/v1/circleFriends/personalSending [POST]
  35. func PersonalSendingList(c *gin.Context) {
  36. var req friend_circles.PersonalSendingListReq
  37. err := c.ShouldBindJSON(&req)
  38. if err != nil {
  39. err = svc.HandleValidateErr(err)
  40. err1 := err.(e.E)
  41. e.OutErr(c, err1.Code, err1.Error())
  42. return
  43. }
  44. resp, err := svc2.GetPersonalSendingList(c, req)
  45. if err != nil {
  46. e.OutErr(c, e.ERR, err.Error())
  47. }
  48. resp.Page = req.Page
  49. resp.PageSize = req.PageSize
  50. e.OutSuc(c, resp, nil)
  51. }
  52. // MySelfList
  53. // @Summary 朋友圈-我的朋友圈列表
  54. // @Tags 朋友圈
  55. // @Description 我的朋友圈列表
  56. // @Accept json
  57. // @Produce json
  58. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  59. // @Param req body friend_circles.MySelfListReq true "签名上传url"
  60. // @Success 200 {object} friend_circles.MySelfListResp "返回数据"
  61. // @Failure 400 {object} md.Response "具体错误"
  62. // @Router /api/v1/circleFriends/mySelfList [POST]
  63. func MySelfList(c *gin.Context) {
  64. var args friend_circles.MySelfListReq
  65. err := c.ShouldBindJSON(&args)
  66. if err != nil {
  67. err = svc.HandleValidateErr(err)
  68. err1 := err.(e.E)
  69. e.OutErr(c, err1.Code, err1.Error())
  70. return
  71. }
  72. resp, err := svc2.MySelfList(c, args)
  73. if err != nil {
  74. e.OutErr(c, e.ERR, err.Error())
  75. return
  76. }
  77. imUserIds := make([]int64, len(resp.List))
  78. for i, friendCircle := range resp.List {
  79. imUserIds[i] = friendCircle.ImUid
  80. }
  81. nicknameAndAvatarMap, err := svc2.GetImUserNicknameAndAvatar(imUserIds)
  82. if err != nil {
  83. e.OutErr(c, e.ERR, err.Error())
  84. return
  85. }
  86. for _, friendCircle := range resp.List {
  87. val, ok := nicknameAndAvatarMap[friendCircle.ImUid]
  88. if ok {
  89. friendCircle.ImNickname = val.NickName
  90. friendCircle.ImAvatar = val.Avatar
  91. }
  92. }
  93. resp.Page = args.Page
  94. resp.PageSize = args.PageSize
  95. e.OutSuc(c, resp, nil)
  96. }
  97. // IsCanPublish
  98. // @Summary 朋友圈-是否可以发布朋友圈
  99. // @Tags 朋友圈
  100. // @Description 是否可以发布朋友圈
  101. // @Accept json
  102. // @Produce json
  103. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  104. // @Success 200 {string} "success"
  105. // @Failure 400 {object} md.Response "具体错误"
  106. // @Router /api/v1/circleFriends/isCanPublish [GET]
  107. func IsCanPublish(c *gin.Context) {
  108. isCan, err := svc2.IsCanPublish(c)
  109. if err != nil {
  110. fmt.Println("IsPublish:::::", err.Error())
  111. }
  112. resp := friend_circles.IsCanCommentResp{IsCan: isCan}
  113. e.OutSuc(c, resp, nil)
  114. }
  115. // FriendCircleLikeList
  116. // @Summary 朋友圈-朋友圈点赞列表
  117. // @Tags 朋友圈
  118. // @Description 朋友圈点赞列表
  119. // @Accept json
  120. // @Produce json
  121. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  122. // @Param req body friend_circles.FriendCircleLikeListReq true "签名上传url"
  123. // @Success 200 {object} friend_circles.FriendCircleLikeListResp "返回数据"
  124. // @Failure 400 {object} md.Response "具体错误"
  125. // @Router /api/v1/circleFriends/likeList [POST]
  126. func FriendCircleLikeList(c *gin.Context) {
  127. var req friend_circles.FriendCircleLikeListReq
  128. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  129. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  130. return
  131. }
  132. //查找朋友圈记录
  133. doc, err := es.FirstDoc(md.EggFriendCircleEsIndex, req.CircleIndexId)
  134. if err != nil {
  135. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  136. return
  137. }
  138. if !doc.Found { // 表示没找到数据
  139. e.OutErr(c, e.ERR_NOT_FAN, "朋友圈文档记录不存在")
  140. return
  141. }
  142. from := (req.Page - 1) * req.PageSize
  143. // 查找点赞列表
  144. query := elastic.NewBoolQuery()
  145. query.Must(elastic.NewTermQuery("circle_id", req.CircleIndexId))
  146. searchResult, err := es.EsClient.Search().
  147. Index(md.EggFriendCircleLikeEsAlias). // 替换为你的索引名称
  148. Query(query).
  149. From(from).
  150. Size(req.PageSize).
  151. Sort("created_at", false). // 按时间倒排
  152. Pretty(true).
  153. Do(context.Background())
  154. if err != nil {
  155. return
  156. }
  157. var docs []*md.EggFriendCircleLikeEs
  158. var docUserIds []int64
  159. var total int64
  160. // 检查是否有结果
  161. if searchResult.Hits.TotalHits.Value != 0 {
  162. // 解析结果
  163. total = searchResult.Hits.TotalHits.Value
  164. for _, hit := range searchResult.Hits.Hits {
  165. var like md.EggFriendCircleLikeEs
  166. err = json.Unmarshal(hit.Source, &like)
  167. if err != nil {
  168. return
  169. }
  170. docUserIds = append(docUserIds, like.ImUid)
  171. docs = append(docs, &like)
  172. }
  173. }
  174. nicknameAndAvatarMap, err := svc2.GetImUserNicknameAndAvatar(docUserIds)
  175. if err != nil {
  176. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  177. return
  178. }
  179. var list []*friend_circles.FriendCircleLikeEsStruct
  180. for _, like := range docs {
  181. temp := friend_circles.FriendCircleLikeEsStruct{
  182. Uid: like.Uid,
  183. ImUid: like.ImUid,
  184. CircleId: like.CircleId,
  185. }
  186. val, ok := nicknameAndAvatarMap[like.ImUid]
  187. if ok {
  188. temp.ImNickname = val.NickName
  189. temp.ImAvatar = val.Avatar
  190. }
  191. list = append(list, &temp)
  192. }
  193. resp := friend_circles.FriendCircleLikeListResp{
  194. List: list,
  195. Page: req.Page,
  196. PageSize: req.PageSize,
  197. Total: total,
  198. }
  199. e.OutSuc(c, resp, nil)
  200. }
  201. // Publish
  202. // @Summary 朋友圈-发布朋友圈
  203. // @Tags 朋友圈
  204. // @Description 发布朋友圈
  205. // @Accept json
  206. // @Produce json
  207. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  208. // @Param req body friend_circles.PublishReq true "请求参数"
  209. // @Success 200 {string} "success"
  210. // @Failure 400 {object} md.Response "具体错误"
  211. // @Router /api/v1/circleFriends/publish [POST]
  212. func Publish(c *gin.Context) {
  213. var req friend_circles.PublishReq
  214. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  215. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  216. return
  217. }
  218. isCan, err := svc2.IsCanPublish(c)
  219. if err != nil {
  220. e.OutErr(c, e.ERR, err.Error())
  221. return
  222. }
  223. if !isCan {
  224. e.OutErr(c, e.ERR, "当前不允许发朋友圈")
  225. return
  226. }
  227. user := svc.GetUser(c)
  228. var imUser model.User
  229. _, err = db.DbIm.Where("phone_number = ?", user.Phone).Get(&imUser)
  230. if err != nil {
  231. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  232. return
  233. }
  234. scheme, domain := svc.ImageBucket(db.Db)
  235. var imageStr string
  236. if len(req.ImageList) > 0 {
  237. imageStr = utils.SerializeStr(req.ImageList)
  238. imageStr = strings.ReplaceAll(imageStr, fmt.Sprintf("%s://%s/", scheme, domain), "{{tempHost}}")
  239. }
  240. //插入es记录
  241. now := time.Now()
  242. nowStr := now.Format("2006-01-02 15:04:05")
  243. createDocRet, err := es.CreateDoc(md.EggFriendCircleEsIndex, strconv.FormatInt(user.Id, 10)+"_"+utils.Int64ToStr(now.Unix()), md.EggFriendCircleEs{
  244. Uid: user.Id,
  245. ImUid: imUser.Id,
  246. Kind: 1,
  247. Content: req.Content,
  248. Image: imageStr,
  249. Video: req.Video,
  250. LikesNums: 0,
  251. ShareNums: 0,
  252. CommentNums: 0,
  253. State: 1,
  254. IsTopUp: 2,
  255. IsPraise: 2,
  256. CreatedAt: nowStr,
  257. UpdatedAt: nowStr,
  258. })
  259. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  260. // 更新发朋友圈次数
  261. year, week := time.Now().ISOWeek()
  262. yearStr := utils.IntToStr(year)
  263. weekStr := utils.IntToStr(week)
  264. index := es2.GetAppointIndexFromAlias(yearStr, weekStr)
  265. id := fmt.Sprintf("%d%d_%d", year, week, user.Id)
  266. _, err = es.FirstDoc(index, id)
  267. if err != nil {
  268. if err.Error() != "elastic: Error 404 (Not Found)" {
  269. e.OutErr(c, e.ERR, err.Error())
  270. return
  271. } else {
  272. m := md.EggEnergyUserEggScoreEs{
  273. Uid: user.Id,
  274. SendCircleOfFriendNums: 1,
  275. CreatedAt: nowStr,
  276. UpdatedAt: nowStr,
  277. }
  278. newCreateDoc, err1 := es.CreateDoc(index, id, &m)
  279. if err1 != nil {
  280. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  281. return
  282. }
  283. fmt.Printf("newCreateDoc ==> %+v \n\n", newCreateDoc)
  284. }
  285. } else {
  286. script := elastic.NewScript("ctx._source.send_circle_of_friend_nums += params.inc").Param("inc", 1)
  287. _, err = es.EsClient.Update().
  288. Index(index).
  289. Id(id).
  290. Script(script).
  291. Do(context.Background())
  292. if err != nil {
  293. e.OutErr(c, e.ERR, err.Error())
  294. return
  295. }
  296. }
  297. e.OutSuc(c, "success", nil)
  298. }
  299. // RecommendList
  300. // @Summary 朋友圈-推荐列表
  301. // @Tags 朋友圈
  302. // @Description 推荐列表
  303. // @Accept json
  304. // @Produce json
  305. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  306. // @Param req body friend_circles.RecommendListReq true "签名上传url"
  307. // @Success 200 {object} friend_circles.RecommendListResp "返回数据"
  308. // @Failure 400 {object} md.Response "具体错误"
  309. // @Router /api/v1/circleFriends/recommendList [POST]
  310. func RecommendList(c *gin.Context) {
  311. var req friend_circles.RecommendListReq
  312. err := c.ShouldBindJSON(&req)
  313. if err != nil {
  314. err = svc.HandleValidateErr(err)
  315. err1 := err.(e.E)
  316. e.OutErr(c, err1.Code, err1.Error())
  317. return
  318. }
  319. resp, err := svc2.GetRecommendList(req)
  320. if err != nil {
  321. e.OutErr(c, e.ERR, err.Error())
  322. return
  323. }
  324. imUserIds := make([]int64, len(resp.List))
  325. for i, friendCircle := range resp.List {
  326. imUserIds[i] = friendCircle.ImUid
  327. }
  328. nicknameAndAvatarMap, err := svc2.GetImUserNicknameAndAvatar(imUserIds)
  329. if err != nil {
  330. e.OutErr(c, e.ERR, err.Error())
  331. return
  332. }
  333. for _, friendCircle := range resp.List {
  334. val, ok := nicknameAndAvatarMap[friendCircle.ImUid]
  335. if ok {
  336. friendCircle.ImNickname = val.NickName
  337. friendCircle.ImAvatar = val.Avatar
  338. }
  339. }
  340. e.OutSuc(c, resp, nil)
  341. }
  342. // CommentList
  343. // @Summary 朋友圈-评论列表
  344. // @Tags 朋友圈
  345. // @Description 评论列表
  346. // @Accept json
  347. // @Produce json
  348. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  349. // @Param circle_index_id query string true "朋友圈文档记录"
  350. // @Param req body friend_circles.CommentListReq true "请求参数"
  351. // @Success 200 {string} "success"
  352. // @Failure 400 {object} md.Response "具体错误"
  353. // @Router /api/v1/circleFriends/commentList [POST]
  354. func CommentList(c *gin.Context) {
  355. var req friend_circles.CommentListReq
  356. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  357. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  358. return
  359. }
  360. //查找朋友圈记录
  361. doc, err := es.FirstDoc(md.EggFriendCircleEsIndex, req.CircleIndexId)
  362. if err != nil {
  363. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  364. return
  365. }
  366. if !doc.Found { // 表示没找到数据
  367. e.OutErr(c, e.ERR_NOT_FAN, "朋友圈文档记录不存在")
  368. return
  369. }
  370. resp, err := svc2.CommentList(req)
  371. if err != nil {
  372. e.OutErr(c, e.ERR, err.Error())
  373. return
  374. }
  375. e.OutSuc(c, resp, nil)
  376. }
  377. // CommentDetail
  378. // @Summary 朋友圈-评论详情
  379. // @Tags 朋友圈
  380. // @Description 评论详情
  381. // @Accept json
  382. // @Produce json
  383. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  384. // @Param comment_index_id query string true "评论文档记录"
  385. // @Success 200 {string} "success"
  386. // @Failure 400 {object} md.Response "具体错误"
  387. // @Router /api/v1/circleFriends/commentDetail [GET]
  388. func CommentDetail(c *gin.Context) {
  389. var req friend_circles.CommentDetailReq
  390. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  391. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  392. return
  393. }
  394. //查找评论记录
  395. boolQuery := elastic.NewBoolQuery()
  396. boolQuery.Must(elastic.NewTermQuery("_id", req.CommentIndexId))
  397. searchResult, err := es.EsClient.Search().
  398. Index(md.EggFriendCircleCommentEsAlias).
  399. Query(boolQuery).
  400. Pretty(true).
  401. Do(context.Background())
  402. if err != nil {
  403. e.OutErr(c, e.ERR, err.Error())
  404. return
  405. }
  406. var doc md.EggFriendCircleCommentEs
  407. // 检查是否有结果
  408. if searchResult.Hits.TotalHits.Value != 0 {
  409. err = json.Unmarshal(searchResult.Hits.Hits[0].Source, &doc)
  410. if err != nil {
  411. e.OutErr(c, e.ERR, err.Error())
  412. return
  413. }
  414. } else {
  415. e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
  416. return
  417. }
  418. resp, err := svc2.CommentDetail(req)
  419. if err != nil {
  420. e.OutErr(c, e.ERR, err.Error())
  421. return
  422. }
  423. resp.CircleIndexId = doc.CircleId
  424. e.OutSuc(c, resp, nil)
  425. }
  426. // Delete
  427. // @Summary 朋友圈-删除朋友圈
  428. // @Tags 朋友圈
  429. // @Description 删除朋友圈
  430. // @Accept json
  431. // @Produce json
  432. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  433. // @Param req body friend_circles.EggFriendCircleDelReq true "请求参数"
  434. // @Success 200 {string} "success"
  435. // @Failure 400 {object} md.Response "具体错误"
  436. // @Router /api/v1/circleFriends/delete [DELETE]
  437. func Delete(c *gin.Context) {
  438. var req friend_circles.EggFriendCircleDelReq
  439. err := c.ShouldBindJSON(&req)
  440. if err != nil {
  441. err = svc.HandleValidateErr(err)
  442. err1 := err.(e.E)
  443. e.OutErr(c, err1.Code, err1.Error())
  444. return
  445. }
  446. user := svc.GetUser(c)
  447. aliasName := md.EggFriendCircleEsAlias
  448. // 1.判断是否是自己文章
  449. doc, err := es.FirstDoc(md.EggFriendCircleEsIndex, req.CircleIndexId)
  450. if err != nil {
  451. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  452. return
  453. }
  454. if !doc.Found { // 表示没找到数据
  455. e.OutErr(c, e.ERR_NOT_FAN, "朋友圈文档记录不存在")
  456. return
  457. }
  458. var docInfo md.EggFriendCircleEs
  459. utils.Unserialize(doc.Source, &docInfo)
  460. if docInfo.Uid != user.Id {
  461. e.OutErr(c, e.ERR, errors.New("不允许删除别人的朋友圈"))
  462. return
  463. }
  464. // 2.删除文章
  465. deleteDocRet, err := es.DeleteDoc(aliasName, req.CircleIndexId)
  466. if err != nil {
  467. e.OutErr(c, e.ERR, err.Error())
  468. return
  469. }
  470. fmt.Printf("CreateDoc ==> %+v \n\n", deleteDocRet)
  471. // 3.将评论id推入mq
  472. err = svc2.DelFriendCircleComment(req.CircleIndexId)
  473. if err != nil {
  474. e.OutErr(c, e.ERR, err.Error())
  475. return
  476. }
  477. e.OutSuc(c, "success", nil)
  478. }
  479. // Share
  480. // @Summary 朋友圈-分享后朋友圈分享数(增加)
  481. // @Tags 朋友圈
  482. // @Description 分享后朋友圈分享数(增加)
  483. // @Accept json
  484. // @Produce json
  485. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  486. // @Param req body friend_circles.EggFriendCircleShareReq true "请求参数"
  487. // @Success 200 {string} "success"
  488. // @Failure 400 {object} md.Response "具体错误"
  489. // @Router /api/v1/circleFriends/share [POST]
  490. func Share(c *gin.Context) {
  491. var req friend_circles.EggFriendCircleShareReq
  492. err := c.ShouldBindJSON(&req)
  493. if err != nil {
  494. err = svc.HandleValidateErr(err)
  495. err1 := err.(e.E)
  496. e.OutErr(c, err1.Code, err1.Error())
  497. return
  498. }
  499. script := elastic.NewScript("ctx._source.share_nums += params.inc").Param("inc", 1)
  500. aliasName := md.EggFriendCircleEsAlias
  501. _, err = es.EsClient.Update().
  502. Index(aliasName).
  503. Id(req.CircleIndexId).
  504. Script(script).
  505. Do(context.Background())
  506. if err != nil {
  507. e.OutErr(c, e.ERR, err.Error())
  508. return
  509. }
  510. e.OutSuc(c, "success", nil)
  511. }
  512. // Like
  513. // @Summary 朋友圈-点赞朋友圈
  514. // @Tags 朋友圈
  515. // @Description 点赞朋友圈
  516. // @Accept json
  517. // @Produce json
  518. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  519. // @Param req body friend_circles.EggFriendCircleLikeReq true "请求参数"
  520. // @Success 200 {string} "success"
  521. // @Failure 400 {object} md.Response "具体错误"
  522. // @Router /api/v1/circleFriends/like [POST]
  523. func Like(c *gin.Context) {
  524. var req friend_circles.EggFriendCircleLikeReq
  525. err := c.ShouldBindJSON(&req)
  526. if err != nil {
  527. err = svc.HandleValidateErr(err)
  528. err1 := err.(e.E)
  529. e.OutErr(c, err1.Code, err1.Error())
  530. return
  531. }
  532. user := svc.GetUser(c)
  533. likeEsIndex := svc3.GetEggFriendCircleLikeEsIndex(user.Id)
  534. likeId := svc3.GetEggFriendCircleLikeEsIndexId(user.Id, req.CircleIndexId)
  535. // 判断是否已经点赞过
  536. query := elastic.NewBoolQuery().
  537. Must(
  538. elastic.NewTermQuery("_id", likeId),
  539. )
  540. searchResult, err := es.EsClient.Search().Index(likeEsIndex).
  541. Query(query).
  542. Size(0). // 只关心总数,不实际返回文档
  543. Do(context.Background())
  544. if err != nil {
  545. return
  546. }
  547. total := searchResult.TotalHits()
  548. if int(total) > 0 {
  549. e.OutSuc(c, "success", nil)
  550. return
  551. }
  552. var imUser model.User
  553. _, err = db.DbIm.Where("phone_number = ?", user.Phone).Get(&imUser)
  554. if err != nil {
  555. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  556. return
  557. }
  558. // 新增点赞信息
  559. now := time.Now()
  560. m := md.EggFriendCircleLikeEs{
  561. Uid: user.Id,
  562. ImUid: imUser.Id,
  563. CircleId: req.CircleIndexId,
  564. CreatedAt: now.Format("2006-01-02 15:04:05"),
  565. UpdatedAt: now.Format("2006-01-02 15:04:05"),
  566. }
  567. createDocRet, err := es.CreateDoc(likeEsIndex, likeId, &m)
  568. if err != nil {
  569. e.OutErr(c, e.ERR, err.Error())
  570. return
  571. }
  572. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  573. // 增加文章点赞数
  574. script := elastic.NewScript("ctx._source.likes_nums += params.inc").Param("inc", 1)
  575. aliasName := md.EggFriendCircleEsAlias
  576. _, err = es.EsClient.Update().
  577. Index(aliasName).
  578. Id(req.CircleIndexId).
  579. Script(script).
  580. Do(context.Background())
  581. if err != nil {
  582. e.OutErr(c, e.ERR, err.Error())
  583. return
  584. }
  585. e.OutSuc(c, "success", nil)
  586. }
  587. // CancelLike
  588. // @Summary 朋友圈-取消点赞朋友圈
  589. // @Tags 朋友圈
  590. // @Description 取消点赞朋友圈
  591. // @Accept json
  592. // @Produce json
  593. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  594. // @Param req body friend_circles.EggFriendCircleCancelLikeReq true "请求参数"
  595. // @Success 200 {string} "success"
  596. // @Failure 400 {object} md.Response "具体错误"
  597. // @Router /api/v1/circleFriends/cancelLike [POST]
  598. func CancelLike(c *gin.Context) {
  599. // 减少文章点赞数
  600. var req friend_circles.EggFriendCircleCancelLikeReq
  601. err := c.ShouldBindJSON(&req)
  602. if err != nil {
  603. err = svc.HandleValidateErr(err)
  604. err1 := err.(e.E)
  605. e.OutErr(c, err1.Code, err1.Error())
  606. return
  607. }
  608. user := svc.GetUser(c)
  609. // 删除点赞信息
  610. likeEsIndex := svc3.GetEggFriendCircleLikeEsIndex(user.Id)
  611. likeId := svc3.GetEggFriendCircleLikeEsIndexId(user.Id, req.CircleIndexId)
  612. deleteDocRet, err := es.DeleteDoc(likeEsIndex, likeId)
  613. if err != nil {
  614. if err.Error() == "elastic: Error 404 (Not Found)" {
  615. e.OutErr(c, e.ERR_BAD_REQUEST, errors.New("点赞已取消,请勿重复请求").Error())
  616. return
  617. }
  618. e.OutErr(c, e.ERR, err.Error())
  619. return
  620. }
  621. fmt.Printf("DeleteDoc ==> %+v \n\n", deleteDocRet)
  622. // 减少文章点赞数
  623. script := elastic.NewScript("ctx._source.likes_nums -= params.inc").Param("inc", 1)
  624. aliasName := md.EggFriendCircleEsAlias
  625. _, err = es.EsClient.Update().
  626. Index(aliasName).
  627. Id(req.CircleIndexId).
  628. Script(script).
  629. Do(context.Background())
  630. if err != nil {
  631. e.OutErr(c, e.ERR, err.Error())
  632. return
  633. }
  634. e.OutSuc(c, "success", nil)
  635. }