-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini_program.go
126 lines (115 loc) · 3.18 KB
/
mini_program.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package miniprogram
import (
"encoding/json"
"fmt"
"github.com/parnurzeal/gorequest"
"io"
"net/http"
"os"
"sync"
"time"
)
type MiniProgramInterface interface {
GetSession(code string) (*Session, error)
Decode(encryptedData, iv string, v interface{}) error
GetWXacodeunLimit(scene, page string, width int, isHyaline bool, filePath string) error
GetWXacodeunLimitWriter(scene, page string, width int, isHyaline bool, writer io.Writer)
GetAccessToken() (string, error)
}
type MiniProgramImpl struct {
mu sync.Mutex
AccessToken string
Expires int64
AppId string
AppSecret string
}
func NewMiniProgramImpl() *MiniProgramImpl {
return &MiniProgramImpl{}
}
func (m *MiniProgramImpl) AddAppId(appId string) *MiniProgramImpl {
m.AppId = appId
return m
}
func (m *MiniProgramImpl) AddAppSecret(secret string) *MiniProgramImpl {
m.AppSecret = secret
return m
}
func (m *MiniProgramImpl) GetSession(code string) (*Session, error) {
s := &Session{}
resp, body, errs := gorequest.New().Get(fmt.Sprintf(JsCode2SessionUrl,
m.AppId, m.AppSecret, code)).EndStruct(s)
if errs != nil {
return nil, fmt.Errorf("get session error %v", errs)
}
if len(s.OpenId) == 0 {
return nil, fmt.Errorf("get session error %s body %s", resp.Status, body)
}
return s, nil
}
func (m *MiniProgramImpl) Decode(encryptedData, iv string, session *Session, v interface{}) error {
wxBizDataCrypt := WxBizDataCrypt{m.AppId, session.SessionKey}
j, err := wxBizDataCrypt.Decrypt(encryptedData, iv, true)
if err != nil {
return err
}
s, _ := j.(string)
err = json.Unmarshal([]byte(s), v)
if err != nil {
return err
}
return nil
}
func (m *MiniProgramImpl) GetWXacodeunLimitToFile(scene, page string, width int, isHyaline bool, filePath string) error {
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
return m.GetWXacodeunLimitWriter(scene, page, width, isHyaline, f)
}
func (m *MiniProgramImpl) GetWXacodeunLimitWriter(scene, page string, width int, isHyaline bool, writer io.Writer) error {
token, err := m.GetAccessToken()
if err != nil {
return err
}
s := &QrCode{
Scene: scene,
Page: page,
Width: width,
IsHyaline: isHyaline,
}
var errs []error
resp, body, errs := gorequest.New().Post(fmt.Sprintf(GetWxacodeunLimitUrl, token)).
Retry(3, 5*time.Second, http.StatusBadRequest, http.StatusInternalServerError).
SendStruct(s).
End()
if errs != nil {
return fmt.Errorf("get qrcode error %v", errs)
}
if len(body) < 1000 {
return fmt.Errorf("get qrcode error %s body %s", resp.Status, body)
}
_, err = writer.Write([]byte(body))
return err
}
func (m *MiniProgramImpl) GetAccessToken() (string, error) {
m.mu.Lock()
defer m.mu.Unlock()
if len(m.AccessToken) > 0 {
if time.Now().Unix() < m.Expires {
return m.AccessToken, nil
}
}
ac := &AccessToken{}
resp, body, errs := gorequest.New().Get(fmt.Sprintf(GetAccessTokenUrl,
m.AppId, m.AppSecret)).EndStruct(ac)
if errs != nil {
return "", fmt.Errorf("%v", errs)
}
if len(ac.Token) == 0 {
return "", fmt.Errorf("getAccessToken error %ds body %s", resp.Status, body)
}
m.AccessToken = ac.Token
m.Expires = time.Now().Unix() + int64(ac.ExpiresIn)
return m.AccessToken, nil
}