-
Notifications
You must be signed in to change notification settings - Fork 15
/
helper.go
69 lines (61 loc) · 1.21 KB
/
helper.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
package tiktok
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"net/url"
"sort"
)
func generateSHA256(path string, queries url.Values, secret string) string {
keys := make([]string, len(queries))
idx := 0
for k := range queries {
keys[idx] = k
idx++
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
input := path
for _, key := range keys {
input = input + key + queries.Get(key)
}
input = secret + input + secret
h := hmac.New(sha256.New, []byte(secret))
if _, err := h.Write([]byte(input)); err != nil {
return ""
}
return hex.EncodeToString(h.Sum(nil))
}
func safeGet(param url.Values, key string) string {
if param == nil {
return ""
}
vs, ok := param[key]
if !ok || len(vs) == 0 {
return ""
}
return vs[0]
}
func CheckEmpty(vals ...string) bool {
for _, v := range vals {
if v == "" {
return true
}
}
return false
}
type Param struct {
AccessToken string `validate:"required"`
ShopID string `validate:"required"`
}
func (c *Client) params(p Param) (param url.Values, err error) {
err = c.validate.Struct(p)
if err != nil {
return
}
param = url.Values{}
param.Set("access_token", p.AccessToken)
param.Set("shop_id", p.ShopID)
return
}