jason 1 tahun lalu
induk
melakukan
98adbbf461
1 mengubah file dengan 72 tambahan dan 9 penghapusan
  1. 72 9
      main.go

+ 72 - 9
main.go

@@ -4,14 +4,16 @@ import (
 	"bytes"
 	"crypto/aes"
 	"crypto/cipher"
+	"crypto/md5"
 	"crypto/tls"
 	"encoding/hex"
 	"encoding/json"
 	"fmt"
-	"github.com/tidwall/gjson"
 	"io/ioutil"
 	"net/http"
 	"time"
+
+	"github.com/tidwall/gjson"
 )
 
 type encBlockModer struct {
@@ -149,7 +151,62 @@ func getDataConten(param map[string]string, enc bool, secret string) map[string]
 	return m
 }
 
-func ApiVisit(param map[string]string, enc bool, token string, url string, secret string) {
+func ApiVisitBySign(param map[string]string, enc bool, url string, secret string, appKey string) {
+	client := &http.Client{
+		Transport: &http.Transport{
+			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+		},
+		Timeout: 60 * time.Second,
+	}
+	fmt.Printf("appkey:%s\n", appKey)
+
+	data := getDataConten(param, enc, secret)
+
+	req, err := http.NewRequest("GET", getParamToUrl(url, data), nil)
+	if err != nil {
+		panic(err)
+	}
+	timestamp := time.Now().Unix()
+	signText := fmt.Sprintf("%s%s%d", appKey, secret, timestamp)
+	sign := MD5(signText)
+	req.Header.Set("sign", sign)
+	req.Header.Set("timestamp", fmt.Sprintf("%d", timestamp))
+	req.Header.Set("appkey", appKey)
+	req.Header.Set("appsecret", secret)
+	req.Header.Set("Content-Type", "application/json")
+
+	resp, err := client.Do(req)
+	if err != nil {
+		panic(err)
+	}
+	defer resp.Body.Close()
+	res, err := ioutil.ReadAll(resp.Body)
+	fmt.Printf("originRes:%s\n", res)
+
+	codeExist := gjson.GetBytes(res, "code").Exists()
+	if !codeExist {
+		fmt.Printf("failed: originResis %s\n", res)
+		return
+	}
+	code := gjson.GetBytes(res, "code").Int()
+	if code != 0 {
+		msg := gjson.GetBytes(res, "msg").String()
+		fmt.Printf("api error:%d,%s\n", code, msg)
+		return
+	}
+	resdata := gjson.GetBytes(res, "data").String()
+	if enc && resdata != "" {
+		resdata, err = AesEcbDecrpyt(resdata, secret)
+		if err != nil {
+			panic(err)
+		}
+	}
+	fmt.Printf("success data is:%s\n", resdata)
+
+	return
+}
+
+func ApiVisitByToken(param map[string]string, enc bool, token string, url string, secret string) {
 	client := &http.Client{
 		Transport: &http.Transport{
 			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
@@ -237,22 +294,27 @@ func tokenGet(tokenUrl string, user string, password string) string {
 	return token
 
 }
+func MD5(text string) string {
+	h := md5.New()
+	h.Write([]byte(text))
+	return hex.EncodeToString(h.Sum(nil))
+}
 
 func main() {
 	var (
 		// api 用户名
-		user = ""
+		user = "529db83441acff61a054eba562185515"
 		// api 密码
-		password = ""
+		password = "DMh7lbyv"
 		// 加密密钥
-		secret = ""
+		secret = "1749f2a7019db090ca7c9cc69e64033c"
 		// token url
-		tokenUrl = ""
+		tokenUrl = "http://127.0.0.1:41002/api/v1/token"
 		// 接口url
-		apiUrl = ""
+		apiUrl = "http://127.0.0.1:41002/api/v1/query/test_api"
 		// 参数
 		params = map[string]string{
-			"": "",
+			"chepai": "川A814A1",
 		}
 		// 是否加密
 		crypt = false
@@ -262,5 +324,6 @@ func main() {
 		fmt.Printf("token is empty\n")
 		return
 	}
-	ApiVisit(params, crypt, token, apiUrl, secret)
+	//ApiVisitByToken(params, crypt, token, apiUrl, secret)
+	ApiVisitBySign(params, crypt, apiUrl, secret, user)
 }