zr.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package thirdparty
  4. import (
  5. "bytes"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "time"
  11. "encoding/json"
  12. "gd_service/common.in/config"
  13. "gd_service/common.in/utils"
  14. "go.uber.org/zap"
  15. )
  16. func concatGetURL(url string, data map[string]string) string {
  17. if data == nil {
  18. return url
  19. }
  20. for k, v := range data {
  21. url = url + "&" + k + "=" + v
  22. }
  23. url = strings.Replace(url, "&", "?", 1)
  24. return url
  25. }
  26. func ZrLogin(hostPath string,httpTimeout int) (body []byte, err error) {
  27. api := hostPath + "/api/login"
  28. data := make(map[string]string)
  29. data["username"] = config.Conf.ThirdPart.ZrUsername
  30. data["password"] = config.Conf.ThirdPart.ZrPassword
  31. defer func() {
  32. l.Info("thirdparty",
  33. zap.String("api", api),
  34. zap.String("request", utils.MarshalJsonString(data)),
  35. zap.String("response", string(body)))
  36. }()
  37. if httpTimeout == 0 {
  38. httpTimeout = 60
  39. }
  40. client := &http.Client{
  41. Timeout: time.Duration(httpTimeout) * time.Second,
  42. }
  43. req, err := http.NewRequest("POST", concatGetURL(api, data), nil)
  44. if err != nil {
  45. return body, err
  46. }
  47. req.Header.Add("Content-Type", "application/json")
  48. resp, err := client.Do(req)
  49. if err != nil {
  50. return body, err
  51. }
  52. defer resp.Body.Close()
  53. body, err = ioutil.ReadAll(resp.Body)
  54. if err != nil {
  55. return body, err
  56. }
  57. return body, nil
  58. }
  59. func ZrHttpClient(api,token string,data map[string]string, httpTimeout int) (body []byte, err error) {
  60. defer func() {
  61. l.Info("thirdparty",
  62. zap.String("api", api),
  63. zap.String("request", utils.MarshalJsonString(data)),
  64. zap.String("response", string(body)))
  65. }()
  66. delete(data,"plateType")
  67. if httpTimeout == 0 {
  68. httpTimeout = 60
  69. }
  70. client := &http.Client{
  71. Timeout: time.Duration(httpTimeout) * time.Second,
  72. }
  73. jsonData, err := json.Marshal(data)
  74. if err != nil {
  75. return nil, err
  76. }
  77. req, err := http.NewRequest("POST", api, bytes.NewBuffer(jsonData))
  78. if err != nil {
  79. return body, err
  80. }
  81. fmt.Println("token:",token)
  82. req.Header.Add("Content-Type", "application/json")
  83. req.Header.Add("token",token)
  84. resp, err := client.Do(req)
  85. if err != nil {
  86. return body, err
  87. }
  88. defer resp.Body.Close()
  89. body, err = ioutil.ReadAll(resp.Body)
  90. if err != nil {
  91. return body, err
  92. }
  93. return body, nil
  94. }