-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwx.go
251 lines (240 loc) · 5.8 KB
/
wx.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package mcommon
import (
"context"
"errors"
"fmt"
"time"
"github.com/go-redis/redis"
jsoniter "github.com/json-iterator/go"
"github.com/gin-gonic/gin"
"github.com/parnurzeal/gorequest"
)
// WxJsCodeResp jscode回复
type WxJsCodeResp struct {
OpenID string `json:"openid"`
SessionKey string `json:"session_key"`
Unionid string `json:"unionid"`
Errcode int64 `json:"errcode"`
Errmsg string `json:"errmsg"`
}
// WxAppCodeResp jscode回复
type WxAppCodeResp struct {
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Openid string `json:"openid"`
Scope string `json:"scope"`
ErrCode int64 `json:"errcode"`
}
// WxAppUserInfoResp userinfo回复
type WxAppUserInfoResp struct {
Openid string `json:"openid"`
Nickname string `json:"nickname"`
Sex int64 `json:"sex"`
Province string `json:"province"`
City string `json:"city"`
Country string `json:"country"`
Headimgurl string `json:"headimgurl"`
Unionid string `json:"unionid"`
ErrCode int64 `json:"errcode"`
}
// WxJsCode js登录
func WxJsCode(appID, appSecret, code string) (*WxJsCodeResp, error) {
retryCount := 0
GotoHttpRetry:
_, body, errs := gorequest.New().
Get("https://api.weixin.qq.com/sns/jscode2session").
Query(gin.H{
"appid": appID,
"secret": appSecret,
"js_code": code,
"grant_type": "authorization_code",
}).EndBytes()
if errs != nil {
Log.Errorf("err: [%T] %s %s", errs[0], errs[0].Error(), code)
retryCount++
if retryCount < 3 {
goto GotoHttpRetry
}
return nil, errs[0]
}
var apiResp WxJsCodeResp
err := jsoniter.Unmarshal(body, &apiResp)
if err != nil {
Log.Errorf("json err: %s %s %s", err.Error(), body, code)
retryCount++
if retryCount < 3 {
goto GotoHttpRetry
}
return nil, fmt.Errorf("json err: %w %s", err, body)
}
if 0 != apiResp.Errcode {
return nil, fmt.Errorf("wx jscode err: %s", apiResp.Errmsg)
}
return &apiResp, nil
}
// WxAppCode app登录
func WxAppCode(appID, appSecret, code string) (*WxAppCodeResp, error) {
retryCount := 0
GotoHttpRetry:
_, body, errs := gorequest.New().
Get("https://api.weixin.qq.com/sns/oauth2/access_token").
Query(gin.H{
"appid": appID,
"secret": appSecret,
"js_code": code,
"grant_type": "authorization_code",
}).EndBytes()
if errs != nil {
retryCount++
if retryCount < 3 {
goto GotoHttpRetry
}
return nil, errs[0]
}
var apiResp WxAppCodeResp
err := jsoniter.Unmarshal(body, &apiResp)
if err != nil {
retryCount++
if retryCount < 3 {
goto GotoHttpRetry
}
return nil, fmt.Errorf("json err: %w %s", err, body)
}
if 0 != apiResp.ErrCode {
return nil, fmt.Errorf("wx jscode err: %d", apiResp.ErrCode)
}
return &apiResp, nil
}
// WxAppUserInfo 用户信息
func WxAppUserInfo(accessToken, openID string) (*WxAppUserInfoResp, error) {
retryCount := 0
GotoHttpRetry:
_, body, errs := gorequest.New().
Get("https://api.weixin.qq.com/sns/userinfo").
Query(gin.H{
"access_token": accessToken,
"openid": openID,
"lang": "zh_CN",
}).EndBytes()
if errs != nil {
retryCount++
if retryCount < 3 {
goto GotoHttpRetry
}
return nil, errs[0]
}
var apiResp WxAppUserInfoResp
err := jsoniter.Unmarshal(body, &apiResp)
if err != nil {
retryCount++
if retryCount < 3 {
goto GotoHttpRetry
}
return nil, fmt.Errorf("json err: %w %s", err, body)
}
if 0 != apiResp.ErrCode {
return nil, fmt.Errorf("wx jscode err: %d", apiResp.ErrCode)
}
return &apiResp, nil
}
// SQLRedisGetWxToken 获取小程序token
func SQLRedisGetWxToken(c context.Context, tx DbExeAble, redisClient *redis.Client, appID string,
funcSQLGetToken func(context.Context, DbExeAble, string) (string, string, int64, error),
funcSQLSetToken func(context.Context, DbExeAble, string, string, string, int64) error,
) (string, error) {
redisKey := fmt.Sprintf("wx_token_%s", appID)
token, err := RedisGet(
c,
redisClient,
redisKey,
)
if err != nil {
Log.Errorf("err: [%T] %s", err, err.Error())
} else {
if "" != token {
return token, nil
}
}
type apiRespSt struct {
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
Errcode int64 `json:"errcode"`
Errmsg string `json:"errmsg"`
}
secret, accessToken, expiresAt, err := funcSQLGetToken(
c,
tx,
appID,
)
if err != nil {
return "", err
}
if secret == "" {
return "", fmt.Errorf("no app of %s", appID)
}
// 1. 在有效期内
if expiresAt > time.Now().Unix() {
return accessToken, nil
}
_, body, errs := gorequest.New().Get("https://api.weixin.qq.com/cgi-bin/token").Query(gin.H{
"appid": appID,
"secret": secret,
"grant_type": "client_credential",
}).End()
if errs != nil {
return "", errs[0]
}
var apiResp apiRespSt
err = jsoniter.Unmarshal([]byte(body), &apiResp)
if err != nil {
return "", err
}
if 0 != apiResp.Errcode {
return "", errors.New("api resp error")
}
err = funcSQLSetToken(
c,
tx,
appID,
secret,
accessToken,
time.Now().Unix()+apiResp.ExpiresIn-10*60,
)
if err != nil {
return "", err
}
err = RedisSet(
c,
redisClient,
redisKey,
apiResp.AccessToken,
time.Second*time.Duration(apiResp.ExpiresIn-10*60),
)
if err != nil {
Log.Errorf("err: [%T] %s", err, err.Error())
}
return apiResp.AccessToken, nil
}
// SQLRedisRestWxToken 重置小程序token
func SQLRedisRestWxToken(c context.Context, tx DbExeAble, redisClient *redis.Client, appID string,
funcSQLResetToken func(context.Context, DbExeAble, string) error,
) {
redisKey := fmt.Sprintf("wx_token_%s", appID)
err := RedisRm(
c,
redisClient,
redisKey,
)
if err != nil {
Log.Errorf("err: [%T] %s", err, err.Error())
}
err = funcSQLResetToken(
c,
tx,
appID,
)
if err != nil {
Log.Errorf("err: [%T] %s", err, err.Error())
}
}