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

165 行
4.5 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) 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. user, err := repo.UserRepo.GetByPhoneNumber(req.UserPhone)
  41. if err != nil {
  42. return nil, err
  43. }
  44. if user == nil {
  45. return nil, errors.New("未查询到自身用户信息")
  46. }
  47. userFriend, err := repo.UserRepo.GetByPhoneNumber(req.FriendPhone)
  48. if err != nil {
  49. return nil, err
  50. }
  51. if userFriend == nil {
  52. return nil, errors.New("未查询到好友用户信息")
  53. }
  54. friend, err := friend2.FriendRepo.Get(user.Id, userFriend.Id)
  55. if err != nil {
  56. return nil, err
  57. }
  58. var isFriend = int64(1)
  59. if friend == nil || friend.Status == friend2.FriendStatusApply {
  60. isFriend = 2
  61. }
  62. return &pb.IsFriendsResp{
  63. User: &pb.User{
  64. UserId: userFriend.Id,
  65. Nickname: userFriend.Nickname,
  66. Sex: userFriend.Sex,
  67. AvatarUrl: userFriend.AvatarUrl,
  68. IsAutoAddedFriends: int64(userFriend.IsAutoAddedFriends),
  69. },
  70. IsFriend: isFriend,
  71. }, err
  72. }
  73. func (s *BusinessExtServer) EmoticonList(ctx context.Context, empty *pb.Empty) (*pb.EmoticonListResp, error) {
  74. list, err := service.EmoticonList()
  75. if err != nil {
  76. return nil, err
  77. }
  78. return &pb.EmoticonListResp{Emoticons: list}, err
  79. }
  80. func (s *BusinessExtServer) SignIn(ctx context.Context, req *pb.SignInReq) (*pb.SignInResp, error) {
  81. utils.FilePutContents("sign_in", utils.SerializeStr(map[string]interface{}{
  82. "args": req,
  83. }))
  84. isNew, userId, token, err := app.AuthApp.SignIn(ctx, req.PhoneNumber, req.Code, req.DeviceId, req.PushAlia, req.Nickname, req.AvatarUrl)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return &pb.SignInResp{
  89. IsNew: isNew,
  90. UserId: userId,
  91. Token: token,
  92. }, nil
  93. }
  94. func (s *BusinessExtServer) GetUser(ctx context.Context, req *pb.GetUserReq) (*pb.GetUserResp, error) {
  95. var user *pb.User
  96. if req.Phone != "" {
  97. useInfo, err := repo.UserRepo.GetByPhoneNumber(req.Phone)
  98. user = &pb.User{
  99. UserId: useInfo.Id,
  100. Nickname: useInfo.Nickname,
  101. Sex: useInfo.Sex,
  102. AvatarUrl: useInfo.AvatarUrl,
  103. IsAutoAddedFriends: int64(useInfo.IsAutoAddedFriends),
  104. PhoneNumber: useInfo.PhoneNumber,
  105. }
  106. if err != nil {
  107. return nil, err
  108. }
  109. } else {
  110. userId, _, err := grpclib.GetCtxData(ctx)
  111. if err != nil {
  112. return nil, err
  113. }
  114. if req.UserId != 0 {
  115. userId = req.UserId
  116. }
  117. user, err = app.UserApp.Get(ctx, userId)
  118. }
  119. return &pb.GetUserResp{User: user}, nil
  120. }
  121. func (s *BusinessExtServer) UpdateUser(ctx context.Context, req *pb.UpdateUserReq) (*pb.Empty, error) {
  122. userId, _, err := grpclib.GetCtxData(ctx)
  123. if err != nil {
  124. return nil, err
  125. }
  126. return new(pb.Empty), app.UserApp.Update(ctx, userId, req)
  127. }
  128. func (s *BusinessExtServer) SearchUser(ctx context.Context, req *pb.SearchUserReq) (*pb.SearchUserResp, error) {
  129. users, err := app.UserApp.Search(ctx, req.Key)
  130. return &pb.SearchUserResp{Users: users}, err
  131. }
  132. func (s *BusinessExtServer) CloudUploadFile(ctx context.Context, req *pb.CloudUploadFileReq) (*pb.CloudUploadFileResp, error) {
  133. userId, _, err := grpclib.GetCtxData(ctx)
  134. if err != nil {
  135. return nil, err
  136. }
  137. upload, err := comm.CloudUpload.FileReqUpload(ctx, strconv.FormatInt(userId, 10), req.Dir, req.FileName, utils.StrToInt64(req.FileSize))
  138. data := utils.SerializeStr(upload)
  139. var result map[string]string
  140. utils.Unserialize([]byte(data), &result)
  141. return &pb.CloudUploadFileResp{
  142. Method: result["method"],
  143. Host: result["host"],
  144. Key: result["key"],
  145. Token: result["token"],
  146. }, err
  147. }