livewallet.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package dao
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "github.com/pkg/errors"
  7. "go-common/app/interface/live/app-room/model"
  8. "go-common/library/ecode"
  9. "net/http"
  10. "net/url"
  11. "strconv"
  12. "time"
  13. )
  14. //LiveWallet 获取用户直播钱包数据
  15. func (d *Dao) LiveWallet(c context.Context, mid int64, platform string) (wallet *model.LiveWallet, err error) {
  16. params := url.Values{}
  17. params.Set("uid", strconv.FormatInt(mid, 10))
  18. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  19. params.Set("appkey", d.c.HTTPClient.LiveRpc.Key)
  20. tmp := params.Encode() + d.c.HTTPClient.LiveRpc.Secret
  21. mh := md5.Sum([]byte(tmp))
  22. sign := hex.EncodeToString(mh[:])
  23. params.Set("sign", sign)
  24. req, _ := http.NewRequest("GET", d.liveWalletURL+"?"+params.Encode(), nil)
  25. req.Header.Set("Content-Type", "application/json")
  26. req.Header.Set("platform", platform)
  27. var wr struct {
  28. Code int `json:"code"`
  29. Message string `json:"message"`
  30. Data struct {
  31. Gold string `json:"gold"`
  32. Silver string `json:"silver"`
  33. GoldRechargeCnt string `json:"gold_recharge_cnt"`
  34. GoldPayCnt string `json:"gold_pay_cnt"`
  35. SilverPayCnt string `json:"silver_pay_cnt"`
  36. } `json:"data"`
  37. }
  38. if err = d.liveWalletClient.Do(c, req, &wr); err != nil {
  39. return
  40. }
  41. if wr.Code != 0 {
  42. err = errors.Wrap(ecode.Int(wr.Code), d.liveWalletURL+"?"+params.Encode())
  43. return
  44. }
  45. wallet = &model.LiveWallet{}
  46. wallet.Gold, _ = strconv.ParseInt(wr.Data.Gold, 10, 64)
  47. wallet.Silver, _ = strconv.ParseInt(wr.Data.Silver, 10, 64)
  48. wallet.GoldRechargeCnt, _ = strconv.ParseInt(wr.Data.GoldRechargeCnt, 10, 64)
  49. wallet.GoldPayCnt, _ = strconv.ParseInt(wr.Data.GoldPayCnt, 10, 64)
  50. wallet.SilverPayCnt, _ = strconv.ParseInt(wr.Data.SilverPayCnt, 10, 64)
  51. return
  52. }