golang-im聊天
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

178 righe
4.9 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 || friend.Status == friend2.FriendStatusApply {
  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. utils.FilePutContents("sign_in", utils.SerializeStr(map[string]interface{}{
  91. "args": req,
  92. }))
  93. isNew, userId, token, masterId, err := app.AuthApp.SignIn(ctx, req.PhoneNumber, req.Code, req.MasterId, req.DeviceId, req.PushAlia, req.Nickname, req.AvatarUrl)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return &pb.SignInResp{
  98. IsNew: isNew,
  99. UserId: userId,
  100. Token: token,
  101. MasterId: masterId,
  102. }, nil
  103. }
  104. func (s *BusinessExtServer) GetUser(ctx context.Context, req *pb.GetUserReq) (*pb.GetUserResp, error) {
  105. var user *pb.User
  106. var err error
  107. if req.Phone != "" {
  108. masterId, err := grpclib.GetCtxMasterId(ctx)
  109. if err != nil {
  110. return nil, err
  111. }
  112. useInfo, err := repo.UserRepo.GetByPhoneNumber(req.Phone, utils.StrToInt64(masterId))
  113. user = &pb.User{
  114. UserId: useInfo.Id,
  115. Nickname: useInfo.Nickname,
  116. Sex: useInfo.Sex,
  117. AvatarUrl: useInfo.AvatarUrl,
  118. MasterId: useInfo.MasterId,
  119. IsAutoAddedFriends: int64(useInfo.IsAutoAddedFriends),
  120. PhoneNumber: useInfo.PhoneNumber,
  121. }
  122. } else {
  123. userId, _, err := grpclib.GetCtxData(ctx)
  124. if err != nil {
  125. return nil, err
  126. }
  127. if req.UserId != 0 {
  128. userId = req.UserId
  129. }
  130. user, err = app.UserApp.Get(ctx, userId)
  131. }
  132. return &pb.GetUserResp{User: user}, err
  133. }
  134. func (s *BusinessExtServer) UpdateUser(ctx context.Context, req *pb.UpdateUserReq) (*pb.Empty, error) {
  135. userId, _, err := grpclib.GetCtxData(ctx)
  136. if err != nil {
  137. return nil, err
  138. }
  139. return new(pb.Empty), app.UserApp.Update(ctx, userId, req)
  140. }
  141. func (s *BusinessExtServer) SearchUser(ctx context.Context, req *pb.SearchUserReq) (*pb.SearchUserResp, error) {
  142. users, err := app.UserApp.Search(ctx, req.Key, req.MasterId)
  143. return &pb.SearchUserResp{Users: users}, err
  144. }
  145. func (s *BusinessExtServer) CloudUploadFile(ctx context.Context, req *pb.CloudUploadFileReq) (*pb.CloudUploadFileResp, error) {
  146. userId, _, err := grpclib.GetCtxData(ctx)
  147. if err != nil {
  148. return nil, err
  149. }
  150. upload, err := comm.CloudUpload.FileReqUpload(ctx, strconv.FormatInt(userId, 10), req.Dir, req.FileName, utils.StrToInt64(req.FileSize))
  151. data := utils.SerializeStr(upload)
  152. var result map[string]string
  153. utils.Unserialize([]byte(data), &result)
  154. return &pb.CloudUploadFileResp{
  155. Method: result["method"],
  156. Host: result["host"],
  157. Key: result["key"],
  158. Token: result["token"],
  159. }, err
  160. }