api.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package vip
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/account/model"
  8. vipmol "go-common/app/service/main/vip/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/net/metadata"
  12. "github.com/pkg/errors"
  13. )
  14. const (
  15. _vipCodeVerify = "/x/internal/vip/code/verify"
  16. _vipCodeOpen = "/x/internal/vip/code/open"
  17. _viptips = "/x/internal/vip/tips"
  18. _couponCancel = "/x/internal/vip/coupon/cancel"
  19. _vipCodeOpened = "/x/internal/vip/code/opened"
  20. // vip with java
  21. _vipInfo = "/internal/v1/user/"
  22. )
  23. //CodeVerify code verify
  24. func (d *Dao) CodeVerify(c context.Context) (token *model.Token, err error) {
  25. ip := metadata.String(c, metadata.RemoteIP)
  26. var tokenResq *model.TokenResq
  27. val := url.Values{}
  28. if err = d.client.Get(c, d.codeVerifyURL, ip, val, &tokenResq); err != nil {
  29. err = errors.WithStack(err)
  30. return
  31. }
  32. if tokenResq.Code == int64(ecode.OK.Code()) {
  33. token = tokenResq.Data
  34. }
  35. return
  36. }
  37. //CodeOpen http code open
  38. func (d *Dao) CodeOpen(c context.Context, mid int64, code, token, verify string) (data *model.ResourceCodeResq, err error) {
  39. ip := metadata.String(c, metadata.RemoteIP)
  40. val := url.Values{}
  41. val.Add("mid", fmt.Sprintf("%v", mid))
  42. val.Add("token", token)
  43. val.Add("verify", verify)
  44. val.Add("code", code)
  45. defer func() {
  46. log.Info("qingqiu url(%+v) params(%+v) return(%+v)", d.codeOpenURL, val, data)
  47. }()
  48. if err = d.client.Post(c, d.codeOpenURL, ip, val, &data); err != nil {
  49. err = errors.WithStack(err)
  50. return
  51. }
  52. if data.Code > int64(ecode.OK.Code()) {
  53. err = ecode.Int(int(data.Code))
  54. return
  55. }
  56. return
  57. }
  58. // Info get vip info
  59. func (d *Dao) Info(c context.Context, mid int64, ip string) (info *model.VIPInfo, err error) {
  60. var res struct {
  61. Code int `json:"code"`
  62. Data *model.VIPInfo `json:"data"`
  63. }
  64. if err = d.client.Get(c, d.infoURL+strconv.FormatInt(mid, 10), ip, nil, &res); err != nil {
  65. log.Error("d.client.Get(%d) error(%v)", mid, err)
  66. return
  67. }
  68. if res.Code != ecode.OK.Code() {
  69. err = ecode.Int(res.Code)
  70. log.Error("d.client.Get(%d) error(%v)", mid, err)
  71. return
  72. }
  73. info = res.Data
  74. return
  75. }
  76. // Tips tips info.
  77. func (d *Dao) Tips(c context.Context, version int64, platform string) (data *vipmol.TipsResp, err error) {
  78. params := url.Values{}
  79. params.Add("version", fmt.Sprintf("%v", version))
  80. params.Add("platform", platform)
  81. var resp struct {
  82. Code int `json:"code"`
  83. Data *vipmol.TipsResp `json:"data"`
  84. }
  85. if err = d.client.Get(c, d.tipsURL, "", params, &resp); err != nil {
  86. err = errors.Errorf("vip tips d.httpClient.Do(%s) error(%+v)", d.tipsURL+"?"+params.Encode(), err)
  87. return
  88. }
  89. if resp.Code != ecode.OK.Code() {
  90. err = errors.Errorf("vip tips url(%s) res(%+v) err(%+v)", d.tipsURL+"?"+params.Encode(), resp, ecode.Int(resp.Code))
  91. }
  92. data = resp.Data
  93. return
  94. }
  95. // CancelUseCoupon cancel use coupon.
  96. func (d *Dao) CancelUseCoupon(c context.Context, arg *vipmol.ArgCancelUseCoupon) (err error) {
  97. params := url.Values{}
  98. params.Add("mid", fmt.Sprintf("%d", arg.Mid))
  99. params.Add("coupon_token", arg.CouponToken)
  100. var resp struct {
  101. Code int `json:"code"`
  102. }
  103. if err = d.clientSlow.Post(c, d.cancelCouponURL, "", params, &resp); err != nil {
  104. err = errors.Errorf("vip cancel coupon d.httpClient.Do(%s) error(%+v)", d.cancelCouponURL+"?"+params.Encode(), err)
  105. return
  106. }
  107. if resp.Code != ecode.OK.Code() {
  108. err = ecode.Int(resp.Code)
  109. }
  110. return
  111. }
  112. //CodeOpeneds sel code opened data
  113. func (d *Dao) CodeOpeneds(c context.Context, arg *model.CodeInfoReq, ip string) (resp []*vipmol.CodeInfoResp, err error) {
  114. val := url.Values{}
  115. val.Add("bis_appkey", arg.Appkey)
  116. val.Add("bis_sign", arg.Sign)
  117. val.Add("bis_ts", strconv.FormatInt(arg.Ts.Time().Unix(), 10))
  118. val.Add("start_time", strconv.FormatInt(arg.StartTime.Time().Unix(), 10))
  119. val.Add("end_time", strconv.FormatInt(arg.EndTime.Time().Unix(), 10))
  120. val.Add("cursor", strconv.FormatInt(arg.Cursor, 10))
  121. rep := new(struct {
  122. Code int `json:"code"`
  123. Data []*vipmol.CodeInfoResp `json:"data"`
  124. })
  125. defer func() {
  126. log.Info("vip code opened url:%+v params:%+v return:%+v", d.codeOpenedURL, val, rep)
  127. }()
  128. if err = d.client.Get(c, d.codeOpenedURL, ip, val, rep); err != nil {
  129. err = errors.Errorf("vip code opened url:%+v params:%+v return:%+v,err:%+v", d.codeOpenedURL, val, rep, err)
  130. return
  131. }
  132. if rep.Code != ecode.OK.Code() {
  133. err = ecode.Int(rep.Code)
  134. return
  135. }
  136. resp = rep.Data
  137. return
  138. }