-
Notifications
You must be signed in to change notification settings - Fork 156
/
client.go
62 lines (46 loc) · 1.54 KB
/
client.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
package url
import (
"context"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/basicService/url/response"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"math"
)
const DAY int = 86400
type Client struct {
BaseClient *kernel.BaseClient
BaseUri string
}
func NewClient(app *kernel.ApplicationInterface) (*Client, error) {
baseClient, err := kernel.NewBaseClient(app, nil)
if err != nil {
return nil, err
}
client := &Client{
baseClient,
"",
}
client.BaseClient.BaseURI = "https://api.weixin.qq.com/"
return client, nil
}
// 短key托管
// https://developers.weixin.qq.com/doc/offiaccount/Account_Management/KEY_Shortener.html
func (comp *Client) ShortGenKey(ctx context.Context, longData string, expireSecond int) (*response.ResponseShortGenKey, error) {
result := &response.ResponseShortGenKey{}
params := &object.HashMap{
"long_data": longData,
"expire_seconds": int(math.Min(float64(expireSecond), float64(30*DAY))),
}
_, err := comp.BaseClient.HttpPostJson(ctx, "cgi-bin/shorten/gen", params, nil, nil, result)
return result, err
}
// 获取端链接
// https://developers.weixin.qq.com/doc/offiaccount/Account_Management/KEY_Shortener.html
func (comp *Client) FetchShorten(ctx context.Context, shortKey string) (*response.ResponseFetchShorten, error) {
result := &response.ResponseFetchShorten{}
params := &object.HashMap{
"short_key": shortKey,
}
_, err := comp.BaseClient.HttpPostJson(ctx, "cgi-bin/shorten/fetch", params, nil, nil, result)
return result, err
}