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

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