http.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/service/main/vip/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _token = "/x/internal/v1/captcha/token"
  15. _verify = "/x/internal/v1/captcha/verify"
  16. _openCode = "/pay/codeOpen"
  17. _passportDetail = "/intranet/acc/detail"
  18. token = "vip-service.token"
  19. )
  20. //GetPassportDetail get passport detail
  21. func (d *Dao) GetPassportDetail(c context.Context, mid int64) (res *model.PassportDetail, err error) {
  22. ip := metadata.String(c, metadata.RemoteIP)
  23. val := url.Values{}
  24. val.Add("mid", strconv.FormatInt(mid, 10))
  25. resq := new(struct {
  26. Code int `json:"code"`
  27. Data *model.PassportDetail `json:"data"`
  28. })
  29. defer func() {
  30. log.Info("get passport detail is error url:%+v params:%+v resq:%+v err:%+v", d.passportDetail, val, resq, err)
  31. }()
  32. if err = d.client.Get(c, d.passportDetail, ip, val, resq); err != nil {
  33. err = errors.WithStack(err)
  34. return
  35. }
  36. if resq.Code != ecode.OK.Code() {
  37. err = ecode.Int(resq.Code)
  38. return
  39. }
  40. res = resq.Data
  41. return
  42. }
  43. //OpenCode open code.
  44. func (d *Dao) OpenCode(c context.Context, mid, batchCodeID int64, unit int32, remark, code string) (data *model.CommonResq, err error) {
  45. data = new(model.CommonResq)
  46. val := url.Values{}
  47. val.Add("token", token)
  48. val.Add("remark", remark)
  49. val.Add("code", code)
  50. val.Add("mid", fmt.Sprintf("%v", mid))
  51. if err = d.doRequest(c, d.c.Property.VipURL, _openCode, "", val, data, d.client.Post); err != nil {
  52. err = errors.WithStack(err)
  53. return
  54. }
  55. return
  56. }
  57. //GetOpenInfo get open info.
  58. func (d *Dao) GetOpenInfo(c context.Context, code string) (data *model.OpenCodeResp, err error) {
  59. data = new(model.OpenCodeResp)
  60. val := url.Values{}
  61. val.Add("token", token)
  62. val.Add("code", code)
  63. if err = d.doRequest(c, d.c.Property.VipURL, _openCode, "", val, data, d.client.Get); err != nil {
  64. err = errors.WithStack(err)
  65. return
  66. }
  67. return
  68. }
  69. //GetToken get open token.
  70. func (d *Dao) GetToken(c context.Context, bid string, ip string) (data *model.TokenResq, err error) {
  71. data = new(model.TokenResq)
  72. val := url.Values{}
  73. val.Add("bid", bid)
  74. if err = d.doRequest(c, d.c.Property.APICoURL, _token, ip, val, data, d.client.Get); err != nil {
  75. err = errors.WithStack(err)
  76. return
  77. }
  78. return
  79. }
  80. //Verify verify code.
  81. func (d *Dao) Verify(c context.Context, code, token, ip string) (data *model.TokenResq, err error) {
  82. data = new(model.TokenResq)
  83. val := url.Values{}
  84. val.Add("token", token)
  85. val.Add("code", code)
  86. if err = d.doRequest(c, d.c.Property.APICoURL, _verify, ip, val, data, d.client.Post); err != nil {
  87. err = errors.WithStack(err)
  88. return
  89. }
  90. return
  91. }
  92. func (d *Dao) doRequest(c context.Context, basicURL, path, IP string, params url.Values, data interface{},
  93. fn func(c context.Context, uri, ip string, params url.Values, res interface{}) error) (err error) {
  94. var (
  95. url = basicURL + path
  96. )
  97. if len(IP) == 0 {
  98. IP = "127.0.0.1"
  99. }
  100. err = fn(c, url, IP, params, data)
  101. log.Info("reques url %v params:%+v result:%+v", url, params, data)
  102. if err != nil {
  103. log.Error("request error %+v", err)
  104. err = errors.WithStack(err)
  105. return
  106. }
  107. return
  108. }