优化签名生成函数
This commit is contained in:
parent
bc8c7d1e12
commit
9000c53f0e
|
@ -62,30 +62,22 @@ func Md5(text string) string {
|
||||||
// @param privateKey
|
// @param privateKey
|
||||||
// @return string
|
// @return string
|
||||||
func MakeSign(obj interface{}, privateKey string) string {
|
func MakeSign(obj interface{}, privateKey string) string {
|
||||||
signmap := map[string]interface{}{}
|
|
||||||
valueOfObj := reflect.ValueOf(obj)
|
valueOfObj := reflect.ValueOf(obj)
|
||||||
for i := 0; i < valueOfObj.NumField(); i++ {
|
keysCount := valueOfObj.NumField()
|
||||||
signmap[valueOfObj.Type().Field(i).Name] = valueOfObj.Field(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
//进行键排序
|
keys := make([]string, keysCount)
|
||||||
keys := make([]string, len(signmap))
|
for i := 0; i < keysCount; i++ {
|
||||||
j := 0
|
keys[i] = valueOfObj.Type().Field(i).Name
|
||||||
for k := range signmap {
|
|
||||||
keys[j] = k
|
|
||||||
j++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings(keys)
|
sort.Strings(keys)
|
||||||
//获取 value 值,拼接成一行
|
|
||||||
str := ""
|
str := ""
|
||||||
for _, k := range keys {
|
currentKey := ""
|
||||||
if strings.ToLower(k) == "sign" {
|
var currentValue reflect.Value
|
||||||
continue
|
for j := 0; j < keysCount; j++ {
|
||||||
}
|
currentKey = strings.ToLower(keys[j])
|
||||||
if v, ok := signmap[k]; ok {
|
currentValue = valueOfObj.FieldByName(keys[j])
|
||||||
str = fmt.Sprintf("%s%s=%v&", str, strings.ToLower(k), v)
|
str = fmt.Sprintf("%s%s=%v&", str, currentKey, currentValue)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Md5(str + "key=" + privateKey)
|
return Md5(str + "key=" + privateKey)
|
||||||
|
|
Loading…
Reference in New Issue