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

120 lines
3.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, _ := GetSend("/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, _ := GetSend("/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, _ := GetSend("/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 PostSend(method, key, secret string, param map[string]interface{}) (string, error) {
  71. paramStr := comm.GetSortJson(param)
  72. param["Timestamp"] = time.Now().Unix()
  73. param["App-Key"] = key
  74. param["Sign"] = GetSign(param, secret)
  75. headers := map[string]string{
  76. "Timestamp": zhios_third_party_utils.Int64ToStr(zhios_third_party_utils.AnyToInt64(param["Timestamp"])),
  77. "App-Key": zhios_third_party_utils.AnyToString(param["App-Key"]),
  78. "Sign": zhios_third_party_utils.AnyToString(param["Sign"]),
  79. }
  80. urls := "https://union.didi.cn" + method
  81. post, err := zhios_third_party_utils.CurlPost(urls, paramStr, headers)
  82. fmt.Println(string(post))
  83. fmt.Println(err)
  84. return string(post), err
  85. }
  86. func GetSign(params map[string]interface{}, accessKey string) string {
  87. // key排序
  88. arr := sort.StringSlice{}
  89. for k := range params {
  90. if k != "sign" {
  91. arr = append(arr, k)
  92. }
  93. }
  94. arr.Sort()
  95. // 参数拼接
  96. var build strings.Builder
  97. for idx, k := range arr {
  98. if idx != 0 {
  99. build.WriteString("&")
  100. }
  101. build.WriteString(fmt.Sprintf("%s=%v", k, params[k]))
  102. }
  103. build.WriteString(accessKey)
  104. // URL encode
  105. sourceStr := url.QueryEscape(build.String())
  106. // sha1加密
  107. h := sha1.New()
  108. _, _ = io.WriteString(h, sourceStr)
  109. shaStr := hex.EncodeToString(h.Sum([]byte("")))
  110. // 返回base64字符串
  111. b64Str := base64.StdEncoding.EncodeToString([]byte(shaStr))
  112. // base64字符串含有=和/,再一次URL encode
  113. return url.QueryEscape(b64Str)
  114. }