golang-im聊天
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

105 líneas
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: useInfo.Nickname,
  49. Sex: useInfo.Sex,
  50. AvatarUrl: useInfo.AvatarUrl,
  51. MasterId: useInfo.MasterId,
  52. IsAutoAddedFriends: int64(useInfo.IsAutoAddedFriends),
  53. }
  54. } else {
  55. userId, _, err := grpclib.GetCtxData(ctx)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if req.UserId != 0 {
  60. userId = req.UserId
  61. }
  62. user, err = app.UserApp.Get(ctx, userId)
  63. }
  64. return &pb.GetUserResp{User: user}, err
  65. }
  66. func (s *BusinessExtServer) UpdateUser(ctx context.Context, req *pb.UpdateUserReq) (*pb.Empty, error) {
  67. userId, _, err := grpclib.GetCtxData(ctx)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return new(pb.Empty), app.UserApp.Update(ctx, userId, req)
  72. }
  73. func (s *BusinessExtServer) SearchUser(ctx context.Context, req *pb.SearchUserReq) (*pb.SearchUserResp, error) {
  74. users, err := app.UserApp.Search(ctx, req.Key, req.MasterId)
  75. return &pb.SearchUserResp{Users: users}, err
  76. }
  77. func (s *BusinessExtServer) CloudUploadFile(ctx context.Context, req *pb.CloudUploadFileReq) (*pb.CloudUploadFileResp, error) {
  78. userId, _, err := grpclib.GetCtxData(ctx)
  79. if err != nil {
  80. return nil, err
  81. }
  82. upload, err := comm.CloudUpload.FileReqUpload(ctx, strconv.FormatInt(userId, 10), req.Dir, req.FileName, utils.StrToInt64(req.FileSize))
  83. data := utils.SerializeStr(upload)
  84. var result map[string]string
  85. utils.Unserialize([]byte(data), &result)
  86. return &pb.CloudUploadFileResp{
  87. Method: result["method"],
  88. Host: result["host"],
  89. Key: result["key"],
  90. Token: result["token"],
  91. }, err
  92. }