dydb.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package thirdparty
  2. import (
  3. "fmt"
  4. "gd_vehicle/common.in/config"
  5. id2 "gd_vehicle/common.in/id"
  6. "gd_vehicle/common.in/utils"
  7. "go.uber.org/zap"
  8. //"gd_vehicle/common.in/config"
  9. "net/url"
  10. //"github.com/tidwall/gjson"
  11. "io/ioutil"
  12. "net/http"
  13. "time"
  14. )
  15. var publicDybdKey = []byte(`
  16. -----BEGIN PUBLIC KEY-----
  17. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDxCRsGHGsaSeavh45EzMtQlx0BvY9TkHuQ1ENF20bLEipKkIFjiZmdiYXXl2GsRj5+yovs5ZkKZZX8zUxN/dPLDqKhTsVn4d8EcZ38QlkTbVNXdbqlmS2OaD0EmJoYbH5ZVY/8AnH0AdonK2x/Xvg4zZeZbGMeN8zmNQf2XwLAVQIDAQAB
  18. -----END PUBLIC KEY-----
  19. `)
  20. func DyBdClientPost(api string, data map[string]string, httpTimeout int) (result []byte, err error) {
  21. defer func() {
  22. l.Info("thirdparty",
  23. zap.String("api", api),
  24. zap.String("request", utils.MarshalJsonString(data)),
  25. zap.String("response", utils.MarshalJsonString(result)))
  26. }()
  27. if httpTimeout == 0 {
  28. httpTimeout = 30
  29. }
  30. client := &http.Client{}
  31. data["pageNo"] = "1"
  32. data["pageNo"] = "10"
  33. client.Timeout = time.Duration(httpTimeout) * time.Second
  34. //requestData, _ := json.Marshal(data)
  35. //fmt.Println("request data :", string(requestData))
  36. v := url.Values{}
  37. for k, v1 := range data {
  38. v.Add(k, v1)
  39. }
  40. req, err := http.NewRequest("GET", api+"?"+v.Encode(), nil)
  41. if err != nil {
  42. return nil, err
  43. }
  44. now := time.Now()
  45. id, _ := id2.GetDistributedUniqueID()
  46. nonce := fmt.Sprintf("%d", id)
  47. aesKey := fmt.Sprintf("%d", now.UnixNano()/1e3)
  48. stamp := fmt.Sprintf("%d", now.UnixNano()/1e6)
  49. header := map[string]string{}
  50. header["appId"] = "test010"
  51. header["appSecret"] = "VePz0pi@"
  52. header["timestamp"] = stamp
  53. header["nonce"] = nonce
  54. aesEncryptData, err := config.Base64RsaEncrypt([]byte(aesKey), publicDybdKey)
  55. if err != nil {
  56. return nil, err
  57. }
  58. header["aesSecretEncrypt"] = string(aesEncryptData)
  59. signStr := fmt.Sprintf("%s:%s:%s:%s:%s", header["appId"], header["appSecret"], header["aesSecretEncrypt"], header["timestamp"], header["nonce"])
  60. //signStr := header["appId"] + ":" + header["appSecret"] + ":" + string(aesEncryptData) + ":" + header["timestamp"] + ":" + header["nonce"]
  61. sign, err := config.Base64AESECBEncrypt(signStr, aesKey)
  62. if err != nil {
  63. return nil, err
  64. }
  65. header["sign"] = string(sign)
  66. for k, v := range header {
  67. req.Header.Set(k, v)
  68. }
  69. req.Header.Set("Content-Type", "application/json")
  70. resp, err := client.Do(req)
  71. if err != nil {
  72. return nil, err
  73. }
  74. if resp.StatusCode != http.StatusOK {
  75. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  76. }
  77. defer resp.Body.Close()
  78. result, err = ioutil.ReadAll(resp.Body)
  79. return
  80. }