login.js
9.99 KB
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
import wx from '../utils/wx';
import event from '../common/event';
import Promise from '../vendors/es6-promise';
import accountModel from '../models/account/index';
/**
* 用户未授权-发送验证码
* @param mobile 手机号
* @param area 国家码
* @param degrees 验证码
*/
function sendVerifyCode(area, mobile, degrees) {
return accountModel.sendSms(area, mobile, degrees);
}
/**
* 用户已授权-发送验证码
* @param mobile 手机号
* @param area 国家码
* @param degrees 验证码
*/
function sendVerifyCodeWithUnionId(area, mobile, degrees) {
return accountModel.sendCodeByMiniApp(area, mobile, getApp().getOpenID(), degrees);
}
/**
* 是否需要验证码
*/
function isNeedImgCheck() {
return accountModel.isNeedImgCheck();
}
/**
* 获取验证码按钮
* @param area
* @param mobile
* @param degrees 验证码
* @returns {*}
*/
function getVerifyCode(area, mobile, degrees) {
let app = getApp();
if (!app.globalData.unionID) {
return sendVerifyCode(area, mobile, degrees);
}
return sendVerifyCodeWithUnionId(area, mobile, degrees);
}
/**
* 用户未授权-直接登录
* @param area 国家码
* @param mobile 手机号
* @param verifyCode 验证码
*/
function autoSignin(area, mobile, verifyCode) {
return accountModel.autoSignIn(mobile, verifyCode, area);
}
/**
* 用户已授权-校验验证码
* @param area 国家码
* @param mobile 手机号
* @param verifyCode 验证码
*/
function checkVerifyCode(area, mobile, verifyCode) {
return accountModel.bindMiniapp(getApp().globalData.unionID, mobile, verifyCode, area);
}
/**
* 验证手机号
* @param area
* @param mobile
* @param verifyCode
*/
function bindMobileAction(area, mobile, verifyCode) {
if (!getApp().globalData.unionID) {
return autoSignin(area, mobile, verifyCode);
}
return checkVerifyCode(area, mobile, verifyCode);
}
/**
* 判断微信用户是否绑定
* @param unionID
* @param nickName
* @returns {Promise.<TResult>|*}
*/
function wechatUserIsBind(unionID, nickName) {
let app = getApp();
return accountModel.wechatUserIsBind(unionID, nickName)
.then(data => {
wx.hideLoading();
if (data.data &&
data.data.is_bind &&
data.data.is_bind === 'Y') { // 已经绑定
let userInfo = {};
userInfo.is_bind = data.data.is_bind;
userInfo.mobile = data.data.mobile;
userInfo.ssouid = data.data.ssouid;
userInfo.uid = data.data.uid;
userInfo.sessionKey = data.data.session_key;
app.setUserInfo(userInfo);
app.setSessionKey(userInfo.sessionKey);
return Promise.resolve({
code: 10003,
data: userInfo,
message: '微信用户已绑定手机号'
});
} else {
return Promise.resolve({
code: 10004,
message: '微信用户未绑定手机号'
});
}
});
}
/**
* 获取unionID、如果用户拒绝授权则无法获得unionID
* @param srd_session
* @param showMsg
* @returns {Promise.<T>}
*/
function getUnionID(srd_session, showMsg) {
let nickName;
let app = getApp();
return wx.getUserInfo()
.then(res => {
let userInfo = res.userInfo;
nickName = userInfo.nickName;
return accountModel.decodeUserInfo(srd_session, res.encryptedData, res.iv);
})
.then(data => {
if (data.data.union_id) {
app.setUnionID(data.data.union_id);
return wechatUserIsBind(data.data.union_id, nickName);
}
})
.catch(() => {
let msg = {
succeed: false,
message: '使用微信小程序登录需要微信授权,您已经拒绝了该请求,请删除小程序重新进入。'
};
if (showMsg) {
msg.code = 10001;
}
return Promise.reject(msg);
});
}
/**
* 微信授权登录
* @param showMsg
* @returns {Promise.<T>}
*/
function wechatAuthLogin(showMsg) { // showMsg: 是否显示拒绝授权提示
let app = getApp();
return wx.login()
.then(res => {
if (res.code) {
return accountModel.wechatMiniAppLogin(res.code);
}
})
.then(data => {
if (data.code !== 200) {
return Promise.reject({
succeed: false,
message: data.message
});
} else {
event.emit('login-type-report', {LOGIN_TYPE: 4});
data = data.data;
app.setOpenID(data.openid);
app.setWechatThirdSession(data.srd_session);
// 如果unionID不存在(未使用过任何有货微信产品的全新用户),
// 调用getUnionID函数,再次获取unionID
if (!data.unionid) {
return getUnionID(data.srd_session, showMsg);
} else {
wx.getUserInfo();
app.setUnionID(data.unionid);
return wechatUserIsBind(data.unionid, '');
}
}
})
.catch(err => {
return Promise.reject(err);
});
}
/**
* 用户手动点击登录
* @returns {Promise.<T>}
*/
function tapToLogin() {
let app = getApp();
let router = global.router;
if (wx.getSetting) {
return wx.getSetting()
.then(res => {
if (res.authSetting['scope.userInfo']) {
return router.go('bindMobile');
} else {
return wx.showModal({
title: '',
content: '检测到您未打开微信用户信息授权,开启后即可进行登录',
confirmText: '去开启',
confirmColor: '#000000'
});
}
})
.then(res => {
if (res.confirm) {
return wx.openSetting();
}
if (res.cancel) {
return router.go('bindMobile');
}
})
.then(res => {
if (res.authSetting['scope.userInfo'] && res.authSetting['scope.userInfo'] === true) {
return getUnionID(app.getWechatThirdSession());
}
})
.then(res => {
if (res.code === 10003) { // 已经绑定手机号
app.setUserInfo(res.data);
app.setSessionKey(res.data.sessionKey);
event.emit('user-login-success');
}
if (res.code === 10004) { // 手动授权登录未绑定强制绑定
return router.go('bindMobile');
}
})
.catch(() => {});
} else {
return wx.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
});
}
}
/**
* 验证sessionKey是否过期
* @returns {*}
*/
function verifySessionKey() {
return accountModel.verify();
}
/**
* 自动绑定微信小程序
* @param mobile
* @param areaCode
* @constructor
*/
function _bindMiniAppByAuto(mobile, areaCode) {
return accountModel.bindMiniAppByAuto(getApp().globalData.unionID, mobile, areaCode);
}
/**
* 解密获取微信用户绑定的手机号
* @param iv
* @param encryptedData
*/
function decodePhoneNumber(iv, encryptedData) {
return accountModel.decodeUserInfo(getApp().globalData.thirdSession, encryptedData, iv);
}
/**
* 获取微信手机号码
* @param e
*/
function getPhoneNumber(e) {
const app = getApp();
let router = global.router;
if (e.detail.errMsg === 'getPhoneNumber:ok') {
let phoneNumber;
let countryCode;
wx.showLoading();
decodePhoneNumber(e.detail.iv, e.detail.encryptedData)
.then(res => {
phoneNumber = res.data.phoneNumber;
countryCode = res.data.countryCode;
if (res.data.phoneNumber && res.data.countryCode) {
return _bindMiniAppByAuto(res.data.phoneNumber, res.data.countryCode);
}
return Promise.reject({});
})
.then(res => {
wx.hideLoading();
if (res.code === 200) {
let userInfo = {};
userInfo.uid = res.data.uid;
userInfo.is_bind = res.data.is_bind || '';
userInfo.mobile = res.data.profile;
userInfo.ssouid = res.data.ssouid;
userInfo.sessionKey = res.data.session_key;
app.setUserInfo(userInfo);
app.setSessionKey(userInfo.sessionKey);
event.emit('user-login-success');
if (res.data && res.data.is_register === 0) {
event.emit('bind-auto-register-type-report', {YB_REGISTER_SUCCESS: 5});
}
} else {
return wx.showModal({
title: '提示',
content: res.message || '该手机已是yoho账户,请使用手机号动态登录'
});
}
})
.then(res => {
if (res && res.confirm) {
router.go('bindMobile', {phone: phoneNumber, area: countryCode});
}
})
.catch(() => {
router.go('bindMobile', {phone: phoneNumber, area: countryCode});
});
} else {
router.go('bindMobile');
}
}
export {
wechatAuthLogin,
isNeedImgCheck,
getVerifyCode,
bindMobileAction,
tapToLogin,
verifySessionKey,
getPhoneNumber
};