forked from shenghui0779/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popularize.go
100 lines (82 loc) · 2.67 KB
/
popularize.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
90
91
92
93
94
95
96
97
98
99
100
package offia
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/url"
"github.com/liniu/gochat/urls"
"github.com/liniu/gochat/wx"
)
type QRCodeAction string
const (
QRScene QRCodeAction = "QR_SCENE" // 临时的整型参数值
QRStrScene QRCodeAction = "QR_STR_SCENE" // 临时的字符串参数值
QRLimitScene QRCodeAction = "QR_LIMIT_SCENE" // 永久的整型参数值
QRLimitStrScene QRCodeAction = "QR_LIMIT_STR_SCENE" // 永久的字符串参数值
)
type QRCodeScene struct {
SceneID int `json:"scene_id,omitempty"`
SceneStr string `json:"scene_str,omitempty"`
}
type QRCodeActionInfo struct {
Scene *QRCodeScene `json:"scene"`
}
type ParamsQRCodeCreate struct {
ActionName QRCodeAction `json:"action_name"`
ActionInfo *QRCodeActionInfo `json:"action_info"`
ExpireSeconds int `json:"expire_seconds,omitempty"`
}
type ResultQRCodeCreate struct {
Ticket string `json:"ticket"`
ExpireSeconds int64 `json:"expire_seconds"`
URL string `json:"url"`
}
// CreateQRCode 帐号管理 - 生成带参数的二维码(expireSeconds:二维码有效时间,最大不超过2592000秒(即30天),不填,则默认有效期为30秒。)
func CreateQRCode(params *ParamsQRCodeCreate, result *ResultQRCodeCreate) wx.Action {
return wx.NewPostAction(urls.OffiaQRCodeCreate,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(params)
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// ShowQRCode 通过 ticket 换取二维码 (base64)
func ShowQRCode(ctx context.Context, ticket string) (string, error) {
resp, err := wx.HTTPGet(ctx, fmt.Sprintf("%s?ticket=%s", urls.OffiaQRCodeShow, url.QueryEscape(ticket)))
if err != nil {
return "", err
}
buf := bytes.NewBuffer(make([]byte, 0, 20<<10)) // 20kb
if _, err = io.Copy(buf, bytes.NewReader(resp)); err != nil {
return "", nil
}
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}
type ParamsShortURL struct {
Action string `json:"action"`
LongURL string `json:"long_url"`
}
// ResultShortURL 短链接
type ResultShortURL struct {
ShortURL string `json:"short_url"`
}
// ShortURL 帐号管理 - 长链接转短链接(长链接支持http://、https://、weixin://wxpay格式的url)
func ShortURL(longURL string, result *ResultShortURL) wx.Action {
params := &ParamsShortURL{
Action: "long2short",
LongURL: longURL,
}
return wx.NewPostAction(urls.OffiaShortURLGenerate,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(params)
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}