diff --git a/hw_365/api.go b/hw_365/api.go new file mode 100644 index 0000000..818f29e --- /dev/null +++ b/hw_365/api.go @@ -0,0 +1,57 @@ +package hw_365 + +import ( + zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils" + "github.com/syyongx/php2go" + "github.com/tidwall/gjson" + "strings" +) + +func Send(url string, param map[string]string, secret string) string { + sign := getSign(param, secret) + param["sign"] = sign + for k, v := range param { + if v == "" { + continue + } + if strings.Contains(url, "?") == false { + url += "?" + k + "=" + php2go.URLEncode(v) + } else { + url += "&" + k + "=" + php2go.URLEncode(v) + } + } + return url +} + +func SendOrder(url string, param map[string]string, secret string) string { + sign := getSign(param, secret) + param["sign"] = sign + for k, v := range param { + if v == "" { + continue + } + if strings.Contains(url, "?") == false { + url += "?" + k + "=" + php2go.URLEncode(v) + } else { + url += "&" + k + "=" + php2go.URLEncode(v) + } + } + data, _ := zhios_third_party_utils.CurlGet(url, nil) + list := gjson.Get(string(data), "data.list").String() + if list == "" { + list = gjson.Get(string(data), "data.order").String() + } + return list +} +func getSign(param map[string]string, secret string) string { + strMap := zhios_third_party_utils.KsortToStr(param) + str := "" + for _, v := range strMap { + if v == "sign" || param[v] == "" { + continue + } + str += zhios_third_party_utils.UderscoreToUpperCamelCase(v) + "=" + param[v] + "&" + } + str += "secretKey=" + secret + return strings.ToUpper(php2go.Md5(str)) +} diff --git a/utils/string.go b/utils/string.go index 190b97e..aba3e94 100644 --- a/utils/string.go +++ b/utils/string.go @@ -201,3 +201,18 @@ func ShowSubstr(s string, l int) string { } return ss } +func KsortToStr(params map[string]string) []string { + keys := make([]string, len(params)) + i := 0 + for k, _ := range params { + keys[i] = k + i++ + } + sort.Strings(keys) + return keys +} +func UderscoreToUpperCamelCase(s string) string { + s = strings.Replace(s, "_", " ", -1) + s = strings.Title(s) + return strings.Replace(s, " ", "", -1) +}