-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat_controller.go
362 lines (323 loc) · 9.05 KB
/
wechat_controller.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package control
import (
"encoding/json"
"fmt"
"bingomall/constant"
"bingomall/helpers"
"bingomall/helpers/wechat"
model "bingomall/models"
"bingomall/repositories"
service "bingomall/services"
"log"
"net/http"
"time"
"github.com/chanxuehong/rand"
"github.com/chanxuehong/session"
"github.com/chanxuehong/sid"
mpOauth2 "github.com/chanxuehong/wechat/mp/oauth2"
"github.com/chanxuehong/wechat/oauth2"
"github.com/gin-gonic/gin"
)
var (
sessionStorage = session.New(20*60, 60*60)
oauth2Endpoint oauth2.Endpoint = mpOauth2.NewEndpoint(constant.WxAppId, constant.WxAppSecret)
)
type WXLoginResp struct {
OpenId string `json:"openid"`
SessionKey string `json:"session_key"`
UnionId string `json:"unionid"`
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
Code string `json:"code"`
}
type RegisterForm struct {
WechatCode string `form:"code" binding:"required" json:"code"`
EncryptedData string `form:"encryptedData" binding:"required" json:"encryptedData"`
Iv string `form:"iv" binding:"required" json:"iv"`
Referrer string `form:"referrer" json:"referrer"`
}
type LoginForm struct {
WechatCode string `form:"code" binding:"required" json:"code"`
}
// wechat授权,建立必要的 session, 然后跳转到授权页面
// @Summary wechat授权
// @Tags UserController
// @Accept json
// @Produce json
// @Param username query string true "用户名"
// @Param password query string true "密码"
// @Param phone query string true "电话号码"
// @Param email query string true "邮件"
// @Success 200 {object} helpers.JsonObject
// @Router /api/wechat/auth [post]
func Auth(context *gin.Context) {
sId := sid.New()
state := string(rand.NewHex())
if err := sessionStorage.Add(sId, state); err != nil {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 2010,
Message: err.Error(),
})
log.Println(err)
return
}
context.SetCookie("sid", sId, 3600*24*30, "/", constant.Domain, false, true)
AuthCodeURL := mpOauth2.AuthCodeURL(constant.WxAppId, constant.Oauth2RedirectURI, constant.Oauth2Scope, state)
log.Println("AuthCodeURL:", AuthCodeURL)
context.Redirect(http.StatusFound, AuthCodeURL)
}
// 授权后回调页面
func ExchangeToken(context *gin.Context) {
log.Println(context)
sId, err := context.Cookie("sid")
if err != nil {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 2003,
Message: err.Error(),
})
return
}
CallbackSession, err := sessionStorage.Get(sId)
if err != nil {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 2004,
Message: err.Error(),
})
return
}
savedState := CallbackSession.(string) // 一般是要序列化的, 这里保存在内存所以可以这么做
code := context.Query("code")
state := context.Query("state")
if code == "" {
log.Println("用户禁止授权")
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 2005,
Message: "用户禁止授权",
})
return
}
if state == "" {
log.Println("state 参数为空")
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 2006,
Message: "state 参数为空",
})
return
}
if savedState != state {
str := fmt.Sprintf("state 不匹配, session 中的为 %q, url 传递过来的是 %q", savedState, helper.Json(context.Request))
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 2007,
Message: str,
})
log.Println(str)
return
}
oauth2Client := oauth2.Client{
Endpoint: oauth2Endpoint,
}
token, err := oauth2Client.ExchangeToken(code)
if err != nil {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 2008,
Message: err.Error(),
})
log.Println(err)
return
}
log.Printf("token: %+v\r\n", token)
fmt.Println("token:", token)
userInfo, err := mpOauth2.GetUserInfo(token.AccessToken, token.OpenId, "", nil)
if err != nil {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 2009,
Message: err.Error(),
})
log.Println(err)
return
}
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 0,
Message: "ok",
Content: userInfo,
})
log.Printf("userinfo: %+v\r\n", userInfo)
return
}
// 这个函数以 code 作为输入, 返回调用微信接口得到的对象指针和异常情况
func RegisterByWeChat(context *gin.Context) {
url := constant.WechatUserInfoUrl
form := &RegisterForm{}
//code := context.PostForm("code") // 'Content-Type': 'application/x-www-form-urlencoded' 用这个方法取,
// 'Content-Type': 'application/json'用context.BindJSON(&form)的方法取
if context.BindJSON(&form) != nil {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 500,
Message: "请求参数错误",
})
return
}
url = fmt.Sprintf(url, constant.WechatAppId, constant.WechatAppSecret, form.WechatCode)
resp, err := http.Get(url)
if err != nil {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 500,
Message: "请求微信服务器错误",
})
return
}
defer resp.Body.Close()
// 解析http请求中body 数据到我们定义的结构体中
wxResp := WXLoginResp{}
decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(&wxResp); err != nil {
return
}
pc := wechat.WxBizDataCrypt{AppID: constant.WechatAppId, SessionKey: wxResp.SessionKey}
userInfo, err := pc.Decrypt(form.EncryptedData, form.Iv, true) //第三个参数解释:需要返回JSON数据类型时使用true, 需要返回map数据类型时使用false
if err != nil {
fmt.Println(err)
}
person := helper.BaseUserInfo{}
_ = json.Unmarshal([]byte(userInfo.(string)), &person) //把json中多个数据取部分数据出来
// 判断微信接口返回的是否是一个异常情况
if wxResp.ErrCode != 0 {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: wxResp.ErrCode,
Message: wxResp.ErrMsg,
})
return
}
userService := service.UserServiceInstance(repositories.UserRepositoryInstance(helper.GetUserDB()))
user := userService.GetByUserUnionId(wxResp.UnionId)
if user != nil {
user.LogonCount += 1
user.LoginTime = time.Now()
user.OpenId = wxResp.OpenId
user.Nickname = person.Nickname
user.Avatar = person.AvatarUrl
err := userService.UpdateByApp(user)
if err != nil {
context.JSON(http.StatusOK, helper.JsonObject{
Code: 4003,
Message: helper.StatusText(helper.LoginStatusSQLErr),
Content: err,
})
return
}
} else {
user = &model.User{}
user.OpenId = wxResp.OpenId
user.UnionId = wxResp.UnionId
user.AccessToken = wxResp.SessionKey
user.Nickname = person.Nickname
user.Avatar = person.AvatarUrl
user.LoginTime = time.Now()
err = userService.SaveByApp(user)
if err != nil {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 4100,
Message: err.Error(),
})
return
}
db := helper.GetDBByName(constant.DBMerchant)
walletService := service.WalletServiceInstance(repositories.WalletRepositoryInstance(db))
err = walletService.InitMyWallet(user.ID)
if err != nil {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 4004,
Message: err.Error(),
})
return
}
}
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 0,
Message: "ok",
Content: wxResp,
})
}
func LoginByWeChat(context *gin.Context) {
form := &LoginForm{}
if context.BindJSON(&form) != nil {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 500,
Message: "请求参数错误",
})
return
}
//code := context.PostForm("code") // 'Content-Type': 'application/x-www-form-urlencoded' 用这个方法取,
// 'Content-Type': 'application/json'用context.BindJSON(&form)的方法取
url := fmt.Sprintf(constant.WechatUserInfoUrl, constant.WechatAppId, constant.WechatAppSecret, form.WechatCode)
resp, err := http.Get(url)
if err != nil {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 500,
Message: "请求微信服务器错误",
})
return
}
defer resp.Body.Close()
// 解析http请求中body 数据到我们定义的结构体中
wxResp := WXLoginResp{}
decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(&wxResp); err != nil {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: wxResp.ErrCode,
Message: err.Error(),
})
return
}
if wxResp.ErrCode != 0 {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: wxResp.ErrCode,
Message: wxResp.ErrMsg,
})
return
}
userService := service.UserServiceInstance(repositories.UserRepositoryInstance(helper.GetUserDB()))
var user *model.User
if wxResp.UnionId != "" {
user = userService.GetByUserUnionId(wxResp.UnionId)
} else {
user = userService.GetByUserOpenId(wxResp.OpenId) //没有绑定公众号,没有unionid,这个临时这样子解决
}
person := helper.BaseUserInfo{}
token, err := generateAppToken(user)
person.Token = token
person.UserID = user.ID
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 0,
Message: "ok",
Content: person,
})
}
func CheckTokenWeChat(context *gin.Context) {
context.JSON(http.StatusOK,
&helper.JsonObject{
Code: 0,
Message: "ok",
Content: "",
})
}