zr.go 2.7 KB

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