package didi 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/sha1" "encoding/base64" "encoding/hex" "encoding/json" "fmt" "github.com/tidwall/gjson" "io" "net/url" "sort" "strings" "time" ) func GetDidiUrl(key, secret string, param map[string]interface{}) map[string]string { send, _ := PostSend("/openapi/v1.0/link/generate", key, secret, param) res := map[string]string{ "app_id": gjson.Get(send, "data.app_id").String(), "app_source": gjson.Get(send, "data.app_source").String(), "dsi": gjson.Get(send, "data.dsi").String(), "link": gjson.Get(send, "data.link").String(), } return res } func GetDidiQrcode(key, secret string, param map[string]interface{}) map[string]string { send, _ := GetSend("/openapi/v1.0/code/generate", key, secret, param) res := map[string]string{ "code_link": gjson.Get(send, "data.code_link").String(), } return res } func GetDidiPoster(key, secret string, param map[string]interface{}) map[string]string { send, _ := GetSend("/openapi/v1.0/poster/generate", key, secret, param) res := map[string]string{ "poster_link": gjson.Get(send, "data.poster_link").String(), } return res } func GetDidiOrder(key, secret string, param map[string]interface{}) []DidiOrder { send, _ := GetSend("/openapi/v1.0/order/list", key, secret, param) list := make([]DidiOrder, 0) json.Unmarshal([]byte(gjson.Get(send, "data.order_list").String()), &list) return list } func GetSend(method, key, secret string, param map[string]interface{}) (string, error) { param["Timestamp"] = zhios_third_party_utils.Int64ToStr(time.Now().Unix()) param["App-Key"] = key param["Sign"] = GetSign(param, secret) headers := map[string]string{ "Timestamp": zhios_third_party_utils.AnyToString(param["Timestamp"]), "App-Key": zhios_third_party_utils.AnyToString(param["App-Key"]), "Sign": zhios_third_party_utils.AnyToString(param["Sign"]), } urls := "https://union.didi.cn" + method for k, v := range param { if strings.Contains(urls, "?") == false { urls += "?" + k + "=" + zhios_third_party_utils.AnyToString(v) } else { urls += "&" + k + "=" + zhios_third_party_utils.AnyToString(v) } } fmt.Println(urls) post, err := zhios_third_party_utils.CurlGet(urls, headers) fmt.Println(string(post)) fmt.Println(err) return string(post), err } func PostSend(method, key, secret string, param map[string]interface{}) (string, error) { paramStr := comm.GetSortJson(param) param["Timestamp"] = time.Now().Unix() param["App-Key"] = key param["Sign"] = GetSign(param, secret) headers := map[string]string{ "Timestamp": zhios_third_party_utils.Int64ToStr(zhios_third_party_utils.AnyToInt64(param["Timestamp"])), "App-Key": zhios_third_party_utils.AnyToString(param["App-Key"]), "Sign": zhios_third_party_utils.AnyToString(param["Sign"]), } urls := "https://union.didi.cn" + method post, err := zhios_third_party_utils.CurlPost(urls, paramStr, headers) fmt.Println(string(post)) fmt.Println(err) return string(post), err } func GetSign(params map[string]interface{}, accessKey string) string { // key排序 arr := sort.StringSlice{} for k := range params { if k != "sign" { arr = append(arr, k) } } arr.Sort() // 参数拼接 var build strings.Builder for idx, k := range arr { if idx != 0 { build.WriteString("&") } build.WriteString(fmt.Sprintf("%s=%v", k, params[k])) } build.WriteString(accessKey) // URL encode sourceStr := url.QueryEscape(build.String()) // sha1加密 h := sha1.New() _, _ = io.WriteString(h, sourceStr) shaStr := hex.EncodeToString(h.Sum([]byte(""))) // 返回base64字符串 b64Str := base64.StdEncoding.EncodeToString([]byte(shaStr)) // base64字符串含有=和/,再一次URL encode return url.QueryEscape(b64Str) }