njHttpClient.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package thirdparty
  2. import (
  3. "bytes"
  4. "gd_management/common.in/config"
  5. "gd_management/common.in/utils"
  6. "encoding/xml"
  7. // "fmt"
  8. "go.uber.org/zap"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. )
  13. var (
  14. njHost = "http://service.jdcnj.com"
  15. njApiPrefix = "OrderService.asmx"
  16. njKeyProduction = "E78E1F76-A9FE-4687-A3EB-198B7628D959" //正式
  17. njKey = "E8D64B6E-1008-49A7-B475-915496C6037D" //测试
  18. )
  19. type NjHttpResp struct {
  20. XMLName xml.Name `xml:"string"`
  21. Content string `xml:",chardata"`
  22. }
  23. func njFullUrl(host string, api string) string {
  24. return host + "/" + njApiPrefix + "/" + api
  25. }
  26. func njEncryptkey() []byte {
  27. //return []byte("yfctct5X")
  28. return []byte(config.Conf.ThirdPart.NjEncKey)
  29. }
  30. func njEncryptIv() []byte {
  31. return []byte{0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef}
  32. }
  33. func NjHttpPost(host string, api string, data map[string]string) (result []byte, err error) {
  34. defer func() {
  35. if l == nil {
  36. return
  37. }
  38. l.Info("thirdparty",
  39. zap.String("api", njFullUrl(host, api)),
  40. zap.String("request", utils.MarshalJsonString(data)),
  41. zap.String("response", string(result)))
  42. }()
  43. if data == nil {
  44. data = make(map[string]string)
  45. }
  46. if true {
  47. data["strKey"] = config.Conf.ThirdPart.NjKey
  48. } else {
  49. data["strKey"] = config.Conf.ThirdPart.NjKey
  50. }
  51. values := url.Values{}
  52. for k, v := range data {
  53. encrypt, err := utils.DesEncrypt(njEncryptkey(), njEncryptIv(), []byte(v))
  54. if err != nil {
  55. continue
  56. }
  57. values.Set(k, string(utils.Base64Encode(encrypt)))
  58. }
  59. client := &http.Client{}
  60. //client.Timeout = baseClient.GetTimeOut()
  61. req, err := http.NewRequest("POST", njFullUrl(host, api), bytes.NewBufferString(values.Encode()))
  62. if err != nil {
  63. return nil, err
  64. }
  65. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  66. resp, err := client.Do(req)
  67. if err != nil {
  68. return nil, err
  69. }
  70. defer resp.Body.Close()
  71. body, err := ioutil.ReadAll(resp.Body)
  72. if err != nil {
  73. return nil, err
  74. }
  75. response := NjHttpResp{}
  76. err = xml.Unmarshal(body, &response)
  77. if err != nil {
  78. return nil, err
  79. }
  80. unBase64, err := utils.Base64Decode([]byte(response.Content))
  81. if err != nil {
  82. return nil, err
  83. }
  84. result, err = utils.DesDecrypt(njEncryptkey(), njEncryptIv(), unBase64)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return result, nil
  89. }