123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package thirdparty
- import (
- "bytes"
- "gd_management/errors"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "net/http"
- "strconv"
- "strings"
- "time"
- "gd_management/common.in/config"
- "gd_management/common.in/utils"
- "github.com/tidwall/gjson"
- "go.uber.org/zap"
- )
- type GdResp struct {
- Code int64 `json:"code"`
- Msg string `json:"msg"`
- Data string `json:"data"`
- }
- func concatGdGetURL(url string, data map[string]string) string {
- if data == nil {
- return url
- }
- for k, v := range data {
- url = url + "&" + k + "=" + v
- }
- url = strings.Replace(url, "&", "?", 1)
- return url
- }
- func GdClientPost(api string, data map[string]interface{}) (result []byte, err error) {
- jsonData, err := json.Marshal(data)
- if err != nil {
- return nil, err
- }
- var realData = map[string]string{
- "data": string(jsonData),
- }
- datas, err := json.Marshal(realData)
- if err != nil {
- return nil, err
- }
- client := &http.Client{}
- client.Timeout = 60 * time.Second
- req, err := http.NewRequest("POST", api, bytes.NewBuffer(datas))
- if err != nil {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", err.Error()))
- return nil, err
- }
- timestamp := strconv.FormatInt(time.Now().Unix(), 10)
- req.Header.Set("app_key", config.Conf.GdApiAppKey)
- req.Header.Set("ts", timestamp)
- signtext := fmt.Sprintf("%s%s%s%s", timestamp, string(jsonData), config.Conf.GdApiAppKey, config.Conf.GdApiAppSecret)
- req.Header.Set("sign", utils.MD5(signtext))
- req.Header.Set("Content-Type", "application/json")
- resp, err := client.Do(req)
- if err != nil {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", err.Error()))
- return nil, err
- }
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
- }
- result, err = ioutil.ReadAll(resp.Body)
- if err != nil {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", err.Error()))
- return nil, err
- }
- //fmt.Println("result:",string(result))
- var gdResp GdResp
- err = json.Unmarshal(result, &gdResp)
- if err != nil {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", err.Error()))
- //fmt.Println("unmrshal error")
- return nil, err
- }
- if gdResp.Code != 0 {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", gdResp.Msg))
- //fmt.Println("code is:",gdResp.Code)
- return nil, errors.VendorError
- }
- return []byte(gdResp.Data), nil
- }
- func GdClientGet(api string, data map[string]interface{}) (result []byte, err error) {
- jsonData, err := json.Marshal(data)
- if err != nil {
- return nil, err
- }
- client := &http.Client{}
- client.Timeout = 10 * time.Second
- rData := map[string]string{"data": string(jsonData)}
- req, err := http.NewRequest("GET", concatGdGetURL(api, rData), nil)
- if err != nil {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", err.Error()))
- return nil, err
- }
- timestamp := strconv.FormatInt(time.Now().Unix(), 10)
- req.Header.Set("app_key", config.Conf.GdApiAppKey)
- req.Header.Set("ts", timestamp)
- signtext := fmt.Sprintf("%s%s%s%s", timestamp, string(jsonData), config.Conf.GdApiAppKey, config.Conf.GdApiAppSecret)
- req.Header.Set("sign", utils.MD5(signtext))
- req.Header.Set("Content-Type", "application/json")
- resp, err := client.Do(req)
- if err != nil {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", err.Error()))
- return nil, err
- }
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
- }
- result, err = ioutil.ReadAll(resp.Body)
- if err != nil {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", err.Error()))
- return nil, err
- }
- var gdResp GdResp
- err = json.Unmarshal(result, &gdResp)
- if err != nil {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", err.Error()))
- return nil, err
- }
- if gdResp.Code != 0 {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", gdResp.Msg))
- return nil, errors.VendorError
- }
- return []byte(gdResp.Data), nil
- }
- func GdClientCryptoGet(api string, data map[string]interface{}) (result []byte, err error) {
- jsonData, err := json.Marshal(data)
- if err != nil {
- return nil, err
- }
- client := &http.Client{}
- client.Timeout = 10 * time.Second
- bytes, _ := config.AesEncrypt(string(jsonData), config.Conf.GdApiAppSecret)
- rData := map[string]string{"data": hex.EncodeToString(bytes)}
- req, err := http.NewRequest("GET", concatGdGetURL(api, rData), nil)
- if err != nil {
- return nil, err
- }
- timestamp := strconv.FormatInt(time.Now().Unix(), 10)
- req.Header.Set("app_key", config.Conf.GdApiAppKey)
- req.Header.Set("ts", timestamp)
- signtext := fmt.Sprintf("%s%s%s%s", timestamp, hex.EncodeToString(bytes), config.Conf.GdApiAppKey, config.Conf.GdApiAppSecret)
- req.Header.Set("sign", utils.MD5(signtext))
- req.Header.Set("Content-Type", "application/json")
- resp, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
- }
- rByte, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", err.Error()))
- return nil, err
- }
- //fmt.Println(string(rByte), " error: ", err)
- res := gjson.GetBytes(rByte, "data").String()
- result, err = config.AesDecrypt([]byte(res), []byte(config.Conf.GdApiAppSecret))
- if err != nil {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", err.Error()))
- return nil, err
- }
- var gdResp GdResp
- err = json.Unmarshal(result, &gdResp)
- if err != nil {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", err.Error()))
- return nil, err
- }
- if gdResp.Code != 0 {
- r, _ := json.Marshal(data)
- l.Error("thirdparty",
- zap.String("call", api),
- zap.String("params", string(r)),
- zap.String("error", gdResp.Msg))
- return nil, errors.VendorError
- }
- return []byte(gdResp.Data), nil
- }
|