back.js
14.5 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
/**
* 找回密码
* Created by Tao.Huang on 2016/6/12.
*/
'use strict';
const helpers = global.yoho.helpers;
const config = global.yoho.config;
const BackService = require('../models/back-service');
const passportHelper = require('../models/passport-helper');
const simpleHeaderModel = require('../../../doraemon/models/simple-header');
const _ = require('lodash');
/**
* 找回密码主页面
*/
const index = (req, res, next) => {
// 清除cookie
res.clearCookie('_UID', {
domain: config.cookieDomain
});
res.clearCookie('_TOKEN', {
domain: config.cookieDomain
});
req.ctx(BackService).indexPageDataAsync()
.then(result => {
res.render('back/index', Object.assign({
module: 'passport',
page: 'back-index',
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
title: '找回密码'
}, result));
})
.catch(next);
};
/**
* 校验用户输入信息,是否是已经注册的用户
*/
const validateInputAPI = (req, res, next) => {
let userInput = req.body.phoneNum || '';
let areaCode = (req.body.area || '86').replace('+', '');
req.ctx(BackService).validateEmailOrMobileAsync(userInput, areaCode)
.then(result => {
req.inputInfo = result;
next();
})
.catch(err => {
res.json({
code: 400,
message: err
});
});
};
/**
* 校验用户输入信息,是否是已经注册的用户
*/
const validateInputPage = (req, res, next) => {
let userInput = req.body.phoneNum || '';
let areaCode = (req.body.area || '86').replace('+', '');
req.ctx(BackService).validateEmailOrMobileAsync(userInput, areaCode)
.then(result => {
req.inputInfo = result;
next();
})
.catch((err) => {
res.json({
code: 400,
message: err
});
});
};
const getUserInfoAPI = (req, res, next) => {
let inputInfo = req.inputInfo;
req.session.type = 'back-step1';
req.ctx(BackService).findUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area)
.then(result => {
res.json(result);
})
.catch(next);
};
const sendCodePage = (req, res, next) => {
let inputInfo = req.inputInfo;
let id = req.session.id;
let captcha = req.body.verifyCode;
req.session.type = 'back-step1';
req.ctx(BackService).sendCodeToUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area, id, captcha)
.then(result => {
if (!(result.code && result.code === 200)) {
return res.json({
code: result.code,
message: result.message,
data: {
refer: helpers.urlFormat('/passport/back/index')
}
});
}
return next();
})
.catch(next);
};
const saveInSession = (req, res) => {
if (req.session.type !== 'back-step1') {
return res.json({
code: 400,
message: '未验证',
data: {
refer: helpers.urlFormat('/passport/back/index')
}
});
}
req.session.type = 'back-step2';
switch (req.inputInfo.type) {
case 'email':
req.session.email = req.inputInfo.phone;
return res.json({
code: 200,
data: {
refer: helpers.urlFormat('/passport/back/sendemail')
}
});
case 'mobile':
req.session.mobile = req.inputInfo.phone;
req.session.area = req.inputInfo.area;
req.session.captcha = req.body.verifyCode;
return res.json({
code: 200,
data: {
refer: helpers.urlFormat('/passport/back/verification')
}
});
default:
return res.json({
code: 500,
data: {
refer: helpers.urlFormat('/passport/back/index')
}
});
}
};
const sendBackMobileAPI = (req, res, next) => {
let mobile = req.body.mobile || '';
let area = req.body.area || '86';
let id = req.session.id;
let captcha = req.body.verifyCode;
req.ctx(BackService).sendCodeToMobileAsync(area, mobile, id, captcha)
.then(result => {
res.json(result);
})
.catch(next);
};
const validateMobileAPI = (req, res, next) => {
let mobile = req.body.mobile || '';
const ERR = {code: 400, message: '验证失败'};
if (req.session.type !== 'back-step3') {
return res.json(ERR);
}
if (!req.ctx(passportHelper).validator.isMobile(mobile)) {
return res.json(ERR);
}
next();
};
const validateEmailInSession = (req, res, next) => {
let email = req.session.email || '';
if (!email) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
let isp = email.split('@')[1].toLowerCase();
const mapperEmailISP = {
'yoho.cn': 'http://exmail.qq.com/login'
};
req.body.emailUrl = mapperEmailISP[isp] || `http://mail.${isp}`;
next();
};
const sendEmailPage = (req, res, next) => {
if (req.session.type !== 'back-step2') {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
req.session.type = 'back-step3';
req.ctx(passportHelper).getLeftBannerAsync()
.then(result => {
res.render('back/send-email', Object.assign({
module: 'passport',
page: 'back-send-email-ok',
title: '邮件发送成功'
}, {
sendEmail: {
coverHref: result.url,
coverImg: result.img,
email: req.body.emailUrl
}
}));
})
.catch(next);
};
const validateCodeByEmailPage = (req, res, next) => {
let code = req.query.code || '';
if (!_.isEmpty(req.mobileAuth)) {
return next();
}
req.ctx(BackService).checkEmailCodeAsync(code)
.then(result => {
if (!result) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
return next();
})
.catch(next);
};
const resetPasswordPage = (req, res, next) => {
let code = req.query.code || '';
req.ctx(passportHelper).getLeftBannerAsync()
.then(result => {
res.render('back/reset-pwd', Object.assign({
module: 'passport',
page: 'back-reset-pwd',
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
title: '重置密码'
}, {
resetPwd: Object.assign({
coverHref: result.url,
coverImg: result.img,
code: code
}, req.mobileAuth)
}));
})
.catch(next);
};
const verifyCodeByMobilePage = (req, res, next) => {
if (req.session.type !== 'back-step2') {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
req.session.type = 'back-step3';
req.ctx(passportHelper).getLeftBannerAsync()
.then(result => {
res.render('back/verification', Object.assign({
module: 'passport',
page: 'back-verify-mobile-code',
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
title: '手机验证'
}, {
verification: {
coverHref: result.url,
coverImg: result.img,
mobile: req.body.mobile,
area: req.body.area,
verifyCode: req.body.verifyCode
}
}));
})
.catch(next);
};
const validateSuccessStatusPage = (req, res, next) => {
let successType = req.session.successType || '';
if (!successType) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
delete req.session.successType;
next();
};
const resetPwdSuccessPage = (req, res, next) => {
req.ctx(passportHelper).getLeftBannerAsync()
.then(result => {
res.render('back/reset-success', Object.assign({
module: 'passport',
page: 'back-index',
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
title: '重置密码成功'
}, {
resetSuccess: {
coverHref: result.url,
coverImg: result.img
}
}));
})
.catch(next);
};
const verifyCodeByMobileAPI = (req, res, next) => {
let mobile = req.body.mobile || '';
let area = req.body.area || '86';
let mobileCode = req.body.code || '';
const ERR = {
code: 400,
message: '验证码错误!',
data: helpers.urlFormat('/passport/back/index')
};
const session = req.session;
if (!mobileCode || mobile !== session.mobile || area !== session.area) {
return res.json(ERR);
}
if (session.type !== 'back-step3') {
return res.json(ERR);
}
req.ctx(BackService).verifyCodyByMobileAsync(area, mobile, mobileCode)
.then(result => {
res.json(result);
})
.catch(next);
};
const validateExistCodePage = (req, res, next) => {
let code = req.query.code || req.body.code;
if (!code) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
next();
};
const validateCodeByMobilePage = (req, res, next) => {
let code = req.query.code || req.body.code;
let mobile = req.query.mobile || req.body.mobile;
let area = req.query.area || req.body.area;
let token = req.query.token || req.body.token;
let createdAt = req.query.createdAt || req.body.createdAt;
if (!mobile) {
req.mobileAuth = {};
return next();
}
let data = {
mobile: mobile,
area: area,
token: token,
createdAt: createdAt
};
code = new Buffer(code, 'base64').toString();
req.mobileAuth = req.ctx(BackService).authRequest(data, code);
next();
};
const validatePwdPage = (req, res, next) => {
let pwd = req.body.pwd || '';
if (!req.ctx(passportHelper).validator.isPassword(pwd)) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
next();
};
const updatePwdAPI = (req, res, next) => {
let code = req.body.code || '';
let mobileAuth = req.mobileAuth || {};
let newPassword = req.body.pwd || '';
req.ctx(BackService).updatePwdAsync(code, mobileAuth, newPassword)
.then(result => {
if (result.status) {
req.session.successType = result.type;
res.redirect(helpers.urlFormat('/passport/back/resetSuccess'));
} else {
res.redirect(helpers.urlFormat('/passport/back/index'));
}
})
.catch(next);
};
const validateMobileInSession = (req, res, next) => {
req.body.mobile = req.session.mobile || '';
req.body.area = req.session.area || '';
if (!req.body.mobile) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
next();
};
const weakPasswordPage = (req, res, next) => {
let refer = req.query.refer || helpers.urlFormat('/');
if (!req.session.CSToken && !req.session.canSkip) {
return res.redirect(refer);
}
req.ctx(passportHelper).getLeftBannerAsync().then(result => {
res.render('back/weak-pwd', Object.assign({
module: 'passport',
page: 'back-weak-pwd',
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
title: '重置登录密码'
}, {
resetPwd: {
coverHref: result.url,
coverImg: result.img,
refer: refer,
isCanSkip: req.session.canSkip === 'Y'
}
}));
}).catch(next);
};
const weakPwdSuccessPage = (req, res, next) => {
let refer = req.query.refer || helpers.urlFormat('/');
req.ctx(passportHelper).getLeftBannerAsync()
.then(result => {
res.render('back/weak-success', Object.assign({
module: 'passport',
page: 'back-index',
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
title: '重置密码成功'
}, {
resetSuccess: {
coverHref: result.url,
coverImg: result.img,
refer: helpers.urlFormat('/signin.html', {
refer: refer
})
}
}));
})
.catch(next);
};
const modPwdByCodeAPI = (req, res, next) => {
let body = req.body;
req.ctx(BackService).modPwdByCodeAsync({
uid: req.user.uid,
oldPwd: body.oldPwd,
newPwd: body.newPwd,
token: req.session.CSToken
}).then(d => {
if (d.code === 200 && req.session) {
delete req.session.CSToken;
delete req.session.canSkip;
}
res.json(d);
}).catch(next);
};
module.exports = {
index, // 首页
getUserInfoAPI, // 通过邮箱或手机号获得用户信息
sendCodePage, // 发送验证码到邮箱或者手机,然后跳转页面
saveInSession, // 保存状态到session中
sendEmailPage, // 发送邮件成功的页面
verifyCodeByMobilePage, // 验证手机验证码的页面
verifyCodeByMobileAPI, // 验证手机验证码
sendBackMobileAPI, // 重新发送验证码到手机
resetPasswordPage, // 重设密码页面
updatePwdAPI, // 重设密码接口
validateMobileAPI, // 验证手机号是否合法
resetPwdSuccessPage, // 重设密码成功页面
validateInputAPI, // 验证用户输入的邮箱或者手机是否合法,返回是json
validateInputPage, // 验证用户输入的邮箱或者手机是否合法,跳转是页面
validateEmailInSession, // 验证邮箱是否在session中
validateMobileInSession, // 验证手机是否在session中
validateCodeByEmailPage, // 验证邮箱验证码
validateCodeByMobilePage, // 验证手机验证码
validateSuccessStatusPage, // 验证重设密码状态
validateExistCodePage, // 验证参数是否存在code
validatePwdPage, // 验证密码是否合法
weakPasswordPage, // 弱密码页面
weakPwdSuccessPage, // 弱密码重置成功页面
modPwdByCodeAPI // 弱密码重置接口
};