bind.js
15.9 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
/**
* 手机号绑定功能
*
* @author JiangFeng<jeff.jiang@yoho.cn>
* @date 2016/06/21
*/
'use strict';
const helpers = global.yoho.helpers;
const config = global.yoho.config;
const RegServiceModel = require('../models/reg-service');
const BindServiceModel = require('../models/bind-service');
const AuthHelperModel = require('../models/auth-helper');
const _ = require('lodash');
const headerModel = require('../../../doraemon/models/header'); // 头部model
const Sources = {
qq: 'QQ',
sina: '微博',
alipay: '支付宝',
wechat: '微信'
};
const bind = {
indexPage: (req, res) => {
// 如果没有originalUrl,判定为非法链接
if (req.session.originalUrl !== 'true') {
return res.redirect('//m.yohobuy.com');
}
let refer = req.get('Referer');
if (refer) {
res.cookie('refer', encodeURI(refer), {
domain: 'yohobuy.com'
});
}
let openId = req.query.openId;
let sourceType = req.query.sourceType;
let nickname = req.query.nickname;
let serviceUrl = helpers.urlFormat('/service/im');
res.render('bind/index', {
captchaShow: req.yoho.captchaShow,
bindIndex: true, // js标识
backUrl: helpers.urlFormat('/signin.html'), // 返回的URL链接
showHeaderImg: true, // 控制显示头部图片
isPassportPage: true, // 模板中模块标识
sourceType: sourceType, // 第三方登录来源
platform: Sources[sourceType],
isWechatLogin: sourceType === 'wechat',
openId: openId, // openId,
nickname: nickname,
areaCode: '+86', // 默认区号
countrys: req.ctx(RegServiceModel).getAreaData(), // 国别码
serviceUrl: serviceUrl, // 在线客服
module: 'passport',
page: 'bind',
title: '绑定手机号',
width750: true
});
},
codePage: (req, res) => {
let openId = req.query.openId;
let sourceType = req.query.sourceType;
let nickname = req.query.nickname;
let areaCode = req.query.areaCode || '86';
let isReg = req.query.is_register;
let phoneNum = req.query.phoneNum;
res.render('bind/code', {
captchaShow: req.yoho.captchaShow,
backUrl: helpers.urlFormat('/signin.html'),
showHeaderImg: true,
isPassportPage: true,
sourceType: sourceType,
openId: openId,
nickname: nickname,
isReg: isReg,
areaCode: areaCode,
phoneNum: phoneNum,
relateCode: isReg === '3',
bindCode: isReg !== '3',
module: 'passport',
page: isReg === '3' ? 'relate' : 'bind-code',
title: '验证手机',
width750: true
});
},
bindCheck: (req, res, next) => {
let phoneNum = req.body.phoneNum;
let openId = req.body.openId;
let areaCode = req.body.areaCode || '86';
let sourceType = req.body.sourceType;
let nickname = req.body.nickname;
if (_.isNumber(parseInt(phoneNum, 0)) && openId && areaCode && sourceType) {
req.ctx(BindServiceModel).bindCheck(phoneNum, openId, sourceType, areaCode).then(result => {
let data = {
code: result.code,
message: result.message,
data: {}
};
if (result.code === 200) {
let nextUrl = helpers.urlFormat('/passport/bind/code', {
is_register: result.data.is_register,
openId: openId,
sourceType: sourceType,
nickname: nickname,
areaCode: areaCode,
phoneNum: phoneNum
});
data.data.is_register = result.data.is_register;
data.data.next = nextUrl;
} else {
data.data = result.data;
}
res.json(data);
}).catch(next);
} else {
res.json({
code: 400,
message: '',
data: ''
});
}
},
sendBindMsg: (req, res, next) => {
let phoneNum = req.body.phoneNum;
let areaCode = req.body.areaCode || '86';
let udid = req.cookies.udid;
let captcha = req.body.captcha;
let superCapture = '';
if (req.xhr && _.isNumber(parseInt(phoneNum, 0)) && areaCode) {
if (req.body.geetest_challenge) {
captcha = '';
superCapture = config.superCapture;
}
req.ctx(BindServiceModel).sendBindMsg({
area: areaCode,
mobile: phoneNum,
udid,
captcha,
superCapture
}).then(result => {
if (result && result.code) {
res.json(result);
} else {
res.json({ code: 400, message: '', data: '' });
}
}).catch(next);
} else {
res.json({ code: 400, message: '', data: '' });
}
},
checkBindMsg: (req, res, next) => {
let phoneNum = req.body.phoneNum;
let code = req.body.code;
let areaCode = req.body.areaCode;
if (_.isNumber(parseInt(phoneNum, 0)) && code && areaCode) {
req.ctx(BindServiceModel).checkBindMsg(areaCode, phoneNum, code).then(result => {
if (result && result.code) {
res.json(result);
} else {
res.json({ code: 400, message: '', data: '' });
}
}).catch(next);
} else {
res.json({ code: 400, message: '', data: '' });
}
},
bindMobile: (req, res, next) => {
let phoneNum = req.body.phoneNum;
let openId = req.body.openId;
let areaCode = req.body.areaCode || '86';
let sourceType = req.body.sourceType;
let code = req.body.code;
let nickname = req.body.nickname;
let password = req.body.password || '';
let from = req.cookies.from || 'yohobuy';
if (_.isNumber(parseInt(phoneNum, 0)) && openId && sourceType && nickname && areaCode && code) {
req.ctx(BindServiceModel).bindMobile({
openId, sourceType, phoneNum,
areaCode, password, from, nickname, code
}).then(result => {
let refer = helpers.urlFormat('/passport/bind/success?type=bind');
if (result && result.code && result.code === 200 && result.data.uid) {
return req.ctx(AuthHelperModel).syncUserSession(result.data.uid, req, res, result.data.session_key)
.then((authData) => {
result.data.refer = _.get(authData, 'refer') || refer;
return result;
});
} else {
return { code: 400, message: '绑定失败', data: '' };
}
}).then(result => {
res.json(result);
}).catch(next);
} else {
res.json({ code: 400, message: '', data: '' });
}
},
relateMobile: (req, res, next) => {
let phoneNum = req.body.phoneNum;
let openId = req.body.openId;
let areaCode = req.body.areaCode || '86';
let sourceType = req.body.sourceType;
let code = req.body.code;
if (_.isNumber(parseInt(phoneNum, 0)) && openId && areaCode && sourceType && code) {
req.ctx(BindServiceModel).checkBindCode(areaCode, phoneNum, code).then(result => {
if (result && result.code && result.code === 200) {
return req.ctx(BindServiceModel).relateMobile(openId, sourceType, phoneNum, areaCode);
} else {
return { code: 400, message: '短信验证码错误', data: '' };
}
}).then(result => {
let refer = helpers.urlFormat('/passport/bind/success', { sourceType: sourceType });
if (result && result.code && result.code === 200 && result.data.uid) {
return req.ctx(AuthHelperModel).syncUserSession(result.data.uid, req, res, result.data.session_key)
.then((authData) => {
result.data.refer = _.get(authData, 'refer') || refer;
return result;
});
} else {
return { code: 400, message: '关联失败', data: '' };
}
}).then(result => {
res.json(result);
}).catch(next);
} else {
res.json({ code: 400, message: '', data: '' });
}
},
passwordPage: (req, res) => {
let openId = req.query.openId;
let sourceType = req.query.sourceType;
let nickname = req.query.nickname;
let areaCode = req.query.areaCode || '86';
let phoneNum = req.query.phoneNum;
let code = req.query.code;
res.render('bind/password', {
module: 'passport',
page: 'bind-password',
bindPwd: true, // js标识
backUrl: helpers.urlFormat('/signin.html'), // 返回的URL链接
showHeaderImg: true, // 控制显示头部图片
isPassportPage: true, // 模板中模块标识
sourceType: sourceType, // 第三方登录来源
nickname: nickname,
openId: openId, // openId
areaCode: areaCode, // 国别码
phoneNum: phoneNum, // 国别码
code: code // 验证码
});
},
successPage: (req, res) => {
let refer = req.cookies.refer;
if (!refer || /signin|login|\/bind\//.test(refer)) {
refer = helpers.urlFormat('/?go=1');
}
// 微信免单活动,判断来源地址 2.16.12.28 by jing.li@yoho.cn
res.cookie('bindUrl', 'http://m.yohobuy.com/passport/bind/success?type=bind');
res.render('bind/success', {
isPassportPage: true,
successTip: '恭喜您,绑定手机号码成功!',
goUrl: refer,
module: 'passport',
page: 'bind-success',
title: '绑定手机号'
});
},
changeCheck: (req, res, next) => {
let phoneNum = req.body.phoneNum;
let areaCode = req.body.areaCode;
if (_.isNumber(parseInt(phoneNum, 0)) && areaCode) {
req.ctx(BindServiceModel).changeCheck(phoneNum, areaCode).then(result => {
res.json(result);
}).catch(next);
} else {
res.json({ code: 400, message: '', data: '' });
}
},
changeMobile: (req, res, next) => {
let uid = req.user.uid;
let phoneNum = req.body.phoneNum;
let areaCode = req.body.areaCode;
let code = req.body.code;
if (_.isNumber(parseInt(phoneNum, 0)) && uid && areaCode && code) {
req.ctx(BindServiceModel).changeMobile(uid, phoneNum, areaCode, code).then(result => {
res.json(result);
}).catch(next);
} else {
res.json({ code: 400, message: '', data: '' });
}
},
// 第三方绑定手机号页面
thirdBindMobilePage: (req, res) => {
let headerData = headerModel.setNav({
navTitle: '绑定手机号', // 标题
navBack: true, // 是否显示返回按钮
backUrl: helpers.urlFormat('/signin.html'), // 返回按钮对应的url
navBtn: false // 顶部下拉菜单选项
});
res.render('bind/bind-mobile', {
captchaShow: req.yoho.captchaShow,
width750: true,
pageHeader: headerData,
localCss: true,
module: 'passport',
page: 'third-bind-mobile',
btnName: '绑定手机号',
areaCode: '+86', // 默认区号
countrys: req.ctx(RegServiceModel).getAreaData(), // 国别码
});
},
// 第三方绑定手机号发送短信
thirdSendMsgApi: (req, res, next) => {
req.ctx(BindServiceModel).sendThirdBindMobileCodeOnly(req.body).then(result => {
if (_.get(result, 'code', 400) === 200) {
return next();
}
return res.json(result);
}).catch(next);
},
// 第三方调用绑定接口
thirdBindMobileApi: (req, res, next) => {
if (_.isEmpty(req.body.open_id) || _.isEmpty(req.body.source_type)) {
return res.json({code: 401, data: {}, message: '请重新登录授权'});
}
req.ctx(BindServiceModel).thirdBindMobile(req.body).then(result => {
if (_.get(result, 'code', 400) !== 200) {
return res.json(result);
}
return req.ctx(AuthHelperModel).syncUserSession(
result.data.uid,
req,
res,
result.data.session_key
).then(() => {
return res.json({code: 200, data: {}, message: 'success'});
});
}).catch(next);
},
// 通过邮箱绑定手机号发送短信
sendMsgApi: (req, res, next) => {
req.ctx(BindServiceModel).sendChangeBindMobileCodeOnly(req.body).then(result => {
if (_.get(result, 'code', 400) === 200) {
return next();
}
return res.json(result);
}).catch(next);
},
// 检查手机是否绑定
changeMobileCheckApi: (req, res, next) => {
req.ctx(BindServiceModel).changeMobileCheck(req.body).then(result => {
if (_.get(result, 'data.isBind', '') === 'N' &&
_.get(result, 'data.isRegister', '') === 'N') {
return next();
}
return res.json(result);
}).catch(next);
},
// 通过邮箱强制绑定手机号页面
forceBindMobilePage: (req, res) => {
let headerData = headerModel.setNav({
navTitle: '绑定手机号', // 标题
navBack: true, // 是否显示返回按钮
backUrl: helpers.urlFormat('/signin.html'), // 返回按钮对应的url
navBtn: false // 顶部下拉菜单选项
});
res.render('bind/bind-mobile', {
captchaShow: req.yoho.captchaShow,
width750: true,
pageHeader: headerData,
localCss: true,
module: 'passport',
page: 'bind-email-mobile',
btnName: '绑定手机号',
areaCode: '+86', // 默认区号
countrys: req.ctx(RegServiceModel).getAreaData(), // 国别码
});
},
forceSendMsg: (req, res) => {
return res.json({code: 200, data: {}, message: 'success'});
},
isCheckEmailPassword: (req, res, next) => {
if (!req.session.bindEmail || !req.session.bindPassword) {
return res.json({code: 401, data: {}, message: '请重新登录授权'});
}
return next();
},
// 调用绑定接口
forceBindMobileApi: (req, res, next) => {
req.ctx(BindServiceModel).bindEmailLoginMobile(Object.assign(req.body, {
email: req.session.bindEmail,
password: req.session.bindPassword,
})).then(result => {
if (_.get(result, 'code', 400) !== 200) {
return res.json(result);
}
return req.ctx(AuthHelperModel).syncUserSession(
result.data.uid,
req,
res,
result.data.session_key
).then(() => {
delete req.session.bindEmail;
delete req.session.bindPassword;
return res.json({code: 200, data: {}, message: 'success'});
});
}).catch(next);
}
};
module.exports = bind;