golang-im聊天
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

177 行
4.8 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. 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. }
  121. } else {
  122. userId, _, err := grpclib.GetCtxData(ctx)
  123. if err != nil {
  124. return nil, err
  125. }
  126. if req.UserId != 0 {
  127. userId = req.UserId
  128. }
  129. user, err = app.UserApp.Get(ctx, userId)
  130. }
  131. return &pb.GetUserResp{User: user}, err
  132. }
  133. func (s *BusinessExtServer) UpdateUser(ctx context.Context, req *pb.UpdateUserReq) (*pb.Empty, error) {
  134. userId, _, err := grpclib.GetCtxData(ctx)
  135. if err != nil {
  136. return nil, err
  137. }
  138. return new(pb.Empty), app.UserApp.Update(ctx, userId, req)
  139. }
  140. func (s *BusinessExtServer) SearchUser(ctx context.Context, req *pb.SearchUserReq) (*pb.SearchUserResp, error) {
  141. users, err := app.UserApp.Search(ctx, req.Key, req.MasterId)
  142. return &pb.SearchUserResp{Users: users}, err
  143. }
  144. func (s *BusinessExtServer) CloudUploadFile(ctx context.Context, req *pb.CloudUploadFileReq) (*pb.CloudUploadFileResp, error) {
  145. userId, _, err := grpclib.GetCtxData(ctx)
  146. if err != nil {
  147. return nil, err
  148. }
  149. upload, err := comm.CloudUpload.FileReqUpload(ctx, strconv.FormatInt(userId, 10), req.Dir, req.FileName, utils.StrToInt64(req.FileSize))
  150. data := utils.SerializeStr(upload)
  151. var result map[string]string
  152. utils.Unserialize([]byte(data), &result)
  153. return &pb.CloudUploadFileResp{
  154. Method: result["method"],
  155. Host: result["host"],
  156. Key: result["key"],
  157. Token: result["token"],
  158. }, err
  159. }