123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package thirdparty
- import (
- "bytes"
- "crypto/sha1"
- "gd_management/common.in/config"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "net/http"
- "strings"
- "time"
- "gd_management/common.in/utils"
- "go.uber.org/zap"
- )
- type ZjtHttpData struct {
- Status int `json:"status"`
- Message string `json:"message"`
- Data string `json:"data"`
- }
- func SHA1(data []byte) []byte {
- h := sha1.New()
- h.Write(data)
- return h.Sum(nil)
- }
- func AESSHA1PRNG(keyBytes []byte, encryptLength int) ([]byte, error) {
- hashs := SHA1(SHA1(keyBytes))
- maxLen := len(hashs)
- realLen := encryptLength / 8
- if realLen > maxLen {
- return nil, fmt.Errorf("%s", "error")
- }
- return hashs[0:realLen], nil
- }
- func ZjtHttpPost(api string, data map[string]string) (m ZjtHttpData, err error) {
- defer func() {
- l.Info("thirdparty",
- zap.String("api", api),
- zap.String("request", utils.MarshalJsonString(data)),
- zap.String("response", utils.MarshalJsonString(m)))
- }()
- data["authorizationCode"] = config.Conf.ThirdPart.ZjtAuthorizationCode
- jsonData, err := json.Marshal(data)
- if err != nil {
- return
- }
- key, _ := AESSHA1PRNG([]byte(config.Conf.ThirdPart.ZjtCryptoKey), 128)
- paramsByte, err := config.AesEncrypt(string(jsonData), string(key))
- if err != nil {
- return m, err
- }
- params := hex.EncodeToString(paramsByte)
- params = strings.ToUpper(params)
- var r http.Request
- r.ParseForm()
- r.Form.Add("userno", config.Conf.ThirdPart.ZjtUserNo)
- r.Form.Add("querydata", params)
- params = r.Form.Encode()
- client := &http.Client{}
- client.Timeout = 60 * time.Second
- req, err := http.NewRequest("POST", api, bytes.NewBuffer([]byte(params)))
- if err != nil {
- return m, err
- }
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
- resp, err := client.Do(req)
- if err != nil {
- return m, err
- }
- if resp.StatusCode != http.StatusOK {
- return m, fmt.Errorf("wrong status code: %d", resp.StatusCode)
- }
- defer resp.Body.Close()
- result, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return m, err
- }
- json.Unmarshal(result, &m)
- if m.Status == 1 && m.Data != "" {
- r, err := hex.DecodeString(m.Data)
- if err != nil {
- return m, err
- }
- respData, err := config.AesDecrypt(r, key)
- m.Data = string(respData)
- return m, err
- }
- return m, err
- }
|