globalData.js
20 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
import Taro from '@tarojs/taro';
import * as globalData from '../constants/globalData';
import {login as loginModel} from '../models';
import config from '../config';
import user from '../utils/user';
export const setUdid = (udid) => {
udid = !udid ? user.getYHStorageSync('udid', 'app') : udid;
return {
udid,
type: globalData.UDID
}
}
export const setUserInfo = (userInfo) => {
userInfo = !userInfo ? user.getYHStorageSync('userInfo', 'app') : userInfo;
return {
userInfo,
type: globalData.USERINFO
}
}
export const setSystemInfo = (systemInfo) => {
return {
systemInfo,
type: globalData.SYSTEMINFO
}
}
export const setWxUnionId = (wxUnionId) => {
wxUnionId = !wxUnionId ? user.getYHStorageSync('unionID', 'app') : wxUnionId;
return {
wxUnionId,
type: globalData.WXUNIONID
}
}
export const setUnionInfo = (unionInfo) => {
return {
unionInfo,
type: globalData.UNIONINFO
}
}
export const setWxThirdSession = (WXThirdSession) => {
WXThirdSession = !WXThirdSession ? user.getYHStorageSync('WXThird_session', 'app') : WXThirdSession;
return {
WXThirdSession,
type: globalData.WXTHIRDSESSION
}
}
export const setOpenId = (id) => {
return {
id,
type: globalData.OPENID
}
}
export const setSessionkey = (sessionKey) => {
sessionKey = !sessionKey ? user.getYHStorageSync('session_key', 'app') : sessionKey;
return {
sessionKey,
type: globalData.SESSIONKEY
}
}
export const getWechatThirdSession = () => {
return (dispatch, getState) => {
let value = getState().globalData.WXThird_session;
return !value ? user.getYHStorageSync('WXThird_session','app') : value;
}
}
// 获取用户相关union_type 有赚
// export const getUnionTypeWithUid = (uid) => {
// return dispatch => {
// return new Promise((resolve, reject) => {
// loginModel.queryUnionTypeByUid({uid: uid}).then(ret => {
// if (ret && ret.code == 200) {
// dispatch(setUnionInfo({
// userUnionType: ret.data.unionType,
// userUnionTypeImageUrl: ret.data.imageUrl,
// shareId: ret.data.shareId,
// userNounionTypeImageUrl: ret.data.applyImageUrl
// }));
// }
// })
// })
// }
// }
// 判断该微信用户是否已经绑定了有货账号,如果已绑定则返回uid等用户信息
export const wechatUserIsBind = (unionID, nickName, userAvatarUrl) => {
return (dispatch, getState) => {
return new Promise((resolve, reject) => {
loginModel.signinByOpenID({
openId: unionID,
nickname: nickName
}).then(ret => {
ret = ret && ret.data || {};
// 已绑定
if (ret.is_bind && ret.is_bind === 'Y') {
let userInfo = {
is_bind: ret.is_bind,
mobile: ret.mobile,
ssouid: ret.ssouid,
uid: ret.uid,
session_key: ret.session_key
};
dispatch(setUserInfo(userInfo));
dispatch(setSessionkey(ret.session_key));
let srd_session = user.getYHStorageSync('WXThird_session', 'app');
if (!getState().globalData.userInfo.avatarUrl){
dispatch(getUnionID(srd_session));
}
user.sendWeChatUserData(userInfo.uid,nickName,userAvatarUrl);
return resolve({
succeed: true,
message: 'UID存在'
});
} else {
return resolve({
succeed: false,
message: 'UID不存在'
});
}
})
});
}
}
/*
申请获取用户个人信息授权后,调用接口解码,返回unionID.
*/
export const getUnionID = (srd_session) => {
return dispatch => {
return new Promise((resolve, reject) => {
Taro.getUserInfo({
success: (res) => {
let userInfo = res.userInfo;
let nickName = userInfo.nickName;
//保存用户信息
dispatch(setUserInfo(userInfo));
// if (userInfo.uid) {
// dispatch(getUnionTypeWithUid(userInfo.uid));// 有赚
// }
loginModel.getDecodeUserInfo({
srdSession: srd_session,
encryptedData: res.encryptedData,
iv: res.iv
}).then(async data => {
let responseData = data.data
// checkUnionIDExist(responseData.openid + '', responseData.union_id + '', responseData.srd_session+'', param.method)
if (responseData.union_id){
//保存并查询该unionID是否有uid
dispatch(setWXUnionID(responseData.union_id));
user.sendWeChatUserDataWithUnionId(responseData.union_id, userInfo.nickName, userInfo.avatarUrl);
let json = await dispatch(wechatUserIsBind(responseData.union_id, nickName, userInfo.avatarUrl));
return resolve(Object.assign(json, {
isHaveUnionID: true
}));
}
})
}
})
})
}
}
export const wechatLogin = function() {
return (dispatch, getState) => {
return new Promise((resolve, reject) => {
wx.login({
success: (res) => {
if (res.code) {
loginModel.onLogin({
jsCode: res.code,
miniapp_type: config.mini_app_type
}).then(ret => {
if (ret && ret.code == 200) {
let responseData = ret.data
//保存srdSession
dispatch(setWxThirdSession(responseData.srd_session));
//保存openid
dispatch(setOpenId(responseData.openid));
//当unionId为空或者头像为空时,调用微信接口获取用户信息
if (responseData.unionid) {
// dispatch(getUnionID(responseData.srd_session));
dispatch(setWxUnionId(responseData.unionid));
user.sendWeChatUserDataWithUnionId(responseData.unionid, getState().globalData.userInfo.nickName, getState().globalData.userInfo.avatarUrl)
dispatch(wechatUserIsBind(responseData.unionid, '', ''));
}
}
})
}
}
});
});
}
}
/*******************************************************************/
/**
*解密通过微信授权获取到的手机号
首先调用wechat.smallProgram.decodeUserInfo解码获得手机号
再调用app.passport.miniAppBindByAuto接口,
此接口会在后台做判断:
如果该手机号【已存在】,则提示用户手动验证手机号,
如果手机号【不存在】,则自动创建账号,并返回uid.
**/
/*******************************************************************/
export const decodePhoneNumber = (iv, encryptedData, inviteCode) => {
return dispatch => {
return loginModel.getDecodeUserInfo({
srdSession: app.globalData.WXThird_session,
encryptedData: encryptedData,
iv: iv
}).then(ret => {
ret = ret && ret.data || {};
if (ret.phoneNumber !== '' && ret.countryCode != '') {
return dispatch(BindMiniAppByAuto(data.data.phoneNumber, data.data.countryCode, inviteCode));
}
});
}
}
export const BindMiniAppByAuto = (mobile, countryCode, inviteCode) => {
return (dispatch, getState) => {
return new Promise((resolve, reject) => {
if (!mobile) {
return resolve({
succeed: false,
message: "手机号为空或格式不正确",
code: -1,
});
}
let globalData = getState().globalData;
let param = {
area: countryCode,
mobile: mobile,
open_id: globalData.WXUnion_ID,
wechatHeadIco: globalData.userInfo.avatarUrl,
wechatNickName: globalData.userInfo.nickName,
inviteCode
};
return loginModel.miniAppBindByAuto(param).then(ret => {
if (ret && ret.code === 200) {
ret = ret.data || {};
dispatch(setSessionkey(ret.session_key));
dispatch(setUserInfo({...globalData.userInfo, uid: ret.uid}));
user.sendWeChatUserData(ret.uid, globalData.userInfo.nickName, globalData.userInfo.avatarUrl);
return resolve({
succeed: true,
message: "登录成功",
code: 200,
is_register: true,
});
} else {
return resolve({
succeed: false,
message: data.message,
code: -1
});
}
})
})
}
}
export const reloadSessionkey = () => {
return (dispatch) => {
dispatch(setSessionkey());
}
}
export const reloadUserInfo = () => {
return (dispatch) => {
dispatch(setUserInfo());
}
}
// 手动开启设置页面授权
export const loginAndRegisterTapped = () => {
return (dispatch, getState) => {
return new Promise((resolve, reject) => {
if (Taro.getSetting) {
Taro.getSetting({
success: (res) => {
if (res.authSetting["scope.userInfo"] !== null && res.authSetting["scope.userInfo"] !== undefined && res.authSetting["scope.userInfo"] === false) {
Taro.showModal({
title: '',
content: '检测到您未打开微信用户信息授权,开启后即可进行登录',
confirmText: "去开启",
confirmColor: "#000000",
success: (ret) => {
if (ret.confirm) {
Taro.openSetting({
success: (result) => {
if (result.authSetting["scope.userInfo"] !== null && result.authSetting["scope.userInfo"] !== undefined && result.authSetting["scope.userInfo"] === true) {
return resolve(dispatch(getUnionID(getState().globalData.WXThirdSession)));
}
},
fail: (e) => {
Taro.navigateTo({
url: '/pages/login/index',
});
}
})
} else {
Taro.navigateTo({
url: '/pages/login/index',
})
}
}
})
} else {
Taro.navigateTo({
url: '/pages/login/index',
})
}
}
})
} else {
Taro.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
})
}
})
}
}
/**
* button触发getUserInfo之后立即解析获取unionid
*/
export const decodeUnionId = (srd_session, result) => {
return dispatch => {
return new Promise((resolve, reject) => {
let res = result.detail;
let userInfo = res.userInfo;
let nickName = userInfo.nickName;
//保存用户信息
dispatch(setUserInfo(userInfo));
loginModel.getDecodeUserInfo({
srdSession: srd_session,
encryptedData: res.encryptedData,
iv: res.iv
}).then(data => {
let responseData = data.data
// checkUnionIDExist(responseData.openid + '', responseData.union_id + '', responseData.srd_session + '', param.method)
if (responseData.union_id) {
//保存并查询该unionID是否有uid
dispatch(setWxUnionId(responseData.union_id))
user.sendWeChatUserDataWithUnionId(responseData.union_id, userInfo.nickName, userInfo.avatarUrl);
dispatch(wechatUserIsBind(responseData.union_id, nickName, userInfo.avatarUrl));
return resolve({
isHaveUnionID: true
});
}
});
})
}
}
/*
点击获取验证码按钮
mobile: 手机号
vierifycode:验证码
areaCode:国家码
*/
export const getVerifyKey = (phoneNumb, areaCode, fromPage, degrees, callbackFunc) => {
return (dispatch, getState) => {
return new Promise((resolve, reject) => {
if (!phoneNumb || !areaCode) {
return resolve({});
}
if (!getState().globalData.wxUnionId) {
return loginModel.sendSms({
area: areaCode,
mobile: phoneNumb,
fromPage ,
degrees,
}).then(ret => {
if (ret.code != 200) {
return resolve({
succeed: false,
message: ret.message
});
} else {
return resolve({
succeed: true,
message: '发送成功'
});
}
})
} else {
return loginModel.sendThirdBindMobileCodeOnlyImg({
area: areaCode,
mobile: phoneNumb,
open_id: getState().globalData.openId,
fromPage,
degrees
}).then(ret => {
if (ret && ret.code !== 200) {
return resolve({
succeed: false,
message: ret.message
});
} else {
return resolve({
succeed: true,
message: '发送成功'
});
}
});
}
})
}
}
/*
点击完成按钮
phoneNumb: 手机号
vierifycode:验证码
areaCode:国家码
*/
export const bindAction = (phoneNumb, verifyCode, areaCode, inviteCode, callbackFunc) => {
return (dispatch, getState) => {
return new Promise((resolve, reject) => {
if (!phoneNumb || !areaCode) {
return resolve({})
}
let globalData = getState().globalData;
if (!globalData.wxUnionId) {
// 用户未授权 直接登录
return loginModel.autoSignin({
profile: phoneNumb,
area: areaCode,
code: verifyCode,
inviteCode
}).then(ret => {
if (ret && ret.code === 200 && ret.data && ret.data.uid > 0) {
let userInfo = {
ssouid: ret.data.ssouid,
uid: ret.data.uid,
profile: ret.data.profile,
session_key: ret.data.session_key,
is_bind: ret.data.is_bind
}
dispatch(setSessionkey(ret.data.session_key));
dispatch(setUserInfo(userInfo));
wx.setStorageSync('userInfo', userInfo);
wx.setStorageSync('session_key', ret.data.session_key);
wx.setStorageSync('uid', ret.data.uid);
return resolve({
succeed: true,
code: ret.code,
message: ret.message
});
} else {
return resolve({
succeed: false,
code: ret.code,
message: ret.message
});
}
});
} else {
// 用户已授权
return loginModel.bindMiniapp({
code: verifyCode,
mobile: phoneNumb,
area: areaCode,
open_id: globalData.wxUnionId,
inviteCode,
wechatHeadIco: globalData.userInfo.avatarUrl,
wechatNickName: globalData.userInfo.nickName
}).then(ret => {
Taro.hideToast()
if (ret.code === 200 && (ret.data.uid !== null) && ret.data.uid > 0 && ret.data.uid !== undefined) {//验证正确
let userInfo = {
ssouid: ret.data.ssouid,
uid: ret.data.uid,
profile: ret.data.profile,
mobile: phoneNumb
}
dispatch(setSessionkey(ret.data.session_key));
dispatch(setUserInfo(userInfo));
user.sendWeChatUserData(userInfo.uid, globalData.userInfo.nickName, globalData.userInfo.avatarUrl);
if (ret.data.is_register !== undefined && ret.data.is_register !== null && ret.data.is_register === 0 ) {
// let logParams = {
// YB_REGISTER_SUCCESS: 5,
// };
// logEvent(YB_REGISTER_SUCCESS, logParams);
return resolve({
succeed: true,
message: '登录成功',
code: 200,
is_register: true,
});
} else {
return resolve({
succeed: true,
code: ret.code,
message: ret.message,
});
}
} else {//验证错误
return resolve({
succeed: false,
message: ret.message
});
}
})
}
})
}
}