-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathparams.go
80 lines (71 loc) · 1.36 KB
/
params.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package pdd
import (
"strconv"
"time"
"sort"
"net/url"
"encoding/json"
)
// pdd params
type Params map[string]interface{}
func NewParams() Params {
p := make(Params)
return p
}
func NewParamsWithType(_type MethodType, params ...Params) Params {
p := make(Params)
p["type"] = _type
p["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
for _, v := range params {
p.SetParams(v)
}
return p
}
func (p Params) Sign(context *Context) {
p["client_id"] = context.ClientId
// 排序所有的 key
var keys []string
for key := range p {
keys = append(keys, key)
}
sort.Strings(keys)
signStr := context.ClientSecret
for _, key := range keys {
signStr += key + getString(p[key])
}
signStr += context.ClientSecret
p["sign"] = createSign(signStr)
}
func (p Params) Set(key string, value interface{}) {
p[key] = value
}
func (p Params) SetParams(params Params) {
for key, value := range params {
p[key] = value
}
}
func (p Params) GetQuery() string {
u := url.Values{}
for k, v := range p {
u.Set(k, getString(v))
}
return u.Encode()
}
func getString(i interface{}) string {
switch v := i.(type) {
case string:
return v
case []byte:
return string(v)
case int:
return strconv.Itoa(v)
case MethodType:
return string(v)
case bool:
return strconv.FormatBool(v)
default:
bytes, _ := json.Marshal(v)
return string(bytes)
}
return ""
}