cert.js
4.53 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
/**
* 未认证手机号邮箱用户注册的引导手机号的绑定
*
* @author: lcy<chuanyang.liu@yoho.cn>
*/
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const passportHelper = require('../models/passport-helper');
const CertService = require('../models/cert-service');
const simpleHeaderModel = require('../../../doraemon/models/simple-header');
const config = global.yoho.config;
const LoginService = require('../models/login-service');
const cert = {
index: (req, res) => {
let refer = req.query.refer || `${config.siteUrl}`;
res.render('cert/index', {
region: req.ctx(passportHelper).getCountry(),
refer: refer,
serviceUrl: helpers.urlFormat('/help/detail', {id: 254}),
privacyUrl: helpers.urlFormat('/help/detail', {id: 256}),
module: 'passport',
page: 'cert',
title: '登录完善信息'
});
},
cert: (req, res) => {
let mobile = req.query.mobile;
let area = req.query.area;
let refer = req.query.refer;
res.render('cert/cert', {
enablePerfectInformation: true,
mobile: mobile,
area: area,
refer: refer,
prevUrl: helpers.urlFormat('/passport/cert/index', {
refer: refer
}),
module: 'passport',
page: 'cert-cert',
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
title: '登录绑定'
});
},
success: (req, res) => {
let type = _.trim(req.query.type);
let refer = req.query.refer || `${config.siteUrl}`;
let isRelate = (type === 'relate');
let data = {
goShopping: refer,
relate: isRelate,
module: 'passport',
page: 'cert-success',
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
title: isRelate ? '绑定手机号' : '关联手机号'
};
res.render('cert/success', data);
},
check: (req, res, next) => {
let mobile = req.body.mobile;
let area = req.body.area || '86';
let code = req.body.code;
if (!mobile || !area || !code) {
return res.json({code: 400, message: '', data: ''});
}
req.ctx(CertService).certCheck(mobile, area, code, req.body).then(({error, result}) => {
if (error) {
return res.json({code: 400, message: '短信验证码不正确', data: ''});
}
return res.json({code: 200, data: result});
}).catch(next);
},
sendCertMsg: (req, res, next) => {
let mobile = req.body.mobile;
let area = req.body.area;
let sessionId = req.session.id;
let captcha = req.body.verifyCode;
req.ctx(CertService).sendCertMsg(mobile, area, sessionId, captcha).then(result => {
if (result && result.code) {
return res.json(result);
} else {
return res.json({code: 400, message: '', data: ''});
}
}).catch(next);
},
certMobile: (req, res, next) => {
let mobile = _.trim(req.body.mobile);
let area = _.trim(req.body.area) || '86';
let code = _.trim(req.body.code);
if (!code) {
return res.json({
code: 400,
message: '短信验证码错误'
});
}
if (!req.session.forceBind ||
!req.session.forceBind.username ||
!req.session.forceBind.password
) {
return res.json({
code: 400,
message: '请重新登录'
});
}
req.ctx(CertService).certMobile(
req.session.forceBind.username,
req.session.forceBind.password,
mobile, code, area
).then(result => {
delete req.session.forceBind;
if (!result || result.code !== 200) {
return res.json({
code: 400,
message: _.get(result, 'message') || '操作错误,请稍后再试!'
});
}
return req.ctx(LoginService).syncUserSession(result.data, req, res).then(() => {
return res.json({code: 200, message: result.message});
}).catch(() => {
return res.json({code: 200, message: result.message});
});
}).catch(() => {
delete req.session.forceBind;
next();
});
}
};
module.exports = cert;