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

219 lines
5.8 KiB

  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(req friend_circles.CommentListReq) (resp friend_circles.CommentListResp, err error) {
  110. // 分页参数
  111. from := (req.Page - 1) * req.PageSize
  112. // 查询所有评论
  113. query := elastic.NewBoolQuery()
  114. query.Must(elastic.NewTermQuery("circle_id", req.CircleIndexId))
  115. query.Must(elastic.NewTermQuery("state", "1"))
  116. searchResult, err := es.EsClient.Search().
  117. Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称
  118. Query(query).
  119. Sort("create_at", false). // 按时间倒排
  120. From(from).
  121. Size(req.PageSize).
  122. Pretty(true).
  123. Do(context.Background())
  124. if err != nil {
  125. logx.Fatalf("Error searching for documents: %v", err)
  126. return
  127. }
  128. // 检查是否有结果
  129. if searchResult.Hits.TotalHits.Value == 0 {
  130. return
  131. }
  132. // 解析结果
  133. for _, hit := range searchResult.Hits.Hits {
  134. var doc friend_circles.EggFriendCircleCommentEsStruct
  135. err = json.Unmarshal(hit.Source, &doc)
  136. if err != nil {
  137. return
  138. }
  139. user, err1 := svc.GetImUser(doc.ImUid, "")
  140. if err1 != nil {
  141. return friend_circles.CommentListResp{}, err1
  142. }
  143. doc.NickName = user.Nickname
  144. doc.AvatarUrl = user.AvatarUrl
  145. resp.List = append(resp.List, doc)
  146. }
  147. resp.Total = searchResult.TotalHits()
  148. return
  149. }
  150. func CommentDetail(req friend_circles.CommentDetailReq) (resp friend_circles.CommentDetailResp, err error) {
  151. // 分页参数
  152. from := (req.Page - 1) * req.PageSize
  153. // 查询所有评论
  154. query := elastic.NewBoolQuery()
  155. query.Must(elastic.NewTermQuery("comment_id", req.CommentIndexId))
  156. query.Must(elastic.NewTermQuery("state", "1"))
  157. searchResult, err := es.EsClient.Search().
  158. Index(md.EggFriendCircleCommentEsAlias). // 替换为你的索引名称
  159. Query(query).
  160. Sort("create_at", false). // 按时间倒排
  161. From(from).
  162. Size(req.PageSize).
  163. Pretty(true).
  164. Do(context.Background())
  165. if err != nil {
  166. logx.Fatalf("Error searching for documents: %v", err)
  167. return
  168. }
  169. // 检查是否有结果
  170. if searchResult.Hits.TotalHits.Value == 0 {
  171. return
  172. }
  173. // 解析结果
  174. for _, hit := range searchResult.Hits.Hits {
  175. var doc friend_circles.EggFriendCircleCommentEsStruct
  176. err = json.Unmarshal(hit.Source, &doc)
  177. if err != nil {
  178. return
  179. }
  180. user, err1 := svc.GetImUser(doc.ImUid, "")
  181. if err1 != nil {
  182. return friend_circles.CommentDetailResp{}, err1
  183. }
  184. doc.NickName = user.Nickname
  185. doc.AvatarUrl = user.AvatarUrl
  186. if doc.ReplyCommentId != "" {
  187. }
  188. resp.List = append(resp.List, doc)
  189. }
  190. resp.Total = searchResult.TotalHits()
  191. return
  192. }