user.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package v1
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. pb "go-common/app/interface/live/web-ucenter/api/http"
  6. "go-common/app/interface/live/web-ucenter/conf"
  7. "go-common/app/interface/live/web-ucenter/dao/user"
  8. "go-common/app/interface/live/web-ucenter/model"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. "go-common/library/sync/errgroup"
  12. "strings"
  13. )
  14. // UserService user service
  15. type UserService struct {
  16. c *conf.Config
  17. dao *user.Dao
  18. }
  19. // NewUserService new user service
  20. func NewUserService(c *conf.Config) (s *UserService) {
  21. s = &UserService{
  22. c: c,
  23. dao: user.New(c),
  24. }
  25. return s
  26. }
  27. // GetUserInfo implementation
  28. // 根据uid查询用户信息
  29. // `midware:"auth"`,需要登录态
  30. func (s *UserService) GetUserInfo(ctx context.Context, req *pb.GetInfoReq) (resp *pb.GetInfoResp, err error) {
  31. var (
  32. group, errCtx = errgroup.WithContext(ctx)
  33. userExp int64
  34. userRank string
  35. )
  36. // check login
  37. uid := metadata.Int64(ctx, metadata.Mid)
  38. if uid == 0 {
  39. err = errors.Wrap(err, "请先登录")
  40. return
  41. }
  42. platform := checkPlatform(req.Platform)
  43. resp = &pb.GetInfoResp{
  44. Uid: uid,
  45. UserCharged: 0,
  46. }
  47. // 并行获取account / xuser.vip / xuser.exp / wallet / rc / rankdb
  48. func() {
  49. // account.ProfileWithStat3
  50. group.Go(func() (err error) {
  51. profile, err := s.dao.GetAccountProfile(errCtx, uid)
  52. if err != nil {
  53. log.Error("[service.v1.user|GetUserInfo] GetAccountProfile error(%v), uid(%d)", err, uid)
  54. return nil
  55. }
  56. resp.Uname = profile.Name
  57. resp.Face = strings.Replace(profile.Face, "http://", "https://", 1)
  58. resp.Coin = profile.Coins
  59. return
  60. })
  61. // wallet
  62. group.Go(func() (err error) {
  63. silver, gold, err := s.dao.GetWallet(errCtx, uid, platform)
  64. if err != nil {
  65. log.Error("[service.v1.user|GetUserInfo] GetWallet error(%v), uid(%d)", err, uid)
  66. return nil
  67. }
  68. resp.Silver = silver
  69. resp.Gold = gold
  70. return
  71. })
  72. // xuser.vip
  73. group.Go(func() (err error) {
  74. vipInfo, err := s.dao.GetLiveVip(errCtx, uid)
  75. if err != nil || vipInfo == nil || vipInfo.Info == nil {
  76. log.Error("[service.v1.user|GetUserInfo] GetLiveVip error(%v), uid(%d)", err, uid)
  77. return nil
  78. }
  79. resp.Vip = vipInfo.Info.Vip
  80. resp.Svip = vipInfo.Info.Svip
  81. return
  82. })
  83. // xuser.exp
  84. group.Go(func() (err error) {
  85. expInfo, err := s.dao.GetLiveExp(errCtx, uid)
  86. if err != nil || expInfo == nil || expInfo.UserLevel == nil {
  87. log.Error("[service.v1.user|GetUserInfo] GetLiveExp error(%v), uid(%d)", err, uid)
  88. return nil
  89. }
  90. userExp = expInfo.UserLevel.UserExp
  91. resp.UserLevel = expInfo.UserLevel.Level
  92. resp.UserNextLevel = expInfo.UserLevel.NextLevel
  93. resp.UserIntimacy = expInfo.UserLevel.UserExp - expInfo.UserLevel.UserExpLeft
  94. resp.UserNextIntimacy = expInfo.UserLevel.UserExpNextLevel
  95. resp.IsLevelTop = expInfo.UserLevel.IsLevelTop
  96. return
  97. })
  98. // rc
  99. group.Go(func() (err error) {
  100. achieve, err := s.dao.GetLiveAchieve(errCtx, uid)
  101. if err != nil {
  102. log.Error("[service.v1.user|GetUserInfo] GetLiveAchieve error(%v), uid(%d)", err, uid)
  103. return nil
  104. }
  105. resp.Achieve = achieve
  106. return
  107. })
  108. // rankdb
  109. group.Go(func() (err error) {
  110. if userRank, err = s.dao.GetLiveRank(errCtx, uid); err != nil {
  111. log.Error("[service.v1.user|GetUserInfo] GetLiveRank error(%v), uid(%d)", err, uid)
  112. return nil
  113. }
  114. return
  115. })
  116. }()
  117. group.Wait()
  118. // 根据exp & rankdb 判断组装返回的user_level_rank字段
  119. if userExp < 120000000 {
  120. resp.UserLevelRank = ">50000"
  121. } else {
  122. resp.UserLevelRank = userRank
  123. }
  124. log.Info("GetUserInfo.resp(%v)", resp)
  125. return
  126. }
  127. func checkPlatform(p string) string {
  128. if p == "" || (p != model.PlatformIos && p != model.PlatformAndroid) {
  129. return model.PlatformPc
  130. }
  131. return p
  132. }