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