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

svc_firend_circle.go 5.5 KiB

1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/im/model"
  5. "applet/app/md/friend_circles"
  6. "applet/app/svc"
  7. "applet/app/utils/logx"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  9. "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  10. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  11. "context"
  12. "encoding/json"
  13. "errors"
  14. "github.com/gin-gonic/gin"
  15. "github.com/olivere/elastic/v7"
  16. "time"
  17. )
  18. func IsCanPublish(c *gin.Context) (isCan bool, err error) {
  19. user := svc.GetUser(c)
  20. //1、检测是否已实名
  21. eggFriendCircleBasicDb := implement.NewEggFriendCircleBasicDb(db.Db)
  22. eggFriendCircleBasic, err := eggFriendCircleBasicDb.EggFriendCircleBasicGet()
  23. if err != nil {
  24. return
  25. }
  26. if eggFriendCircleBasic.PublishIsRealName == 1 && user.IsRealName == 0 {
  27. err = errors.New("当前未实名~")
  28. return
  29. }
  30. //2、检测是否在黑名单中
  31. eggFriendCircleUserBlackListDb := implement.NewEggFriendCircleUserBlackListDb(db.Db)
  32. eggFriendCircleUserBlackList, err := eggFriendCircleUserBlackListDb.EggFriendCircleUserBlackListGet(user.Id)
  33. if err != nil {
  34. return
  35. }
  36. if eggFriendCircleUserBlackList != nil {
  37. err = errors.New("已被拉入黑名单~")
  38. return
  39. }
  40. //3、检测是否已达次数
  41. now := time.Now()
  42. startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Format("2006-01-02 15:04:05")
  43. endOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Add(24 * time.Hour).Format("2006-01-02 15:04:05")
  44. query := elastic.NewBoolQuery().
  45. Must(
  46. elastic.NewTermQuery("uid", user.Id),
  47. elastic.NewRangeQuery("created_at").Gte(startOfDay).Lt(endOfDay), // 根据你的索引结构调整字段名
  48. )
  49. searchResult, err := es.EsClient.Search().Index(md.EggFriendCircleEsIndex).
  50. Query(query).
  51. Size(0). // 只关心总数,不实际返回文档
  52. Do(context.Background())
  53. if err != nil {
  54. return
  55. }
  56. total := searchResult.TotalHits()
  57. if int(total) >= eggFriendCircleBasic.PublishNumsEveryDay {
  58. err = errors.New("今日朋友圈发布数量已达上限~")
  59. return
  60. }
  61. return
  62. }
  63. func MySelfList(c *gin.Context, req friend_circles.MySelfListReq) (resp friend_circles.MySelfListResp, err error) {
  64. user := svc.GetUser(c)
  65. //获取当前用户在im中的user_id
  66. var friends []*model.Friend
  67. err = db.DbIm.Where("`phone_number`=?", user.Phone).Limit(5000).Find(&friends)
  68. if err != nil {
  69. return
  70. }
  71. var friendIDs []interface{}
  72. for _, v := range friends {
  73. friendIDs = append(friendIDs, v.FriendId)
  74. }
  75. // 分页参数
  76. from := (req.Page - 1) * req.PageSize
  77. // 构建查询
  78. query := elastic.NewBoolQuery()
  79. query.Must(elastic.NewTermsQuery("im_uid", friendIDs...))
  80. query.Must(elastic.NewTermQuery("state", "1"))
  81. searchResult, err := es.EsClient.Search().
  82. Index(md.EggFriendCircleEsIndex). // 替换为你的索引名称
  83. Query(query).
  84. Sort("create_at", false). // 按时间倒排
  85. From(from).
  86. Size(req.PageSize).
  87. Pretty(true).
  88. Do(context.Background())
  89. if err != nil {
  90. logx.Fatalf("Error searching for documents: %v", err)
  91. return
  92. }
  93. // 检查是否有结果
  94. if searchResult.Hits.TotalHits.Value == 0 {
  95. return
  96. }
  97. // 解析结果
  98. for _, hit := range searchResult.Hits.Hits {
  99. var doc md.EggFriendCircleEs
  100. err = json.Unmarshal(hit.Source, &doc)
  101. if err != nil {
  102. return
  103. }
  104. resp.List = append(resp.List, doc)
  105. }
  106. resp.Total = searchResult.TotalHits()
  107. return
  108. }
  109. func CommentList(circleIndexId string) (resp friend_circles.CommentListResp, err error) {
  110. // 查询所有评论
  111. query := elastic.NewBoolQuery()
  112. query.Must(elastic.NewTermQuery("circle_id", circleIndexId))
  113. query.Must(elastic.NewTermQuery("state", "1"))
  114. searchResult, err := es.EsClient.Search().
  115. Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称
  116. Query(query).
  117. Sort("create_at", false). // 按时间倒排
  118. Pretty(true).
  119. Do(context.Background())
  120. if err != nil {
  121. logx.Fatalf("Error searching for documents: %v", err)
  122. return
  123. }
  124. // 检查是否有结果
  125. if searchResult.Hits.TotalHits.Value == 0 {
  126. return
  127. }
  128. // 解析结果
  129. for _, hit := range searchResult.Hits.Hits {
  130. var doc friend_circles.EggFriendCircleCommentEsStruct
  131. err = json.Unmarshal(hit.Source, &doc)
  132. if err != nil {
  133. return
  134. }
  135. user, err1 := svc.GetImUser(doc.ImUid, "")
  136. if err1 != nil {
  137. return friend_circles.CommentListResp{}, err1
  138. }
  139. doc.NickName = user.Nickname
  140. doc.AvatarUrl = user.AvatarUrl
  141. resp.List = append(resp.List, doc)
  142. }
  143. resp.Total = searchResult.TotalHits()
  144. return
  145. }
  146. func CommentDetail(commentIndexId string) (resp friend_circles.CommentDetailResp, err error) {
  147. // 查询所有评论
  148. query := elastic.NewBoolQuery()
  149. query.Must(elastic.NewTermQuery("comment_id", commentIndexId))
  150. query.Must(elastic.NewTermQuery("state", "1"))
  151. searchResult, err := es.EsClient.Search().
  152. Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称
  153. Query(query).
  154. Sort("create_at", false). // 按时间倒排
  155. Pretty(true).
  156. Do(context.Background())
  157. if err != nil {
  158. logx.Fatalf("Error searching for documents: %v", err)
  159. return
  160. }
  161. // 检查是否有结果
  162. if searchResult.Hits.TotalHits.Value == 0 {
  163. return
  164. }
  165. // 解析结果
  166. for _, hit := range searchResult.Hits.Hits {
  167. var doc friend_circles.EggFriendCircleCommentEsStruct
  168. err = json.Unmarshal(hit.Source, &doc)
  169. if err != nil {
  170. return
  171. }
  172. user, err1 := svc.GetImUser(doc.ImUid, "")
  173. if err1 != nil {
  174. return friend_circles.CommentDetailResp{}, err1
  175. }
  176. doc.NickName = user.Nickname
  177. doc.AvatarUrl = user.AvatarUrl
  178. resp.List = append(resp.List, doc)
  179. }
  180. resp.Total = searchResult.TotalHits()
  181. return
  182. }