golang-im聊天
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

149 rader
4.0 KiB

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