metal.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/pkg/errors"
  7. "go-common/app/service/live/wallet/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "net/http"
  11. "net/url"
  12. "time"
  13. )
  14. const (
  15. ModifyUrl = "http://api.bilibili.co/x/internal/v1/coin/user/modify"
  16. InfoUrl = "http://api.bilibili.co/x/internal/v1/coin/user/count"
  17. ExchangeSilverReason = "兑换直播银瓜子 %d"
  18. ExchangeMetalReason = "银瓜子兑换硬币"
  19. )
  20. var (
  21. respCodeError = errors.New("query response code err")
  22. paramError = errors.New("param error")
  23. )
  24. func (d *Dao) GetMetal(c context.Context, uid int64) (metal float64, err error) {
  25. params := url.Values{}
  26. params.Set("mid", fmt.Sprintf("%d", uid))
  27. now := time.Now().Unix()
  28. params.Set("ts", fmt.Sprintf("%d", now))
  29. var req *http.Request
  30. if req, err = d.httpClient.NewRequest("GET", InfoUrl, "", params); err != nil {
  31. log.Error("wallet metal newRequest err:%s", err.Error())
  32. return
  33. }
  34. queryUrl := req.URL.String()
  35. var res struct {
  36. Code int `json:"code"`
  37. Data model.MetalData `json:"data"`
  38. }
  39. err = d.httpClient.Do(c, req, &res)
  40. if err != nil {
  41. log.Error("wallet query metal err url: %s,err:%s", queryUrl, err.Error())
  42. return
  43. }
  44. resStr, _ := json.Marshal(res)
  45. log.Info("wallet query metal success url:%s,res:%s", queryUrl, resStr)
  46. if res.Code != 0 {
  47. err = respCodeError
  48. } else {
  49. metal = res.Data.Count
  50. }
  51. return
  52. }
  53. func (d *Dao) ModifyMetal(c context.Context, uid int64, coins int64, seeds int64, reason interface{}) (success bool, code int, err error) {
  54. if coins == 0 {
  55. err = paramError
  56. return
  57. }
  58. if coins < 0 {
  59. // 检查是否足够
  60. metal, _ := d.GetMetal(c, uid)
  61. if metal < float64(0-coins) {
  62. err = ecode.CoinNotEnough
  63. return
  64. }
  65. }
  66. params := url.Values{}
  67. params.Set("mid", fmt.Sprintf("%d", uid))
  68. params.Set("count", fmt.Sprintf("%d", coins))
  69. now := time.Now().Unix()
  70. params.Set("ts", fmt.Sprintf("%d", now))
  71. var realReason string
  72. switch reason.(type) {
  73. case string:
  74. realReason = reason.(string)
  75. default:
  76. if coins < 0 {
  77. realReason = fmt.Sprintf(ExchangeSilverReason, seeds)
  78. } else {
  79. realReason = ExchangeMetalReason
  80. }
  81. }
  82. log.Info("user %d consume or recharge metal %d by reason %s", uid, coins, realReason)
  83. params.Set("reason", realReason)
  84. var req *http.Request
  85. req, err = d.httpClient.NewRequest("POST", ModifyUrl, "", params)
  86. if err != nil {
  87. log.Error("wallet metal newRequest err:%s", err.Error())
  88. return
  89. }
  90. queryUrl := req.URL.String()
  91. var res struct {
  92. Code int `json:"code"`
  93. }
  94. err = d.httpClient.Do(c, req, &res)
  95. if err != nil {
  96. log.Error("Metal#wallet query metal err url: %s,err:%s uid:%d, count:%d", queryUrl, err.Error(), uid, coins)
  97. // 认为成功
  98. err = nil
  99. success = true
  100. code = 0
  101. return
  102. }
  103. resStr, _ := json.Marshal(res)
  104. log.Info("wallet query metal success url:%s, uid:%d, count:%d res:%s", queryUrl, uid, coins, resStr)
  105. code = res.Code
  106. if res.Code == 0 {
  107. success = true
  108. } else {
  109. if res.Code == ecode.LackOfCoins.Code() {
  110. err = ecode.CoinNotEnough
  111. } else {
  112. log.Error("Metal#wallet query metal code failed : uid:%d, count:%d,code:%d", uid, coins, res.Code)
  113. err = respCodeError
  114. }
  115. }
  116. return
  117. }