// 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 }