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

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