package kuaishou

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"
	"fmt"
	"github.com/syyongx/php2go"
	"strings"
	"time"
)

func SendGet(method string, signSecret string, param map[string]string) (string, error) {
	url := "https://openapi.kwaixiaodian.com/" + strings.ReplaceAll(method, ".", "/")
	param["signMethod"] = "MD5"
	param["method"] = method
	param["version"] = "1"
	param["timestamp"] = zhios_third_party_utils.Int64ToStr(time.Now().UnixNano() / 1e6)
	param["sign"] = GetSign(param, signSecret)
	for k, v := range param {
		if strings.Contains(url, "?") {
			url += "&" + k + "=" + php2go.URLEncode(v)
		} else {
			url += "?" + k + "=" + php2go.URLEncode(v)
		}
	}
	get, err := zhios_third_party_utils.CurlGet(url, nil)
	fmt.Println(string(get))
	fmt.Println(url)
	fmt.Println(err)
	return string(get), err
}
func SendPost(method string, signSecret string, param map[string]string) (string, error) {
	url := "https://openapi.kwaixiaodian.com/" + strings.ReplaceAll(method, ".", "/")
	param["signMethod"] = "MD5"
	param["method"] = method
	param["sign"] = GetSign(param, signSecret)
	for k, v := range param {
		if strings.Contains(url, "?") {
			url += "&" + k + "=" + php2go.URLEncode(v)
		} else {
			url += "?" + k + "=" + php2go.URLEncode(v)
		}
	}
	get, err := zhios_third_party_utils.CurlPost(url, param["param"], nil)
	fmt.Println(string(get))
	fmt.Println(err)
	return string(get), err
}
func GetSign(param map[string]string, signSecret string) string {
	str := ""
	keys := comm.KsortToStr(param)
	for _, k := range keys {
		if str != "" {
			str += "&" + k + "=" + param[k]
		} else {
			str += k + "=" + param[k]
		}
	}
	str += "&signSecret=" + signSecret
	return php2go.Md5(str)
}