From af2b8173a6e3278130e83bd8abd3d076e594f9dd Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Fri, 28 Jun 2024 16:28:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=BC=E5=A4=9A=E5=A4=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- meituan_cps/api.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 meituan_cps/api.go diff --git a/meituan_cps/api.go b/meituan_cps/api.go new file mode 100644 index 0000000..a39fbbc --- /dev/null +++ b/meituan_cps/api.go @@ -0,0 +1,60 @@ +package meituan_cps + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/comm" + zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils" + "crypto/hmac" + "crypto/md5" + "crypto/sha256" + "encoding/base64" + "fmt" + "strings" + "time" +) + +func Send(method, key, secret string, param map[string]interface{}) { + header := map[string]string{ + "S-Ca-App": key, + "S-Ca-Timestamp": zhios_third_party_utils.Int64ToStr(time.Now().Unix() * 1000), + } + url := "https://media.meituan.com/cps_open/common/api/v1/" + paramStr := comm.GetSortJson(param) + paramStr = strings.ReplaceAll(paramStr, "\n", "") + header = GetSign(method, secret, paramStr, header) + fmt.Println(header) + fmt.Println(paramStr) + + post, err := zhios_third_party_utils.CurlPost(url+method, paramStr, header) + fmt.Println(string(post)) + fmt.Println(err) +} + +func GetSign(method, secret string, paramStr string, header map[string]string) map[string]string { + headers := comm.KsortToStr(header) + + HTTPMethod := "POST" + h1 := md5.New() + h1.Write([]byte(paramStr)) + ContentMd5 := base64.StdEncoding.EncodeToString(h1.Sum(nil)) + fmt.Println(ContentMd5) + headerStr := "" + signHeader := "" + for _, v := range headers { + headerStr += v + ":" + header[v] + "\n" + if signHeader == "" { + signHeader += v + } else { + signHeader += "," + v + } + } + + Url := "/cps_open/common/api/v1/" + method + sign := HTTPMethod + "\n" + ContentMd5 + "\n" + headerStr + Url + h := hmac.New(sha256.New, []byte(secret)) + _, _ = h.Write([]byte(sign)) + header["S-Ca-Signature"] = base64.StdEncoding.EncodeToString(h.Sum(nil)) + header["Content-MD5"] = ContentMd5 + header["S-Ca-Signature-Headers"] = signHeader + header["Content-Type"] = "application/json; charset=utf-8" + return header +}