第三方api接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.go 1.8 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package meituan_cps
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/comm"
  4. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  5. "crypto/hmac"
  6. "crypto/md5"
  7. "crypto/sha256"
  8. "encoding/base64"
  9. "fmt"
  10. "strings"
  11. "time"
  12. )
  13. func Send(method, key, secret string, param map[string]interface{}) (string, error) {
  14. header := map[string]string{
  15. "S-Ca-App": key,
  16. "S-Ca-Timestamp": zhios_third_party_utils.Int64ToStr(time.Now().Unix() * 1000),
  17. }
  18. url := "https://media.meituan.com/cps_open/common/api/v1/"
  19. paramStr := comm.GetSortJson(param)
  20. paramStr = strings.ReplaceAll(paramStr, "\n", "")
  21. header = GetSign(method, secret, paramStr, header)
  22. fmt.Println(header)
  23. fmt.Println(paramStr)
  24. post, err := zhios_third_party_utils.CurlPost(url+method, paramStr, header)
  25. fmt.Println(string(post))
  26. fmt.Println(err)
  27. return string(post), err
  28. }
  29. func GetSign(method, secret string, paramStr string, header map[string]string) map[string]string {
  30. headers := comm.KsortToStr(header)
  31. HTTPMethod := "POST"
  32. h1 := md5.New()
  33. h1.Write([]byte(paramStr))
  34. ContentMd5 := base64.StdEncoding.EncodeToString(h1.Sum(nil))
  35. fmt.Println(ContentMd5)
  36. headerStr := ""
  37. signHeader := ""
  38. for _, v := range headers {
  39. headerStr += v + ":" + header[v] + "\n"
  40. if signHeader == "" {
  41. signHeader += v
  42. } else {
  43. signHeader += "," + v
  44. }
  45. }
  46. Url := "/cps_open/common/api/v1/" + method
  47. sign := HTTPMethod + "\n" + ContentMd5 + "\n" + headerStr + Url
  48. h := hmac.New(sha256.New, []byte(secret))
  49. _, _ = h.Write([]byte(sign))
  50. header["S-Ca-Signature"] = base64.StdEncoding.EncodeToString(h.Sum(nil))
  51. header["Content-MD5"] = ContentMd5
  52. header["S-Ca-Signature-Headers"] = signHeader
  53. header["Content-Type"] = "application/json; charset=utf-8"
  54. return header
  55. }