您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

301 行
8.1 KiB

  1. package api
  2. import (
  3. "context"
  4. "egg-im/internal/business/app"
  5. comm "egg-im/internal/business/comm"
  6. "egg-im/internal/business/comm/utils"
  7. "egg-im/internal/business/domain/user/repo"
  8. friend2 "egg-im/internal/logic/domain/friend"
  9. "egg-im/internal/logic/domain/group/model"
  10. repo2 "egg-im/internal/logic/domain/group/repo"
  11. "egg-im/internal/logic/domain/message/service"
  12. "egg-im/pkg/grpclib"
  13. "egg-im/pkg/pb"
  14. "encoding/json"
  15. "errors"
  16. "strconv"
  17. "time"
  18. )
  19. type BusinessExtServer struct{}
  20. func (s *BusinessExtServer) ViewGroupNotice(ctx context.Context, req *pb.ViewGroupNoticeReq) (*pb.ViewGroupNoticeResp, error) {
  21. userId, _, err := grpclib.GetCtxData(ctx)
  22. if err != nil {
  23. return nil, err
  24. }
  25. groupNotice, err := repo2.GroupNoticeDao.Get(req.GroupId)
  26. if err != nil {
  27. return nil, err
  28. }
  29. if groupNotice == nil {
  30. return &pb.ViewGroupNoticeResp{
  31. GroupId: 0,
  32. UserId: 0,
  33. Content: "",
  34. LikeNums: 0,
  35. ReadNums: 0,
  36. PublishType: 0,
  37. PublishTime: "",
  38. IsLike: false,
  39. }, nil
  40. }
  41. groupNotice.ReadNums++
  42. err = repo2.GroupNoticeDao.Save(groupNotice, false)
  43. if err != nil {
  44. return nil, err
  45. }
  46. groupNoticeWithLikeRecords, err := repo2.GroupNoticeWithLikeRecordsDao.Get(userId, req.GroupId)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &pb.ViewGroupNoticeResp{
  51. GroupId: groupNotice.GroupId,
  52. UserId: groupNotice.UserId,
  53. Content: groupNotice.Content,
  54. LikeNums: int64(groupNotice.LikeNums),
  55. ReadNums: int64(groupNotice.ReadNums),
  56. PublishType: pb.GroupNoticePublishType(groupNotice.PublishType),
  57. PublishTime: groupNotice.UpdateTime.Format("2006-01-02 15:04:05"),
  58. IsLike: func() bool {
  59. if groupNoticeWithLikeRecords == nil {
  60. return false
  61. }
  62. return true
  63. }(),
  64. }, nil
  65. }
  66. func (s *BusinessExtServer) LikeGroupNotice(ctx context.Context, req *pb.LikeGroupNoticeReq) (*pb.Empty, error) {
  67. userId, _, err := grpclib.GetCtxData(ctx)
  68. if err != nil {
  69. return nil, err
  70. }
  71. groupNoticeWithLikeRecords, err := repo2.GroupNoticeWithLikeRecordsDao.Get(userId, req.GroupId)
  72. if err != nil {
  73. return nil, err
  74. }
  75. if groupNoticeWithLikeRecords != nil {
  76. return nil, errors.New("请勿重复点赞!")
  77. }
  78. groupNotice, err := repo2.GroupNoticeDao.Get(req.GroupId)
  79. if err != nil {
  80. return nil, err
  81. }
  82. if groupNotice == nil {
  83. return nil, errors.New("群公告记录不存在!")
  84. }
  85. err = repo2.GroupNoticeWithLikeRecordsDao.Save(&model.GroupNoticeWithLikeRecords{
  86. GroupId: req.GroupId,
  87. UserId: userId,
  88. })
  89. if err != nil {
  90. return nil, err
  91. }
  92. groupNotice.LikeNums++
  93. return new(pb.Empty), repo2.GroupNoticeDao.Save(groupNotice, false)
  94. }
  95. func (s *BusinessExtServer) CancelLikeGroupNotice(ctx context.Context, req *pb.CancelLikeGroupNoticeReq) (*pb.Empty, error) {
  96. userId, _, err := grpclib.GetCtxData(ctx)
  97. if err != nil {
  98. return nil, err
  99. }
  100. groupNoticeWithLikeRecords, err := repo2.GroupNoticeWithLikeRecordsDao.Get(userId, req.GroupId)
  101. if err != nil {
  102. return nil, err
  103. }
  104. if groupNoticeWithLikeRecords == nil {
  105. return nil, errors.New("不存在的点赞记录!")
  106. }
  107. groupNotice, err := repo2.GroupNoticeDao.Get(req.GroupId)
  108. if err != nil {
  109. return nil, err
  110. }
  111. if groupNotice == nil {
  112. return nil, errors.New("群公告记录不存在!")
  113. }
  114. err = repo2.GroupNoticeWithLikeRecordsDao.Delete(userId, req.GroupId)
  115. if err != nil {
  116. return nil, err
  117. }
  118. groupNotice.LikeNums--
  119. return new(pb.Empty), repo2.GroupNoticeDao.Save(groupNotice, false)
  120. }
  121. func (s *BusinessExtServer) PublishGroupNotice(ctx context.Context, req *pb.PublishGroupNoticeReq) (*pb.Empty, error) {
  122. now := time.Now()
  123. userId, _, err := grpclib.GetCtxData(ctx)
  124. if err != nil {
  125. return nil, err
  126. }
  127. //1、判断是否可以进行发布群公告
  128. groupUser, err := repo2.GroupUserRepo.Get(req.GroupId, userId)
  129. if groupUser.MemberType == int(pb.MemberType_GMT_MEMBER) {
  130. return nil, errors.New("普通成员不能发布群公告")
  131. }
  132. groupComplain := model.GroupNotice{
  133. GroupId: req.GroupId,
  134. UserId: userId,
  135. PublishType: int32(req.PublishType),
  136. Content: req.Content,
  137. LikeNums: 0,
  138. ReadNums: 0,
  139. CreateTime: now,
  140. UpdateTime: now,
  141. }
  142. return new(pb.Empty), repo2.GroupNoticeDao.Save(&groupComplain, true)
  143. }
  144. func (s *BusinessExtServer) ComplainGroup(ctx context.Context, req *pb.ComplainGroupReq) (*pb.Empty, error) {
  145. now := time.Now()
  146. userId, _, err := grpclib.GetCtxData(ctx)
  147. if err != nil {
  148. return nil, err
  149. }
  150. imageList, _ := json.Marshal(req.ImageList)
  151. groupComplain := model.GroupComplain{
  152. GroupId: req.GroupId,
  153. UserId: userId,
  154. ComplainType: int(req.ComplainType),
  155. Text: req.Text,
  156. ImageList: string(imageList),
  157. Status: 0,
  158. CreateTime: now,
  159. UpdateTime: now,
  160. }
  161. return new(pb.Empty), repo2.GroupComplainDao.Save(&groupComplain)
  162. }
  163. func (s *BusinessExtServer) IsFriends(ctx context.Context, req *pb.IsFriendsReq) (*pb.IsFriendsResp, error) {
  164. user, err := repo.UserRepo.GetByPhoneNumber(req.UserPhone)
  165. if err != nil {
  166. return nil, err
  167. }
  168. if user == nil {
  169. return nil, errors.New("未查询到自身用户信息")
  170. }
  171. userFriend, err := repo.UserRepo.GetByPhoneNumber(req.FriendPhone)
  172. if err != nil {
  173. return nil, err
  174. }
  175. if userFriend == nil {
  176. return nil, errors.New("未查询到好友用户信息")
  177. }
  178. friend, err := friend2.FriendRepo.Get(user.Id, userFriend.Id)
  179. if err != nil {
  180. return nil, err
  181. }
  182. var isFriend = int64(1)
  183. if friend == nil || friend.Status == friend2.FriendStatusApply {
  184. isFriend = 2
  185. }
  186. return &pb.IsFriendsResp{
  187. User: &pb.User{
  188. UserId: userFriend.Id,
  189. Nickname: userFriend.Nickname,
  190. Sex: userFriend.Sex,
  191. AvatarUrl: userFriend.AvatarUrl,
  192. IsAutoAddedFriends: int64(userFriend.IsAutoAddedFriends),
  193. },
  194. IsFriend: isFriend,
  195. }, err
  196. }
  197. func (s *BusinessExtServer) EmoticonList(ctx context.Context, empty *pb.Empty) (*pb.EmoticonListResp, error) {
  198. list, err := service.EmoticonList()
  199. if err != nil {
  200. return nil, err
  201. }
  202. return &pb.EmoticonListResp{Emoticons: list}, err
  203. }
  204. func (s *BusinessExtServer) SignIn(ctx context.Context, req *pb.SignInReq) (*pb.SignInResp, error) {
  205. utils.FilePutContents("sign_in", utils.SerializeStr(map[string]interface{}{
  206. "args": req,
  207. }))
  208. isNew, userId, token, err := app.AuthApp.SignIn(ctx, req.PhoneNumber, req.Code, req.DeviceId, req.PushAlia, req.Nickname, req.AvatarUrl)
  209. if err != nil {
  210. return nil, err
  211. }
  212. return &pb.SignInResp{
  213. IsNew: isNew,
  214. UserId: userId,
  215. Token: token,
  216. }, nil
  217. }
  218. func (s *BusinessExtServer) GetUser(ctx context.Context, req *pb.GetUserReq) (*pb.GetUserResp, error) {
  219. var user *pb.User
  220. if req.Phone != "" {
  221. useInfo, err := repo.UserRepo.GetByPhoneNumber(req.Phone)
  222. user = &pb.User{
  223. UserId: useInfo.Id,
  224. Nickname: useInfo.Nickname,
  225. Sex: useInfo.Sex,
  226. AvatarUrl: useInfo.AvatarUrl,
  227. IsAutoAddedFriends: int64(useInfo.IsAutoAddedFriends),
  228. PhoneNumber: useInfo.PhoneNumber,
  229. }
  230. if err != nil {
  231. return nil, err
  232. }
  233. } else {
  234. userId, _, err := grpclib.GetCtxData(ctx)
  235. if err != nil {
  236. return nil, err
  237. }
  238. if req.UserId != 0 {
  239. userId = req.UserId
  240. }
  241. user, err = app.UserApp.Get(ctx, userId)
  242. }
  243. return &pb.GetUserResp{User: user}, nil
  244. }
  245. func (s *BusinessExtServer) UpdateUser(ctx context.Context, req *pb.UpdateUserReq) (*pb.Empty, error) {
  246. userId, _, err := grpclib.GetCtxData(ctx)
  247. if err != nil {
  248. return nil, err
  249. }
  250. return new(pb.Empty), app.UserApp.Update(ctx, userId, req)
  251. }
  252. func (s *BusinessExtServer) SearchUser(ctx context.Context, req *pb.SearchUserReq) (*pb.SearchUserResp, error) {
  253. users, err := app.UserApp.Search(ctx, req.Key)
  254. return &pb.SearchUserResp{Users: users}, err
  255. }
  256. func (s *BusinessExtServer) CloudUploadFile(ctx context.Context, req *pb.CloudUploadFileReq) (*pb.CloudUploadFileResp, error) {
  257. userId, _, err := grpclib.GetCtxData(ctx)
  258. if err != nil {
  259. return nil, err
  260. }
  261. upload, err := comm.CloudUpload.FileReqUpload(ctx, strconv.FormatInt(userId, 10), req.Dir, req.FileName, utils.StrToInt64(req.FileSize))
  262. data := utils.SerializeStr(upload)
  263. var result map[string]string
  264. utils.Unserialize([]byte(data), &result)
  265. return &pb.CloudUploadFileResp{
  266. Method: result["method"],
  267. Host: result["host"],
  268. Key: result["key"],
  269. Token: result["token"],
  270. }, err
  271. }