12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package thirdparty
- import (
- "fmt"
- "gd_vehicle/common.in/config"
- id2 "gd_vehicle/common.in/id"
- "gd_vehicle/common.in/utils"
- "go.uber.org/zap"
- //"gd_vehicle/common.in/config"
- "net/url"
- //"github.com/tidwall/gjson"
- "io/ioutil"
- "net/http"
- "time"
- )
- var publicDybdKey = []byte(`
- -----BEGIN PUBLIC KEY-----
- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDxCRsGHGsaSeavh45EzMtQlx0BvY9TkHuQ1ENF20bLEipKkIFjiZmdiYXXl2GsRj5+yovs5ZkKZZX8zUxN/dPLDqKhTsVn4d8EcZ38QlkTbVNXdbqlmS2OaD0EmJoYbH5ZVY/8AnH0AdonK2x/Xvg4zZeZbGMeN8zmNQf2XwLAVQIDAQAB
- -----END PUBLIC KEY-----
- `)
- func DyBdClientPost(api string, data map[string]string, httpTimeout int) (result []byte, err error) {
- defer func() {
- l.Info("thirdparty",
- zap.String("api", api),
- zap.String("request", utils.MarshalJsonString(data)),
- zap.String("response", utils.MarshalJsonString(result)))
- }()
- if httpTimeout == 0 {
- httpTimeout = 30
- }
- client := &http.Client{}
- data["pageNo"] = "1"
- data["pageNo"] = "10"
- client.Timeout = time.Duration(httpTimeout) * time.Second
- //requestData, _ := json.Marshal(data)
- //fmt.Println("request data :", string(requestData))
- v := url.Values{}
- for k, v1 := range data {
- v.Add(k, v1)
- }
- req, err := http.NewRequest("GET", api+"?"+v.Encode(), nil)
- if err != nil {
- return nil, err
- }
- now := time.Now()
- id, _ := id2.GetDistributedUniqueID()
- nonce := fmt.Sprintf("%d", id)
- aesKey := fmt.Sprintf("%d", now.UnixNano()/1e3)
- stamp := fmt.Sprintf("%d", now.UnixNano()/1e6)
- header := map[string]string{}
- header["appId"] = "test010"
- header["appSecret"] = "VePz0pi@"
- header["timestamp"] = stamp
- header["nonce"] = nonce
- aesEncryptData, err := config.Base64RsaEncrypt([]byte(aesKey), publicDybdKey)
- if err != nil {
- return nil, err
- }
- header["aesSecretEncrypt"] = string(aesEncryptData)
- signStr := fmt.Sprintf("%s:%s:%s:%s:%s", header["appId"], header["appSecret"], header["aesSecretEncrypt"], header["timestamp"], header["nonce"])
- //signStr := header["appId"] + ":" + header["appSecret"] + ":" + string(aesEncryptData) + ":" + header["timestamp"] + ":" + header["nonce"]
- sign, err := config.Base64AESECBEncrypt(signStr, aesKey)
- if err != nil {
- return nil, err
- }
- header["sign"] = string(sign)
- for k, v := range header {
- req.Header.Set(k, v)
- }
- req.Header.Set("Content-Type", "application/json")
- resp, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
- }
- defer resp.Body.Close()
- result, err = ioutil.ReadAll(resp.Body)
- return
- }
|