golang-im聊天
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

174 рядки
4.7 KiB

  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "gim/internal/business/app"
  7. comm "gim/internal/business/comm"
  8. "gim/internal/business/comm/utils"
  9. "gim/internal/business/domain/user/repo"
  10. friend2 "gim/internal/logic/domain/friend"
  11. "gim/internal/logic/domain/group/model"
  12. repo2 "gim/internal/logic/domain/group/repo"
  13. "gim/internal/logic/domain/message/service"
  14. "gim/pkg/grpclib"
  15. "gim/pkg/pb"
  16. "strconv"
  17. "time"
  18. )
  19. type BusinessExtServer struct{}
  20. func (s *BusinessExtServer) ComplainGroup(ctx context.Context, req *pb.ComplainGroupReq) (*pb.Empty, error) {
  21. now := time.Now()
  22. userId, _, err := grpclib.GetCtxData(ctx)
  23. if err != nil {
  24. return nil, err
  25. }
  26. imageList, _ := json.Marshal(req.ImageList)
  27. groupComplain := model.GroupComplain{
  28. GroupId: req.GroupId,
  29. UserId: userId,
  30. ComplainType: int(req.ComplainType),
  31. Text: req.Text,
  32. ImageList: string(imageList),
  33. Status: 0,
  34. CreateTime: now,
  35. UpdateTime: now,
  36. }
  37. return new(pb.Empty), repo2.GroupComplainDao.Save(&groupComplain)
  38. }
  39. func (s *BusinessExtServer) IsFriends(ctx context.Context, req *pb.IsFriendsReq) (*pb.IsFriendsResp, error) {
  40. masterId, err := grpclib.GetCtxMasterId(ctx)
  41. if err != nil {
  42. return nil, err
  43. }
  44. user, err := repo.UserRepo.GetByPhoneNumber(req.UserPhone, utils.StrToInt64(masterId))
  45. if err != nil {
  46. return nil, err
  47. }
  48. if user == nil {
  49. return nil, errors.New("未查询到自身用户信息")
  50. }
  51. userFriend, err := repo.UserRepo.GetByPhoneNumber(req.FriendPhone, utils.StrToInt64(masterId))
  52. if err != nil {
  53. return nil, err
  54. }
  55. if userFriend == nil {
  56. return nil, errors.New("未查询到好友用户信息")
  57. }
  58. friend, err := friend2.FriendRepo.Get(user.Id, userFriend.Id)
  59. if err != nil {
  60. return nil, err
  61. }
  62. var isFriend = int64(1)
  63. if friend == nil {
  64. isFriend = 2
  65. }
  66. return &pb.IsFriendsResp{
  67. User: &pb.User{
  68. UserId: userFriend.Id,
  69. Nickname: userFriend.Nickname,
  70. Sex: userFriend.Sex,
  71. AvatarUrl: userFriend.AvatarUrl,
  72. MasterId: userFriend.MasterId,
  73. IsAutoAddedFriends: int64(userFriend.IsAutoAddedFriends),
  74. },
  75. IsFriend: isFriend,
  76. }, err
  77. }
  78. func (s *BusinessExtServer) EmoticonList(ctx context.Context, empty *pb.Empty) (*pb.EmoticonListResp, error) {
  79. masterId, err := grpclib.GetCtxMasterId(ctx)
  80. if err != nil {
  81. return nil, err
  82. }
  83. list, err := service.EmoticonList(masterId)
  84. if err != nil {
  85. return nil, err
  86. }
  87. return &pb.EmoticonListResp{Emoticons: list}, err
  88. }
  89. func (s *BusinessExtServer) SignIn(ctx context.Context, req *pb.SignInReq) (*pb.SignInResp, error) {
  90. isNew, userId, token, masterId, err := app.AuthApp.SignIn(ctx, req.PhoneNumber, req.Code, req.MasterId, req.DeviceId, req.PushAlia)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return &pb.SignInResp{
  95. IsNew: isNew,
  96. UserId: userId,
  97. Token: token,
  98. MasterId: masterId,
  99. }, nil
  100. }
  101. func (s *BusinessExtServer) GetUser(ctx context.Context, req *pb.GetUserReq) (*pb.GetUserResp, error) {
  102. var user *pb.User
  103. var err error
  104. if req.Phone != "" {
  105. masterId, err := grpclib.GetCtxMasterId(ctx)
  106. if err != nil {
  107. return nil, err
  108. }
  109. useInfo, err := repo.UserRepo.GetByPhoneNumber(req.Phone, utils.StrToInt64(masterId))
  110. user = &pb.User{
  111. UserId: useInfo.Id,
  112. Nickname: useInfo.Nickname,
  113. Sex: useInfo.Sex,
  114. AvatarUrl: useInfo.AvatarUrl,
  115. MasterId: useInfo.MasterId,
  116. IsAutoAddedFriends: int64(useInfo.IsAutoAddedFriends),
  117. }
  118. } else {
  119. userId, _, err := grpclib.GetCtxData(ctx)
  120. if err != nil {
  121. return nil, err
  122. }
  123. if req.UserId != 0 {
  124. userId = req.UserId
  125. }
  126. user, err = app.UserApp.Get(ctx, userId)
  127. }
  128. return &pb.GetUserResp{User: user}, err
  129. }
  130. func (s *BusinessExtServer) UpdateUser(ctx context.Context, req *pb.UpdateUserReq) (*pb.Empty, error) {
  131. userId, _, err := grpclib.GetCtxData(ctx)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return new(pb.Empty), app.UserApp.Update(ctx, userId, req)
  136. }
  137. func (s *BusinessExtServer) SearchUser(ctx context.Context, req *pb.SearchUserReq) (*pb.SearchUserResp, error) {
  138. users, err := app.UserApp.Search(ctx, req.Key, req.MasterId)
  139. return &pb.SearchUserResp{Users: users}, err
  140. }
  141. func (s *BusinessExtServer) CloudUploadFile(ctx context.Context, req *pb.CloudUploadFileReq) (*pb.CloudUploadFileResp, error) {
  142. userId, _, err := grpclib.GetCtxData(ctx)
  143. if err != nil {
  144. return nil, err
  145. }
  146. upload, err := comm.CloudUpload.FileReqUpload(ctx, strconv.FormatInt(userId, 10), req.Dir, req.FileName, utils.StrToInt64(req.FileSize))
  147. data := utils.SerializeStr(upload)
  148. var result map[string]string
  149. utils.Unserialize([]byte(data), &result)
  150. return &pb.CloudUploadFileResp{
  151. Method: result["method"],
  152. Host: result["host"],
  153. Key: result["key"],
  154. Token: result["token"],
  155. }, err
  156. }