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

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