forked from rosbit/go-wx-api
/
qrcode-api.go
82 lines (71 loc) · 2.61 KB
/
qrcode-api.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
package wxtools
import (
"fmt"
"encoding/json"
"github.com/donpools/go-wx-api/auth"
)
func CreateTempQrIntScene(accessToken string, sceneId int, expireInSec int) (ticketURL2ShowQrCode, urlIncluedInQrcode string, err error) {
ticketURL2ShowQrCode, urlIncluedInQrcode, err = createTempQr(accessToken, sceneId, expireInSec, "QR_SCENE", "scene_id")
return
}
func CreateTempQrStrScene(accessToken, sceneId string, expireInSec int) (ticketURL2ShowQrCode, urlIncluedInQrcode string, err error) {
ticketURL2ShowQrCode, urlIncluedInQrcode, err = createTempQr(accessToken, sceneId, expireInSec, "QR_STR_SCENE", "scene_str")
return
}
func createTempQr(accessToken string, sceneId interface{}, expireInSec int, action, idName string) (string, string, error) {
params := map[string]interface{}{
"expire_seconds": expireInSec,
"action_name": action,
"action_info": map[string]interface{}{
"scene": map[string]interface{}{
idName : sceneId,
},
},
}
return createQr(accessToken, params)
}
func CreateQrIntScene(accessToken string, sceneId int) (ticketURL2ShowQrCode, urlIncluedInQrcode string, err error) {
ticketURL2ShowQrCode, urlIncluedInQrcode, err = createForeverQr(accessToken, sceneId, "QR_LIMIT_SCENE", "scene_id")
return
}
func CreateQrStrScene(accessToken, sceneId string) (ticketURL2ShowQrCode, urlIncluedInQrcode string, err error) {
ticketURL2ShowQrCode, urlIncluedInQrcode, err = createForeverQr(accessToken, sceneId, "QR_LIMIT_STR_SCENE", "scene_str")
return
}
func createForeverQr(accessToken string, sceneId interface{}, action, idName string) (string, string, error) {
params := map[string]interface{}{
"action_name": action,
"action_info": map[string]interface{}{
"scene": map[string]interface{}{
idName: sceneId,
},
},
}
return createQr(accessToken, params)
}
func createQr(accessToken string, params map[string]interface{}) (ticketURL2ShowQrCode, urlIncluedInQrcode string, err error) {
url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s", accessToken)
var resp []byte
if resp, err = wxauth.JsonCall(url, "POST", params); err != nil {
return
}
var res struct {
Ticket string `json:"ticket"`
ExpireSeconds int `json:"expire_seconds"`
Url string `json:"url"`
}
if err = json.Unmarshal(resp, &res); err != nil {
return
}
if res.Ticket == "" {
err = fmt.Errorf("no ticket item found in result")
return
}
ticketURL2ShowQrCode = fmt.Sprintf("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s", res.Ticket)
if res.Url == "" {
err = fmt.Errorf("no url item found in result")
return
}
urlIncluedInQrcode = res.Url
return
}