第三方api接口
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

api.go 2.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package tik_tok
  2. import (
  3. "bytes"
  4. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  5. "crypto/hmac"
  6. "crypto/sha256"
  7. "encoding/hex"
  8. "encoding/json"
  9. "github.com/syyongx/php2go"
  10. "sort"
  11. "strings"
  12. "time"
  13. )
  14. func Send(appkey, appSecret, method string, params map[string]interface{}, acctoken string) (string, error) {
  15. methodPath := strings.ReplaceAll(method, ".", "/")
  16. url := "https://openapi-fxg.jinritemai.com/" + methodPath
  17. paramJson := GetSortJson(params)
  18. paramJson = strings.ReplaceAll(paramJson, "\n", "")
  19. param := map[string]string{
  20. "app_key": appkey,
  21. "method": method,
  22. "param_json": paramJson,
  23. "timestamp": zhios_third_party_utils.Int64ToStr(time.Now().Unix()),
  24. "v": "2",
  25. }
  26. sign := GetSign(appSecret, param)
  27. if acctoken != "" {
  28. param["access_token"] = acctoken
  29. }
  30. param["sign"] = sign
  31. param["sign_method"] = "hmac-sha256"
  32. for k, v := range param {
  33. if strings.Contains(url, "?") {
  34. url += "&" + k + "=" + php2go.URLEncode(v)
  35. } else {
  36. url += "?" + k + "=" + php2go.URLEncode(v)
  37. }
  38. }
  39. data, err := zhios_third_party_utils.CurlPost(url, paramJson, nil)
  40. return string(data), err
  41. }
  42. func GetSign(appSecret string, param map[string]string) string {
  43. str := ""
  44. keys := KsortToStr(param)
  45. for _, k := range keys {
  46. str += k + param[k]
  47. }
  48. signStr := appSecret + str + appSecret
  49. h := hmac.New(sha256.New, []byte(appSecret))
  50. _, _ = h.Write([]byte(signStr))
  51. return hex.EncodeToString(h.Sum(nil))
  52. }
  53. func Ksort(params map[string]interface{}) []string {
  54. keys := make([]string, len(params))
  55. i := 0
  56. for k, _ := range params {
  57. keys[i] = k
  58. i++
  59. }
  60. sort.Strings(keys)
  61. return keys
  62. }
  63. func KsortToStr(params map[string]string) []string {
  64. keys := make([]string, len(params))
  65. i := 0
  66. for k, _ := range params {
  67. keys[i] = k
  68. i++
  69. }
  70. sort.Strings(keys)
  71. return keys
  72. }
  73. //func GetSortJson(params map[string]interface{}) string {
  74. // // 排序
  75. // keys := Ksort(params)
  76. // for _, k := range keys {
  77. // fmt.Println(k)
  78. // }
  79. //
  80. // byteBuf := bytes.NewBuffer([]byte{})
  81. // encoder := json.NewEncoder(byteBuf)
  82. // // 特殊字符不转义
  83. // encoder.SetEscapeHTML(false)
  84. // err := encoder.Encode(params)
  85. // if err != nil {
  86. // panic(err)
  87. // }
  88. // data := byteBuf.String()
  89. // return data
  90. //}
  91. // Marshal 序列化参数
  92. func GetSortJson(o interface{}) string {
  93. // 序列化一次
  94. raw, _ := json.Marshal(o)
  95. // 反序列化为map
  96. m := make(map[string]interface{})
  97. reader := bytes.NewReader(raw)
  98. decode := json.NewDecoder(reader)
  99. decode.UseNumber()
  100. _ = decode.Decode(&m)
  101. // 重新做一次序列化,并禁用Html Escape
  102. buffer := bytes.NewBufferString("")
  103. encoder := json.NewEncoder(buffer)
  104. encoder.SetEscapeHTML(false)
  105. _ = encoder.Encode(m)
  106. marshal := strings.TrimSpace(buffer.String()) // Trim掉末尾的换行符
  107. return marshal
  108. }