api.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package order
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "time"
  8. "go-common/app/interface/main/creative/conf"
  9. "go-common/app/interface/main/creative/dao/tool"
  10. "go-common/app/interface/main/creative/model/order"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. )
  14. // UpValidate fn
  15. func (d *Dao) UpValidate(c context.Context, mid int64, ip string) (uv *order.UpValidate, err error) {
  16. params := url.Values{}
  17. params.Set("mid", strconv.FormatInt(mid, 10))
  18. params.Set("appkey", conf.Conf.HTTPClient.UpMng.Key)
  19. params.Set("appsecret", conf.Conf.HTTPClient.UpMng.Secret)
  20. params.Set("ts", strconv.FormatInt(time.Now().UnixNano()/1000000, 10))
  21. var (
  22. sign, _ = tool.Sign(params)
  23. res struct {
  24. Status string `json:"status"`
  25. CurrentTime int64 `json:"current_time"`
  26. Result *order.UpValidate `json:"result"`
  27. }
  28. _upValidateURL = d.upValidateURI + "?" + sign
  29. )
  30. log.Info("upValidate url(%s)", _upValidateURL)
  31. req, err := http.NewRequest("GET", _upValidateURL, nil)
  32. if err != nil {
  33. log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", _upValidateURL, err, mid, ip)
  34. err = ecode.CreativeOrderAPIErr
  35. return
  36. }
  37. req.Header.Set("Accept", "application/json")
  38. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  39. if err = d.client.Do(c, req, &res); err != nil {
  40. log.Error("d.client.Do upValidate url(%s)|mid(%d)|ip(%s)|error(%v)", _upValidateURL, mid, ip, err)
  41. err = ecode.CreativeOrderAPIErr
  42. return
  43. }
  44. if res.Status != "success" {
  45. log.Error("upValidate url(%s)|mid(%d) res(%v)", _upValidateURL, mid, res)
  46. return
  47. }
  48. uv = res.Result
  49. return
  50. }
  51. // GrowAccountState 获取up主状态 type 类型 0 视频 2 专栏 3 素材.
  52. func (d *Dao) GrowAccountState(c context.Context, mid int64, ty int) (result *order.UpValidate, err error) {
  53. params := url.Values{}
  54. params.Set("mid", strconv.FormatInt(mid, 10))
  55. params.Set("type", strconv.Itoa(ty))
  56. var res struct {
  57. Code int `json:"code"`
  58. Message string `json:"message"`
  59. Data *order.UpValidate `json:"data"`
  60. }
  61. if err = d.client.Get(c, d.accountStateURI, "", params, &res); err != nil {
  62. log.Error("GrowAccountState url(%s) response(%v) error(%v)", d.accountStateURI+"?"+params.Encode(), res, err)
  63. err = ecode.CreativeOrderAPIErr
  64. return
  65. }
  66. log.Info("GrowAccountState url(%s)", d.accountStateURI+"?"+params.Encode())
  67. if res.Code != 0 {
  68. log.Error("GrowAccountState url(%s),code(%d) msg(%s) res(%v)", d.accountStateURI, res.Code, res.Message, res)
  69. err = ecode.Int(res.Code)
  70. return
  71. }
  72. result = res.Data
  73. return
  74. }