cert.js
8.11 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
/**
* 未认证手机号邮箱用户注册的引导手机号的绑定
*
* @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 UserService = require('../models/user-service');
const simpleHeaderModel = require('../../../doraemon/models/simple-header');
const config = global.yoho.config;
const cert = {
headerTip: (req, res, next) => {
let uid = req.user.uid,
dest = {
code: 200,
data: 'N'
};
if (!uid) {
res.type('text/javascript');
return res.send(req.query.callback + '(' + JSON.stringify(dest) + ')');
}
req.ctx(CertService).checkEmailCertTip(uid)
.then(ret => {
dest.data = _.get(ret, 'data.isBindedOrRelated') === 'N' ? 'Y' : 'N';
res.type('text/javascript');
res.send(req.query.callback + '(' + JSON.stringify(dest) + ')');
})
.catch(next);
},
tip: (req, res) => {
let refer = req.query.refer;
res.render('cert/tip', {
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
refer: refer,
certUrl: helpers.urlFormat('/passport/cert/index', {
refer: refer
})
});
},
index: (req, res) => {
let refer = req.query.refer || `${config.siteUrl}`;
res.render('cert/index', {
region: passportHelper.getCountry(),
refer: refer,
serviceUrl: helpers.urlFormat('/help', {
category_id: 9
}),
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
module: 'passport',
page: 'cert',
title: '登录完善信息'
});
},
cert: (req, res) => {
let mobile = req.body.mobile;
let area = req.body.area;
let refer = req.body.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: '登录绑定'
});
},
relate: (req, res, next) => {
let mobile = req.body.mobile;
let area = req.body.area;
let refer = req.body.refer;
req.ctx(UserService).getUserInfo(area, mobile).then(user => {
let data = _.assign(user, {
phoneNum: mobile,
areaCode: area,
refer: refer,
changeHref: helpers.urlFormat('/passport/cert/index', {
refer: refer
}),
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
module: 'passport',
page: 'cert-relate',
title: '账号关联'
});
res.render('cert/relate', data);
}).catch(next);
},
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';
if (mobile && area) {
/**
* 接口绑定返回值:
* code:200,isCanBind=Y // 可绑定
* code:200,isCanBind=Y // 不可绑定
*/
req.ctx(CertService).certCheck(mobile, area).then(result => {
if (!result || !result.code) {
return {code: 400, message: '', data: ''};
} else if (result.code === 200) {
let nextUrl;
if (_.get(result, 'data.isCanBind') === 'Y') {
nextUrl = helpers.urlFormat('/passport/cert/cert');
return {code: 200, message: result.message, data: {next: nextUrl}};
} else {
// 不可以关联
return {code: 403, message: '绑定失败,该手机号已注册,请更换或使用该手机号直接登录'};
}
} else {
return {code: result.code, message: result.message, data: result.data ? result.data : ''};
}
}).then(result => {
return res.json(result);
}).catch(next);
} else {
return res.json({code: 400, message: '', data: ''});
}
},
sendCertMsg: (req, res, next) => {
let mobile = req.body.mobile;
let area = req.body.area;
let id = req.session.id;
let captcha = req.body.verifyCode;
req.ctx(CertService).sendCertMsg(mobile, area, id, 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 uid = req.user.uid;
let mobile = _.trim(req.body.mobile);
let area = _.trim(req.body.area) || '86';
let code = _.trim(req.body.code);
let refer = req.body.refer;
req.ctx(CertService).certMobile(uid, mobile, code, area).then(result => {
if (result && result.code === 200) {
let url = helpers.urlFormat('/passport/cert/success', {
type: 'cert',
refer: refer
});
return {
code: 200,
message: result.message,
data: {
nextUrl: url
}
};
} else {
return {
code: _.get(result, 'code', 400),
message: _.get(result, 'message') || '操作错误,请稍后再试!'
};
}
}).then(result => {
res.json(result);
}).catch(next);
},
relateMobile: (req, res, next) => {
let uid = req.user.uid;
let mobile = _.trim(req.body.mobile);
let area = _.trim(req.body.areaCode) || '86';
let code = _.trim(req.body.code);
let refer = req.body.refer;
let email;
req.ctx(UserService).profile(uid).then(info => {
if (info && info.code === 200) {
email = _.get(info, 'data.email');
} else {
return res.json({
code: 400,
message: '系统错误,请稍后再试!'
});
}
req.ctx(CertService).relateMobile(uid, email, code, mobile, area).then(result => {
if (result && result.code === 200) {
let url = helpers.urlFormat('/passport/cert/success', {
type: 'relate',
refer: refer
});
return {
code: 200,
message: result.message,
data: {
nextUrl: url
}
};
} else {
return {
code: _.get(result, 'code', 400),
message: _.get(result, 'message')
};
}
}).then(result => {
res.json(result);
}).catch(next);
}).catch(next);
}
};
module.exports = cert;