dao.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package user
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. "go-common/app/interface/live/web-ucenter/conf"
  6. "go-common/app/interface/live/web-ucenter/dao"
  7. rankdbv1 "go-common/app/service/live/rankdb/api/liverpc/v1"
  8. rcv1 "go-common/app/service/live/rc/api/liverpc/v1"
  9. xuserv1 "go-common/app/service/live/xuser/api/grpc/v1"
  10. accModel "go-common/app/service/main/account/model"
  11. account "go-common/app/service/main/account/rpc/client"
  12. "go-common/library/ecode"
  13. "go-common/library/log"
  14. bm "go-common/library/net/http/blademaster"
  15. "net/http"
  16. "strconv"
  17. )
  18. var (
  19. _walletApiUrl = "/x/internal/livewallet/wallet/getAll"
  20. )
  21. // Dao user dao, wrap clients
  22. type Dao struct {
  23. c *conf.Config
  24. bmClient *bm.Client
  25. vipClient xuserv1.VipClient
  26. expClient xuserv1.UserExpClient
  27. walletUrl string
  28. accountClient *account.Service3
  29. rankdbClient rankdbv1.UserRank
  30. rcClient rcv1.AchvRPCClient
  31. }
  32. // New new user dao
  33. func New(c *conf.Config) *Dao {
  34. conn, err := xuserv1.NewClient(c.Warden)
  35. if err != nil {
  36. panic(err)
  37. }
  38. d := &Dao{
  39. c: c,
  40. bmClient: bm.NewClient(c.HTTPClient),
  41. walletUrl: c.Host.LiveRpc + _walletApiUrl,
  42. accountClient: account.New3(c.AccountRPC),
  43. rankdbClient: dao.RankdbApi.V1UserRank,
  44. rcClient: dao.RcApi.V1Achv,
  45. }
  46. d.vipClient = conn.VipClient
  47. d.expClient = conn.UserExpClient
  48. return d
  49. }
  50. // GetAccountProfile get account profile
  51. func (d *Dao) GetAccountProfile(ctx context.Context, uid int64) (profile *accModel.ProfileStat, err error) {
  52. arg := &accModel.ArgMid{Mid: uid}
  53. if profile, err = d.accountClient.ProfileWithStat3(ctx, arg); err != nil || profile == nil {
  54. log.Error("[dao.user|GetAccountProfile] get account profile3 error(%v), uid(%d), profile(%v)", err, uid, profile)
  55. return
  56. }
  57. return
  58. }
  59. // GetWallet get silver/gold from go-wallet by http request
  60. func (d *Dao) GetWallet(ctx context.Context, uid int64, platform string) (silver, gold int64, err error) {
  61. m := make(map[string]string)
  62. m["uid"] = strconv.FormatInt(uid, 10)
  63. paramString := dao.EncodeHttpParams(m, d.c.HTTPClient.Key, d.c.HTTPClient.Secret)
  64. req, _ := http.NewRequest("GET", d.walletUrl+"?"+paramString, nil)
  65. req.Header.Set("Content-Type", "application/json")
  66. req.Header.Set("platform", platform)
  67. var wr struct {
  68. Code int `json:"code"`
  69. Message string `json:"message"`
  70. Data struct {
  71. Gold string `json:"gold"`
  72. Silver string `json:"silver"`
  73. } `json:"data"`
  74. }
  75. if err = d.bmClient.Do(ctx, req, &wr); err != nil {
  76. log.Error("[dao.user|GetWallet] connect error(%v), uid(%d), platform(%s)", err, uid, platform)
  77. return
  78. }
  79. if wr.Code != 0 {
  80. err = errors.Wrap(ecode.Int(wr.Code), d.walletUrl+"?"+paramString)
  81. log.Error("[dao.user|GetWallet] request error(%v), uid(%d), platform(%s)", err, uid, platform)
  82. return
  83. }
  84. gold, _ = strconv.ParseInt(wr.Data.Gold, 10, 64)
  85. silver, _ = strconv.ParseInt(wr.Data.Silver, 10, 64)
  86. return
  87. }
  88. // GetLiveVip get live vip/svip from xuser.vip.Info
  89. func (d *Dao) GetLiveVip(ctx context.Context, uid int64) (vipInfo *xuserv1.InfoReply, err error) {
  90. uidReq := &xuserv1.UidReq{
  91. Uid: uid,
  92. }
  93. if vipInfo, err = d.vipClient.Info(ctx, uidReq); err != nil || vipInfo == nil {
  94. log.Error("[dao.user|GetLiveVip] get vip error(%v), uid(%d)", err, uid)
  95. return
  96. }
  97. return
  98. }
  99. // GetLiveExp get live exp from xuser.exp.GetUserExp
  100. func (d *Dao) GetLiveExp(ctx context.Context, uid int64) (expInfo *xuserv1.LevelInfo, err error) {
  101. req := &xuserv1.GetUserExpReq{
  102. Uids: []int64{uid},
  103. }
  104. resp, err := d.expClient.GetUserExp(ctx, req)
  105. if err != nil {
  106. log.Error("[dao.user|GetLiveExp] get exp error(%v), uid(%d)", err, uid)
  107. return
  108. }
  109. var ok bool
  110. if expInfo, ok = resp.Data[uid]; !ok {
  111. log.Error("[dao.user|GetLiveExp] get exp empty, uid(%d)", uid)
  112. return
  113. }
  114. return
  115. }
  116. // GetLiveAchieve get rc achieve by liverpc
  117. func (d *Dao) GetLiveAchieve(ctx context.Context, uid int64) (achieve int64, err error) {
  118. resp, err := d.rcClient.Userstatus(ctx, &rcv1.AchvUserstatusReq{})
  119. if err != nil || resp == nil || resp.Data == nil {
  120. log.Error("[dao.user|GetLiveAchieve] get rc achieve error(%v), uid(%d), resp(%v)", err, uid, resp)
  121. return
  122. }
  123. achieve = resp.Data.Point
  124. return
  125. }
  126. // GetLiveRank get user rank by liverpc
  127. func (d *Dao) GetLiveRank(ctx context.Context, uid int64) (rank string, err error) {
  128. rank = "1000000"
  129. req := &rankdbv1.UserRankGetUserRankReq{
  130. Uid: uid,
  131. Type: "user_level",
  132. }
  133. resp, err := d.rankdbClient.GetUserRank(ctx, req)
  134. if err != nil || resp == nil || resp.Data == nil {
  135. log.Error("[dao.user|GetLiveRank] get rankdb user rank error(%v), uid(%d)", err, uid)
  136. return
  137. }
  138. rank = strconv.FormatInt(resp.Data.Rank, 10)
  139. return
  140. }