zxHttpClient.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package thirdparty
  2. import (
  3. "gd_management/common.in/config"
  4. "gd_management/common.in/utils"
  5. "fmt"
  6. "go.uber.org/zap"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "time"
  11. )
  12. const (
  13. zxHost = "http://vip.98zhengxin.com"
  14. //zxAppKey = "001061808271603202J1I3Q1H0T2L"
  15. //zxAppCode = "65db6e13ecef31a54642df747986472a"
  16. zxAppKey = "002131812131656325G1J8O8E2Z7E"
  17. zxAppCode = "2b7a2390dabe846437c74a51244fee4c"
  18. )
  19. func zxFullUrl(api string) string {
  20. return fmt.Sprintf("%s/%s", zxHost, api)
  21. }
  22. func ZxHttpGet(api string, data map[string]string) (result []byte, err error) {
  23. //url := zxFullUrl(api)
  24. defer func() {
  25. l.Info("thirdparty",
  26. zap.String("api", api),
  27. zap.String("request", utils.MarshalJsonString(data)),
  28. zap.String("response", string(result)))
  29. }()
  30. req, err := http.NewRequest("GET", zxConcatGetURL(api, data), nil)
  31. req.Header.Set("COMPANY_KEY", config.Conf.ThirdPart.ZxCompanyKey)
  32. req.Header.Set("COMPANY_CODE", config.Conf.ThirdPart.ZxCompanyCode)
  33. req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  34. client := &http.Client{}
  35. client.Timeout = 60 * time.Second
  36. resp, err := client.Do(req)
  37. if err != nil {
  38. return nil, err
  39. }
  40. defer resp.Body.Close()
  41. result, err = ioutil.ReadAll(resp.Body)
  42. return result, err
  43. }
  44. func zxConcatGetURL(url string, data map[string]string) string {
  45. if data == nil {
  46. return url
  47. }
  48. for k, v := range data {
  49. url = url + "&" + k + "=" + v
  50. }
  51. url = strings.Replace(url, "&", "?", 1)
  52. return url
  53. }