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

hdl_friend_circle.go 20 KiB

2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
1 week ago
2 weeks ago
3 days ago
3 days ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
1 week ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. createDocRet, err := es.CreateDoc(md.EggFriendCircleEsIndex, strconv.FormatInt(user.Id, 10)+"_"+utils.Int64ToStr(now.Unix()), md.EggFriendCircleEs{
  243. Uid: user.Id,
  244. ImUid: imUser.Id,
  245. Kind: 1,
  246. Content: req.Content,
  247. Image: imageStr,
  248. Video: req.Video,
  249. LikesNums: 0,
  250. ShareNums: 0,
  251. CommentNums: 0,
  252. State: 1,
  253. IsTopUp: 2,
  254. IsPraise: 2,
  255. CreatedAt: now.Format("2006-01-02 15:04:05"),
  256. UpdatedAt: now.Format("2006-01-02 15:04:05"),
  257. })
  258. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  259. // 更新发朋友圈次数
  260. year, week := time.Now().ISOWeek()
  261. yearStr := utils.IntToStr(year)
  262. weekStr := utils.IntToStr(week)
  263. index := es2.GetAppointIndexFromAlias(yearStr, weekStr)
  264. id := fmt.Sprintf("%d%d_%d", year, week, user.Id)
  265. _, err = es.FirstDoc(index, id)
  266. if err != nil {
  267. if err.Error() != "elastic: Error 404 (Not Found)" {
  268. e.OutErr(c, e.ERR, err.Error())
  269. return
  270. } else {
  271. m := md.EggEnergyUserEggScoreEs{
  272. Uid: user.Id,
  273. SendCircleOfFriendNums: 1,
  274. }
  275. newCreateDoc, err := es.CreateDoc(index, id, &m)
  276. if err != nil {
  277. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  278. return
  279. }
  280. fmt.Printf("newCreateDoc ==> %+v \n\n", newCreateDoc)
  281. }
  282. } else {
  283. script := elastic.NewScript("ctx._source.send_circle_of_friend_nums += params.inc").Param("inc", 1)
  284. _, err = es.EsClient.Update().
  285. Index(index).
  286. Id(id).
  287. Script(script).
  288. Do(context.Background())
  289. if err != nil {
  290. e.OutErr(c, e.ERR, err.Error())
  291. return
  292. }
  293. }
  294. e.OutSuc(c, "success", nil)
  295. }
  296. // RecommendList
  297. // @Summary 朋友圈-推荐列表
  298. // @Tags 朋友圈
  299. // @Description 推荐列表
  300. // @Accept json
  301. // @Produce json
  302. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  303. // @Param req body friend_circles.RecommendListReq true "签名上传url"
  304. // @Success 200 {object} friend_circles.RecommendListResp "返回数据"
  305. // @Failure 400 {object} md.Response "具体错误"
  306. // @Router /api/v1/circleFriends/recommendList [POST]
  307. func RecommendList(c *gin.Context) {
  308. var req friend_circles.RecommendListReq
  309. err := c.ShouldBindJSON(&req)
  310. if err != nil {
  311. err = svc.HandleValidateErr(err)
  312. err1 := err.(e.E)
  313. e.OutErr(c, err1.Code, err1.Error())
  314. return
  315. }
  316. resp, err := svc2.GetRecommendList(req)
  317. if err != nil {
  318. e.OutErr(c, e.ERR, err.Error())
  319. return
  320. }
  321. imUserIds := make([]int64, len(resp.List))
  322. for i, friendCircle := range resp.List {
  323. imUserIds[i] = friendCircle.ImUid
  324. }
  325. nicknameAndAvatarMap, err := svc2.GetImUserNicknameAndAvatar(imUserIds)
  326. if err != nil {
  327. e.OutErr(c, e.ERR, err.Error())
  328. return
  329. }
  330. for _, friendCircle := range resp.List {
  331. val, ok := nicknameAndAvatarMap[friendCircle.ImUid]
  332. if ok {
  333. friendCircle.ImNickname = val.NickName
  334. friendCircle.ImAvatar = val.Avatar
  335. }
  336. }
  337. e.OutSuc(c, resp, nil)
  338. }
  339. // CommentList
  340. // @Summary 朋友圈-评论列表
  341. // @Tags 朋友圈
  342. // @Description 评论列表
  343. // @Accept json
  344. // @Produce json
  345. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  346. // @Param circle_index_id query string true "朋友圈文档记录"
  347. // @Param req body friend_circles.CommentListReq true "请求参数"
  348. // @Success 200 {string} "success"
  349. // @Failure 400 {object} md.Response "具体错误"
  350. // @Router /api/v1/circleFriends/commentList [POST]
  351. func CommentList(c *gin.Context) {
  352. var req friend_circles.CommentListReq
  353. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  354. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  355. return
  356. }
  357. //查找朋友圈记录
  358. doc, err := es.FirstDoc(md.EggFriendCircleEsIndex, req.CircleIndexId)
  359. if err != nil {
  360. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  361. return
  362. }
  363. if !doc.Found { // 表示没找到数据
  364. e.OutErr(c, e.ERR_NOT_FAN, "朋友圈文档记录不存在")
  365. return
  366. }
  367. resp, err := svc2.CommentList(req)
  368. if err != nil {
  369. e.OutErr(c, e.ERR, err.Error())
  370. return
  371. }
  372. e.OutSuc(c, resp, nil)
  373. }
  374. // CommentDetail
  375. // @Summary 朋友圈-评论详情
  376. // @Tags 朋友圈
  377. // @Description 评论详情
  378. // @Accept json
  379. // @Produce json
  380. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  381. // @Param comment_index_id query string true "评论文档记录"
  382. // @Success 200 {string} "success"
  383. // @Failure 400 {object} md.Response "具体错误"
  384. // @Router /api/v1/circleFriends/commentDetail [GET]
  385. func CommentDetail(c *gin.Context) {
  386. var req friend_circles.CommentDetailReq
  387. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  388. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  389. return
  390. }
  391. //查找评论记录
  392. boolQuery := elastic.NewBoolQuery()
  393. boolQuery.Must(elastic.NewTermQuery("_id", req.CommentIndexId))
  394. searchResult, err := es.EsClient.Search().
  395. Index(md.EggFriendCircleCommentEsAlias).
  396. Query(boolQuery).
  397. Pretty(true).
  398. Do(context.Background())
  399. if err != nil {
  400. e.OutErr(c, e.ERR, err.Error())
  401. return
  402. }
  403. var doc md.EggFriendCircleCommentEs
  404. // 检查是否有结果
  405. if searchResult.Hits.TotalHits.Value != 0 {
  406. err = json.Unmarshal(searchResult.Hits.Hits[0].Source, &doc)
  407. if err != nil {
  408. e.OutErr(c, e.ERR, err.Error())
  409. return
  410. }
  411. } else {
  412. e.OutErr(c, e.ERR_NOT_FAN, "评论文档记录不存在")
  413. return
  414. }
  415. resp, err := svc2.CommentDetail(req)
  416. if err != nil {
  417. e.OutErr(c, e.ERR, err.Error())
  418. return
  419. }
  420. resp.CircleIndexId = doc.CircleId
  421. e.OutSuc(c, resp, nil)
  422. }
  423. // Delete
  424. // @Summary 朋友圈-删除朋友圈
  425. // @Tags 朋友圈
  426. // @Description 删除朋友圈
  427. // @Accept json
  428. // @Produce json
  429. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  430. // @Param req body friend_circles.EggFriendCircleDelReq true "请求参数"
  431. // @Success 200 {string} "success"
  432. // @Failure 400 {object} md.Response "具体错误"
  433. // @Router /api/v1/circleFriends/delete [DELETE]
  434. func Delete(c *gin.Context) {
  435. var req friend_circles.EggFriendCircleDelReq
  436. err := c.ShouldBindJSON(&req)
  437. if err != nil {
  438. err = svc.HandleValidateErr(err)
  439. err1 := err.(e.E)
  440. e.OutErr(c, err1.Code, err1.Error())
  441. return
  442. }
  443. user := svc.GetUser(c)
  444. aliasName := md.EggFriendCircleEsAlias
  445. // 1.判断是否是自己文章
  446. doc, err := es.FirstDoc(md.EggFriendCircleEsIndex, req.CircleIndexId)
  447. if err != nil {
  448. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  449. return
  450. }
  451. if !doc.Found { // 表示没找到数据
  452. e.OutErr(c, e.ERR_NOT_FAN, "朋友圈文档记录不存在")
  453. return
  454. }
  455. var docInfo md.EggFriendCircleEs
  456. utils.Unserialize(doc.Source, &docInfo)
  457. if docInfo.Uid != user.Id {
  458. e.OutErr(c, e.ERR, errors.New("不允许删除别人的朋友圈"))
  459. return
  460. }
  461. // 2.删除文章
  462. deleteDocRet, err := es.DeleteDoc(aliasName, req.CircleIndexId)
  463. if err != nil {
  464. e.OutErr(c, e.ERR, err.Error())
  465. return
  466. }
  467. fmt.Printf("CreateDoc ==> %+v \n\n", deleteDocRet)
  468. // 3.将评论id推入mq
  469. err = svc2.DelFriendCircleComment(req.CircleIndexId)
  470. if err != nil {
  471. e.OutErr(c, e.ERR, err.Error())
  472. return
  473. }
  474. e.OutSuc(c, "success", nil)
  475. }
  476. // Share
  477. // @Summary 朋友圈-分享后朋友圈分享数(增加)
  478. // @Tags 朋友圈
  479. // @Description 分享后朋友圈分享数(增加)
  480. // @Accept json
  481. // @Produce json
  482. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  483. // @Param req body friend_circles.EggFriendCircleShareReq true "请求参数"
  484. // @Success 200 {string} "success"
  485. // @Failure 400 {object} md.Response "具体错误"
  486. // @Router /api/v1/circleFriends/share [POST]
  487. func Share(c *gin.Context) {
  488. var req friend_circles.EggFriendCircleShareReq
  489. err := c.ShouldBindJSON(&req)
  490. if err != nil {
  491. err = svc.HandleValidateErr(err)
  492. err1 := err.(e.E)
  493. e.OutErr(c, err1.Code, err1.Error())
  494. return
  495. }
  496. script := elastic.NewScript("ctx._source.share_nums += params.inc").Param("inc", 1)
  497. aliasName := md.EggFriendCircleEsAlias
  498. _, err = es.EsClient.Update().
  499. Index(aliasName).
  500. Id(req.CircleIndexId).
  501. Script(script).
  502. Do(context.Background())
  503. if err != nil {
  504. e.OutErr(c, e.ERR, err.Error())
  505. return
  506. }
  507. e.OutSuc(c, "success", nil)
  508. }
  509. // Like
  510. // @Summary 朋友圈-点赞朋友圈
  511. // @Tags 朋友圈
  512. // @Description 点赞朋友圈
  513. // @Accept json
  514. // @Produce json
  515. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  516. // @Param req body friend_circles.EggFriendCircleLikeReq true "请求参数"
  517. // @Success 200 {string} "success"
  518. // @Failure 400 {object} md.Response "具体错误"
  519. // @Router /api/v1/circleFriends/like [POST]
  520. func Like(c *gin.Context) {
  521. var req friend_circles.EggFriendCircleLikeReq
  522. err := c.ShouldBindJSON(&req)
  523. if err != nil {
  524. err = svc.HandleValidateErr(err)
  525. err1 := err.(e.E)
  526. e.OutErr(c, err1.Code, err1.Error())
  527. return
  528. }
  529. user := svc.GetUser(c)
  530. likeEsIndex := svc3.GetEggFriendCircleLikeEsIndex(user.Id)
  531. likeId := svc3.GetEggFriendCircleLikeEsIndexId(user.Id, req.CircleIndexId)
  532. // 判断是否已经点赞过
  533. query := elastic.NewBoolQuery().
  534. Must(
  535. elastic.NewTermQuery("_id", likeId),
  536. )
  537. searchResult, err := es.EsClient.Search().Index(likeEsIndex).
  538. Query(query).
  539. Size(0). // 只关心总数,不实际返回文档
  540. Do(context.Background())
  541. if err != nil {
  542. return
  543. }
  544. total := searchResult.TotalHits()
  545. if int(total) > 0 {
  546. e.OutSuc(c, "success", nil)
  547. return
  548. }
  549. var imUser model.User
  550. _, err = db.DbIm.Where("phone_number = ?", user.Phone).Get(&imUser)
  551. if err != nil {
  552. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  553. return
  554. }
  555. // 新增点赞信息
  556. now := time.Now()
  557. m := md.EggFriendCircleLikeEs{
  558. Uid: user.Id,
  559. ImUid: imUser.Id,
  560. CircleId: req.CircleIndexId,
  561. CreatedAt: now.Format("2006-01-02 15:04:05"),
  562. UpdatedAt: now.Format("2006-01-02 15:04:05"),
  563. }
  564. createDocRet, err := es.CreateDoc(likeEsIndex, likeId, &m)
  565. if err != nil {
  566. e.OutErr(c, e.ERR, err.Error())
  567. return
  568. }
  569. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  570. // 增加文章点赞数
  571. script := elastic.NewScript("ctx._source.likes_nums += params.inc").Param("inc", 1)
  572. aliasName := md.EggFriendCircleEsAlias
  573. _, err = es.EsClient.Update().
  574. Index(aliasName).
  575. Id(req.CircleIndexId).
  576. Script(script).
  577. Do(context.Background())
  578. if err != nil {
  579. e.OutErr(c, e.ERR, err.Error())
  580. return
  581. }
  582. e.OutSuc(c, "success", nil)
  583. }
  584. // CancelLike
  585. // @Summary 朋友圈-取消点赞朋友圈
  586. // @Tags 朋友圈
  587. // @Description 取消点赞朋友圈
  588. // @Accept json
  589. // @Produce json
  590. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  591. // @Param req body friend_circles.EggFriendCircleCancelLikeReq true "请求参数"
  592. // @Success 200 {string} "success"
  593. // @Failure 400 {object} md.Response "具体错误"
  594. // @Router /api/v1/circleFriends/cancelLike [POST]
  595. func CancelLike(c *gin.Context) {
  596. // 减少文章点赞数
  597. var req friend_circles.EggFriendCircleCancelLikeReq
  598. err := c.ShouldBindJSON(&req)
  599. if err != nil {
  600. err = svc.HandleValidateErr(err)
  601. err1 := err.(e.E)
  602. e.OutErr(c, err1.Code, err1.Error())
  603. return
  604. }
  605. user := svc.GetUser(c)
  606. // 删除点赞信息
  607. likeEsIndex := svc3.GetEggFriendCircleLikeEsIndex(user.Id)
  608. likeId := svc3.GetEggFriendCircleLikeEsIndexId(user.Id, req.CircleIndexId)
  609. deleteDocRet, err := es.DeleteDoc(likeEsIndex, likeId)
  610. if err != nil {
  611. if err.Error() == "elastic: Error 404 (Not Found)" {
  612. e.OutErr(c, e.ERR_BAD_REQUEST, errors.New("点赞已取消,请勿重复请求").Error())
  613. return
  614. }
  615. e.OutErr(c, e.ERR, err.Error())
  616. return
  617. }
  618. fmt.Printf("DeleteDoc ==> %+v \n\n", deleteDocRet)
  619. // 减少文章点赞数
  620. script := elastic.NewScript("ctx._source.likes_nums -= params.inc").Param("inc", 1)
  621. aliasName := md.EggFriendCircleEsAlias
  622. _, err = es.EsClient.Update().
  623. Index(aliasName).
  624. Id(req.CircleIndexId).
  625. Script(script).
  626. Do(context.Background())
  627. if err != nil {
  628. e.OutErr(c, e.ERR, err.Error())
  629. return
  630. }
  631. e.OutSuc(c, "success", nil)
  632. }