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

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