123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package thirdparty
- import (
- "bytes"
- "gd_management/common.in/config"
- "gd_management/common.in/utils"
- "encoding/xml"
- // "fmt"
- "go.uber.org/zap"
- "io/ioutil"
- "net/http"
- "net/url"
- )
- var (
- njHost = "http://service.jdcnj.com"
- njApiPrefix = "OrderService.asmx"
- njKeyProduction = "E78E1F76-A9FE-4687-A3EB-198B7628D959" //正式
- njKey = "E8D64B6E-1008-49A7-B475-915496C6037D" //测试
- )
- type NjHttpResp struct {
- XMLName xml.Name `xml:"string"`
- Content string `xml:",chardata"`
- }
- func njFullUrl(host string, api string) string {
- return host + "/" + njApiPrefix + "/" + api
- }
- func njEncryptkey() []byte {
- //return []byte("yfctct5X")
- return []byte(config.Conf.ThirdPart.NjEncKey)
- }
- func njEncryptIv() []byte {
- return []byte{0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef}
- }
- func NjHttpPost(host string, api string, data map[string]string) (result []byte, err error) {
- defer func() {
- if l == nil {
- return
- }
- l.Info("thirdparty",
- zap.String("api", njFullUrl(host, api)),
- zap.String("request", utils.MarshalJsonString(data)),
- zap.String("response", string(result)))
- }()
- if data == nil {
- data = make(map[string]string)
- }
- if true {
- data["strKey"] = config.Conf.ThirdPart.NjKey
- } else {
- data["strKey"] = config.Conf.ThirdPart.NjKey
- }
- values := url.Values{}
- for k, v := range data {
- encrypt, err := utils.DesEncrypt(njEncryptkey(), njEncryptIv(), []byte(v))
- if err != nil {
- continue
- }
- values.Set(k, string(utils.Base64Encode(encrypt)))
- }
- client := &http.Client{}
- //client.Timeout = baseClient.GetTimeOut()
- req, err := http.NewRequest("POST", njFullUrl(host, api), bytes.NewBufferString(values.Encode()))
- if err != nil {
- return nil, err
- }
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- resp, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return nil, err
- }
- response := NjHttpResp{}
- err = xml.Unmarshal(body, &response)
- if err != nil {
- return nil, err
- }
- unBase64, err := utils.Base64Decode([]byte(response.Content))
- if err != nil {
- return nil, err
- }
- result, err = utils.DesDecrypt(njEncryptkey(), njEncryptIv(), unBase64)
- if err != nil {
- return nil, err
- }
- return result, nil
- }
|