第三方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.

149 lines
4.8 KiB

  1. package didi
  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/sha1"
  6. "encoding/base64"
  7. "encoding/hex"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/tidwall/gjson"
  11. "io"
  12. "net/url"
  13. "sort"
  14. "strings"
  15. "time"
  16. )
  17. func GetDidiUrl(key, secret string, param map[string]interface{}) map[string]string {
  18. send, _ := PostSend("/openapi/v1.0/link/generate", key, secret, param)
  19. res := map[string]string{
  20. "app_id": gjson.Get(send, "data.app_id").String(),
  21. "app_source": gjson.Get(send, "data.app_source").String(),
  22. "dsi": gjson.Get(send, "data.dsi").String(),
  23. "link": gjson.Get(send, "data.link").String(),
  24. }
  25. return res
  26. }
  27. func GetDidiQrcode(key, secret string, param map[string]interface{}) map[string]string {
  28. send, _ := GetSendJson("/openapi/v1.0/code/generate", key, secret, param)
  29. res := map[string]string{
  30. "code_link": gjson.Get(send, "data.code_link").String(),
  31. }
  32. return res
  33. }
  34. func GetDidiPoster(key, secret string, param map[string]interface{}) map[string]string {
  35. send, _ := GetSendJson("/openapi/v1.0/poster/generate", key, secret, param)
  36. res := map[string]string{
  37. "poster_link": gjson.Get(send, "data.poster_link").String(),
  38. }
  39. return res
  40. }
  41. func GetDidiOrder(key, secret string, param map[string]interface{}) []DidiOrder {
  42. send, _ := GetSendJson("/openapi/v1.0/order/list", key, secret, param)
  43. list := make([]DidiOrder, 0)
  44. json.Unmarshal([]byte(gjson.Get(send, "data.order_list").String()), &list)
  45. return list
  46. }
  47. func GetSend(method, key, secret string, param map[string]interface{}) (string, error) {
  48. param["Timestamp"] = zhios_third_party_utils.Int64ToStr(time.Now().Unix())
  49. param["App-Key"] = key
  50. param["Sign"] = GetSign(param, secret)
  51. headers := map[string]string{
  52. "Timestamp": zhios_third_party_utils.AnyToString(param["Timestamp"]),
  53. "App-Key": zhios_third_party_utils.AnyToString(param["App-Key"]),
  54. "Sign": zhios_third_party_utils.AnyToString(param["Sign"]),
  55. }
  56. urls := "https://union.didi.cn" + method
  57. for k, v := range param {
  58. if strings.Contains(urls, "?") == false {
  59. urls += "?" + k + "=" + zhios_third_party_utils.AnyToString(v)
  60. } else {
  61. urls += "&" + k + "=" + zhios_third_party_utils.AnyToString(v)
  62. }
  63. }
  64. fmt.Println(urls)
  65. post, err := zhios_third_party_utils.CurlGet(urls, headers)
  66. fmt.Println(string(post))
  67. fmt.Println(err)
  68. return string(post), err
  69. }
  70. func GetSendJson(method, key, secret string, param map[string]interface{}) (string, error) {
  71. paramStr := comm.GetSortJson(param)
  72. urls := "https://union.didi.cn" + method
  73. for k, v := range param {
  74. if strings.Contains(urls, "?") == false {
  75. urls += "?" + k + "=" + zhios_third_party_utils.AnyToString(v)
  76. } else {
  77. urls += "&" + k + "=" + zhios_third_party_utils.AnyToString(v)
  78. }
  79. }
  80. param["Timestamp"] = zhios_third_party_utils.Int64ToStr(time.Now().Unix())
  81. param["App-Key"] = key
  82. param["Sign"] = GetSign(param, secret)
  83. headers := map[string]string{
  84. "Timestamp": zhios_third_party_utils.AnyToString(param["Timestamp"]),
  85. "App-Key": zhios_third_party_utils.AnyToString(param["App-Key"]),
  86. "Sign": zhios_third_party_utils.AnyToString(param["Sign"]),
  87. }
  88. fmt.Println(urls)
  89. fmt.Println(paramStr)
  90. post, err := zhios_third_party_utils.CurlGetBody(urls, paramStr, headers)
  91. fmt.Println(string(post))
  92. fmt.Println(err)
  93. return string(post), err
  94. }
  95. func PostSend(method, key, secret string, param map[string]interface{}) (string, error) {
  96. paramStr := comm.GetSortJson(param)
  97. param["Timestamp"] = time.Now().Unix()
  98. param["App-Key"] = key
  99. param["Sign"] = GetSign(param, secret)
  100. headers := map[string]string{
  101. "Timestamp": zhios_third_party_utils.Int64ToStr(zhios_third_party_utils.AnyToInt64(param["Timestamp"])),
  102. "App-Key": zhios_third_party_utils.AnyToString(param["App-Key"]),
  103. "Sign": zhios_third_party_utils.AnyToString(param["Sign"]),
  104. }
  105. urls := "https://union.didi.cn" + method
  106. post, err := zhios_third_party_utils.CurlPost(urls, paramStr, headers)
  107. fmt.Println(string(post))
  108. fmt.Println(err)
  109. return string(post), err
  110. }
  111. func GetSign(params map[string]interface{}, accessKey string) string {
  112. // key排序
  113. arr := sort.StringSlice{}
  114. for k := range params {
  115. if k != "sign" {
  116. arr = append(arr, k)
  117. }
  118. }
  119. arr.Sort()
  120. // 参数拼接
  121. var build strings.Builder
  122. for idx, k := range arr {
  123. if idx != 0 {
  124. build.WriteString("&")
  125. }
  126. build.WriteString(fmt.Sprintf("%s=%v", k, params[k]))
  127. }
  128. build.WriteString(accessKey)
  129. // URL encode
  130. sourceStr := url.QueryEscape(build.String())
  131. // sha1加密
  132. h := sha1.New()
  133. _, _ = io.WriteString(h, sourceStr)
  134. shaStr := hex.EncodeToString(h.Sum([]byte("")))
  135. // 返回base64字符串
  136. b64Str := base64.StdEncoding.EncodeToString([]byte(shaStr))
  137. // base64字符串含有=和/,再一次URL encode
  138. return url.QueryEscape(b64Str)
  139. }