-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataoke.go
89 lines (72 loc) · 1.98 KB
/
dataoke.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
81
82
83
84
85
86
87
88
89
package dataoke
import (
"fmt"
"strings"
"time"
"github.com/lalifeier/vvgo-mall/pkg/util/convert"
"github.com/lalifeier/vvgo-mall/pkg/util/crypto/md5"
"github.com/lalifeier/vvgo-mall/pkg/util/random"
"github.com/lalifeier/vvgo-mall/pkg/util/request"
)
var defaultClientOptions = clientOptions{
Version: "v2.0.0",
BaseUrl: "https://openapi.dataoke.com/api",
}
type ClientOption func(*clientOptions)
type clientOptions struct {
Version string
BaseUrl string
}
func WithVersion(v string) ClientOption {
return func(o *clientOptions) {
o.Version = v
}
}
// type Client interface {
// GenerateSign(params map[string]string) map[string]string
// Get(url string, params map[string]string) (string, error)
// CarouseList(params map[string]string) (string, error)
// }
type Client struct {
appKey string
appSecret string
version string
baseUrl string
}
func NewClient(appKey, appSecret string, opts ...ClientOption) *Client {
options := defaultClientOptions
for _, o := range opts {
o(&options)
}
return &Client{
appKey: appKey,
appSecret: appSecret,
version: options.Version,
baseUrl: options.BaseUrl,
}
}
// appKey
// appsecret
// nonce:一个6位的随机数
// timer:毫秒级时间戳
func (c *Client) GenerateSign(params map[string]string) map[string]string {
if _, ok := params["appKey"]; !ok {
params["appKey"] = c.appKey
}
if _, ok := params["appSecret"]; !ok {
params["key"] = c.appSecret
}
if _, ok := params["version"]; !ok {
params["version"] = c.version
}
params["nonce"] = random.GenerateNumber(6)
params["timer"] = convert.Int64ToString(time.Now().Unix() * 1000)
data := fmt.Sprintf("appKey=%s&timer=%s&nonce=%s&key=%s", params["appKey"], params["timer"], params["nonce"], params["key"])
params["signRan"] = strings.ToUpper(md5.Encode(data))
return params
}
func (c *Client) Get(url string, params map[string]string) (string, error) {
params = c.GenerateSign(params)
url = c.baseUrl + url
return request.Get(url, params, nil)
}