-
Notifications
You must be signed in to change notification settings - Fork 35
/
qrcode.go
55 lines (47 loc) · 2.16 KB
/
qrcode.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
package weixin
import (
"fmt"
"net/url"
)
// 二维码
const (
AccountCreateQRCodeURL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s"
AccountGetQRCodeImgAddrURL = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s"
)
// QRCodeTicket 二维码ticket
type QRCodeTicket struct {
WXError
Ticket string `json:"ticket"` // 获取的二维码ticket,凭借此ticket可以在有效时间内换取二维码。
ExpireSeconds int `json:"expire_seconds"` // 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天)。
URL string `json:"url"` // 二维码图片解析后的地址,开发者可根据该地址自行生成需要的二维码图片
}
// CreateTemporaryQRCodeTicket 创建临时二维码
func CreateTemporaryQRCodeTicket(sceneId int, expireSeconds ...int) (ticket *QRCodeTicket, err error) {
if len(expireSeconds) == 0 {
expireSeconds = []int{30}
} else if expireSeconds[0] >= 2592000 || expireSeconds[0] < 0 {
expireSeconds[0] = 2592000
}
body := fmt.Sprintf(`{"expire_seconds": %d, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": %d}}}`, expireSeconds[0], sceneId)
return createQRCodeTicket(body)
}
// CreatePermanentQRCodeTicket 创建永久二维码
func CreatePermanentQRCodeTicket(sceneId int) (ticket *QRCodeTicket, err error) {
body := fmt.Sprintf(`{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_str": %d}}}`, sceneId)
return createQRCodeTicket(body)
}
// CreatePermanentQRCodeTicketString 创建字符串形式的二维码
func CreatePermanentQRCodeTicketString(sceneId string) (ticket *QRCodeTicket, err error) {
body := fmt.Sprintf(`{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "%s"}}}`, sceneId)
return createQRCodeTicket(body)
}
func createQRCodeTicket(js string) (ticket *QRCodeTicket, err error) {
ticket = &QRCodeTicket{}
url := fmt.Sprintf(AccountCreateQRCodeURL, AccessToken())
err = Post(url, []byte(js), ticket)
return ticket, err
}
// GetQRCodeImg 通过ticket换取二维码
func GetQRCodeImg(ticket string) string {
return fmt.Sprintf(AccountGetQRCodeImgAddrURL, url.QueryEscape(ticket))
}