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.
 
 
 
 

107 righe
2.9 KiB

  1. package api
  2. import (
  3. "context"
  4. "gim/internal/business/app"
  5. comm "gim/internal/business/comm"
  6. "gim/internal/business/comm/utils"
  7. "gim/internal/business/domain/user/repo"
  8. "gim/internal/logic/domain/message/service"
  9. "gim/pkg/grpclib"
  10. "gim/pkg/pb"
  11. "strconv"
  12. )
  13. type BusinessExtServer struct{}
  14. func (s *BusinessExtServer) EmoticonList(ctx context.Context, empty *pb.Empty) (*pb.EmoticonListResp, error) {
  15. masterId, err := grpclib.GetCtxMasterId(ctx)
  16. if err != nil {
  17. return nil, err
  18. }
  19. list, err := service.EmoticonList(masterId)
  20. if err != nil {
  21. return nil, err
  22. }
  23. return &pb.EmoticonListResp{Emoticons: list}, err
  24. }
  25. func (s *BusinessExtServer) SignIn(ctx context.Context, req *pb.SignInReq) (*pb.SignInResp, error) {
  26. isNew, userId, token, masterId, err := app.AuthApp.SignIn(ctx, req.PhoneNumber, req.Code, req.MasterId, req.DeviceId, req.PushAlia)
  27. if err != nil {
  28. return nil, err
  29. }
  30. return &pb.SignInResp{
  31. IsNew: isNew,
  32. UserId: userId,
  33. Token: token,
  34. MasterId: masterId,
  35. }, nil
  36. }
  37. func (s *BusinessExtServer) GetUser(ctx context.Context, req *pb.GetUserReq) (*pb.GetUserResp, error) {
  38. var user *pb.User
  39. var err error
  40. if req.Phone != "" {
  41. masterId, err := grpclib.GetCtxMasterId(ctx)
  42. if err != nil {
  43. return nil, err
  44. }
  45. useInfo, err := repo.UserRepo.GetByPhoneNumber(req.Phone, utils.StrToInt64(masterId))
  46. user = &pb.User{
  47. UserId: useInfo.Id,
  48. Nickname: user.Nickname,
  49. Sex: user.Sex,
  50. AvatarUrl: user.AvatarUrl,
  51. CreateTime: user.CreateTime,
  52. UpdateTime: user.UpdateTime,
  53. MasterId: user.MasterId,
  54. IsAutoAddedFriends: user.IsAutoAddedFriends,
  55. }
  56. } else {
  57. userId, _, err := grpclib.GetCtxData(ctx)
  58. if err != nil {
  59. return nil, err
  60. }
  61. if req.UserId != 0 {
  62. userId = req.UserId
  63. }
  64. user, err = app.UserApp.Get(ctx, userId)
  65. }
  66. return &pb.GetUserResp{User: user}, err
  67. }
  68. func (s *BusinessExtServer) UpdateUser(ctx context.Context, req *pb.UpdateUserReq) (*pb.Empty, error) {
  69. userId, _, err := grpclib.GetCtxData(ctx)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return new(pb.Empty), app.UserApp.Update(ctx, userId, req)
  74. }
  75. func (s *BusinessExtServer) SearchUser(ctx context.Context, req *pb.SearchUserReq) (*pb.SearchUserResp, error) {
  76. users, err := app.UserApp.Search(ctx, req.Key, req.MasterId)
  77. return &pb.SearchUserResp{Users: users}, err
  78. }
  79. func (s *BusinessExtServer) CloudUploadFile(ctx context.Context, req *pb.CloudUploadFileReq) (*pb.CloudUploadFileResp, error) {
  80. userId, _, err := grpclib.GetCtxData(ctx)
  81. if err != nil {
  82. return nil, err
  83. }
  84. upload, err := comm.CloudUpload.FileReqUpload(ctx, strconv.FormatInt(userId, 10), req.Dir, req.FileName, utils.StrToInt64(req.FileSize))
  85. data := utils.SerializeStr(upload)
  86. var result map[string]string
  87. utils.Unserialize([]byte(data), &result)
  88. return &pb.CloudUploadFileResp{
  89. Method: result["method"],
  90. Host: result["host"],
  91. Key: result["key"],
  92. Token: result["token"],
  93. }, err
  94. }