adm.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package thirdparty
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "gd_vehicle/apis"
  7. "io/ioutil"
  8. "net/http"
  9. "strconv"
  10. "time"
  11. "gd_vehicle/common.in/config"
  12. "gd_vehicle/common.in/utils"
  13. jsoniter "github.com/json-iterator/go"
  14. "go.uber.org/zap"
  15. )
  16. func Adm(
  17. ctx context.Context,
  18. api string,
  19. params map[string]string,
  20. provider apis.MerchantProviderLimitInfo,
  21. ) (result []byte, err error) {
  22. defer func() {
  23. l.Info("thirdparty",
  24. zap.String("api", api),
  25. zap.String("request", utils.MarshalJsonString(params)),
  26. zap.String("response", utils.MarshalJsonString(result)))
  27. }()
  28. ts := strconv.FormatInt(time.Now().Unix(), 10)
  29. appkey := config.Conf.ThirdPart.AdmAppkey
  30. sign := utils.MD5(appkey + ts)
  31. body := map[string]interface{}{
  32. "api": api,
  33. }
  34. body["param"], _ = jsoniter.MarshalToString(params)
  35. b, err := jsoniter.Marshal(body)
  36. if err != nil {
  37. return result, err
  38. }
  39. client := &http.Client{}
  40. req, err := http.NewRequest(
  41. provider.ThirdpartApiMethod,
  42. provider.ThirdpartHost+"/"+provider.ThirdpartApiRouter,
  43. bytes.NewBuffer(b),
  44. )
  45. if err != nil {
  46. return result, err
  47. }
  48. req.Header.Add("Content-Type", "application/json")
  49. req.Header.Add("appkey", appkey)
  50. req.Header.Add("ts", ts)
  51. req.Header.Add("sign", sign)
  52. resp, err := client.Do(req)
  53. if err != nil {
  54. return result, err
  55. }
  56. if resp.StatusCode != http.StatusOK {
  57. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  58. }
  59. defer resp.Body.Close()
  60. result, err = ioutil.ReadAll(resp.Body)
  61. return result, err
  62. }