dao.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package pay
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "go-common/app/interface/main/app-interface/conf"
  14. "go-common/app/interface/main/app-interface/model/pay"
  15. "go-common/library/ecode"
  16. httpx "go-common/library/net/http/blademaster"
  17. "github.com/pkg/errors"
  18. )
  19. type Dao struct {
  20. c *conf.Config
  21. client *httpx.Client
  22. wallet string
  23. }
  24. func New(c *conf.Config) (d *Dao) {
  25. d = &Dao{
  26. c: c,
  27. client: httpx.NewClient(c.HTTPClient),
  28. wallet: c.Host.Pay + "/wallet-int/wallet/getUserWalletInfo",
  29. }
  30. return
  31. }
  32. // UserWalletInfo get user bcoin doc:http://info.bilibili.co/pages/viewpage.action?pageId=7559096
  33. func (d *Dao) UserWalletInfo(c context.Context, mid int64, platform string) (availableBp float64, err error) {
  34. var plat int
  35. if platform == "ios" {
  36. plat = 1
  37. } else if platform == "android" {
  38. plat = 2
  39. } else {
  40. err = fmt.Errorf("platform(%s) error", platform)
  41. return
  42. }
  43. params := url.Values{}
  44. params.Set("customerId", "10006")
  45. params.Set("platformType", strconv.Itoa(plat))
  46. params.Set("mid", strconv.FormatInt(mid, 10))
  47. params.Set("traceId", strconv.FormatInt(time.Now().Unix(), 10))
  48. params.Set("timestamp", strconv.FormatInt(time.Now().UnixNano()/1000, 10))
  49. params.Set("signType", "MD5")
  50. params.Set("appkey", d.c.HTTPClient.Key)
  51. type pJSON struct {
  52. CustomerID string `json:"customerId"`
  53. PlatformType int `json:"platformType"`
  54. Mid int64 `json:"mid"`
  55. TraceID string `json:"traceId"`
  56. Timestamp string `json:"timestamp"`
  57. SignType string `json:"signType"`
  58. Appkey string `json:"appkey"`
  59. Sign string `json:"sign"`
  60. }
  61. tmp := params.Encode() + d.c.HTTPClient.Secret
  62. if strings.IndexByte(tmp, '+') > -1 {
  63. tmp = strings.Replace(tmp, "+", "%20", -1)
  64. }
  65. mh := md5.Sum([]byte(tmp))
  66. sign := hex.EncodeToString(mh[:])
  67. p := &pJSON{
  68. CustomerID: "10006",
  69. PlatformType: plat,
  70. Mid: mid,
  71. TraceID: params.Get("traceId"),
  72. Timestamp: params.Get("timestamp"),
  73. SignType: params.Get("signType"),
  74. Appkey: params.Get("appkey"),
  75. Sign: sign,
  76. }
  77. bs, _ := json.Marshal(p)
  78. req, _ := http.NewRequest("POST", d.wallet, strings.NewReader(string(bs)))
  79. req.Header.Set("Content-Type", "application/json")
  80. var wallet *pay.UserWallet
  81. if err = d.client.Do(c, req, &wallet); err != nil {
  82. return
  83. }
  84. if wallet.Code != 0 {
  85. err = errors.Wrap(ecode.Int(wallet.Code), d.wallet+"?"+params.Encode())
  86. return
  87. }
  88. availableBp = wallet.Data.AvailableBp
  89. return
  90. }