|
|
@@ -1,6 +1,7 @@
|
|
|
package asign
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"crypto"
|
|
|
"crypto/md5"
|
|
|
"crypto/rand"
|
|
|
@@ -11,13 +12,62 @@ import (
|
|
|
"encoding/hex"
|
|
|
"encoding/pem"
|
|
|
"fmt"
|
|
|
+ "io"
|
|
|
+ "mime/multipart"
|
|
|
+ "mtp2_if/config"
|
|
|
+ "mtp2_if/logger"
|
|
|
+ "net/http"
|
|
|
"sort"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
-func HttpPost(url string, bizData map[string]interface{}) (rspBody []byte, err error) {
|
|
|
+// HttpPost
|
|
|
+//
|
|
|
+// 爱签HttpPost请求方法
|
|
|
+// @param apiUrl 接口地址
|
|
|
+// @param bizData formData入参
|
|
|
+// @return rspBody response body数据
|
|
|
+// @return err 错误
|
|
|
+func HttpPost(apiUrl string, bizData map[string]interface{}) (rspBody []byte, err error) {
|
|
|
+ appId := config.SerCfg.AsignCfg.AppId
|
|
|
+ privateKey := fmt.Sprintf("-----BEGIN PRIVATE KEY-----\n%s\n-----END PRIVATE KEY-----", config.SerCfg.AsignCfg.PrivateKey)
|
|
|
+
|
|
|
+ // fuck asign dev.
|
|
|
+ timestamp := strconv.Itoa(int(time.Now().UnixMilli() + 1000*60))
|
|
|
|
|
|
// 签名
|
|
|
- // timestamp := int64(time.Now().Unix())
|
|
|
+ sortedData := sortMapByKey(bizData)
|
|
|
+ signature, err := getSignature(sortedData, appId, timestamp, privateKey)
|
|
|
+ if err != nil {
|
|
|
+ logger.GetLogger().Errorf("[asign.HttpPost] 签名失败:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建form-data请求参数
|
|
|
+ var requestBody bytes.Buffer
|
|
|
+ multipartWriter := multipart.NewWriter(&requestBody)
|
|
|
+ multipartWriter.WriteField("appId", appId)
|
|
|
+ multipartWriter.WriteField("timestamp", timestamp)
|
|
|
+ multipartWriter.WriteField("bizData", sortedData)
|
|
|
+ multipartWriter.Close()
|
|
|
+ // 构建请求
|
|
|
+ req, err := http.NewRequest("POST", apiUrl, &requestBody)
|
|
|
+ // 设置请求头
|
|
|
+ req.Header.Set("sign", signature)
|
|
|
+ req.Header.Set("timestamp", timestamp)
|
|
|
+ req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
|
|
|
+
|
|
|
+ // 调用接口
|
|
|
+ client := &http.Client{}
|
|
|
+ rsp, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ logger.GetLogger().Errorf("[asign.HttpPost] 调用接口失败:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer rsp.Body.Close()
|
|
|
+ rspBody, err = io.ReadAll(rsp.Body)
|
|
|
|
|
|
return
|
|
|
}
|
|
|
@@ -46,14 +96,11 @@ func HttpPost(url string, bizData map[string]interface{}) (rspBody []byte, err e
|
|
|
// log.error( "sdk异常", e ) ;
|
|
|
// return ApiRespBody.create ( ApiResponseInfo. _ERROR ) ;
|
|
|
// }
|
|
|
-func getSignature(bizData map[string]interface{}, appId, timestamp, privateKey string) (signature string, err error) {
|
|
|
+func getSignature(sortedData string, appId, timestamp, privateKey string) (signature string, err error) {
|
|
|
pem10, _ := pem.Decode([]byte(privateKey))
|
|
|
privateKey10I, _ := x509.ParsePKCS8PrivateKey(pem10.Bytes)
|
|
|
privateKey10 := privateKey10I.(*rsa.PrivateKey)
|
|
|
|
|
|
- // sort by key
|
|
|
- sortedData := sortMapByKey(bizData)
|
|
|
-
|
|
|
// md5(bizData)
|
|
|
m := md5.New()
|
|
|
m.Write([]byte(sortedData))
|
|
|
@@ -74,6 +121,7 @@ func getSignature(bizData map[string]interface{}, appId, timestamp, privateKey s
|
|
|
|
|
|
// fmt.Println(signature)
|
|
|
signature = base64.StdEncoding.EncodeToString(sign)
|
|
|
+ signature = strings.ReplaceAll(signature, "\r\n", "")
|
|
|
|
|
|
return
|
|
|
}
|