...
|
...
|
@@ -12,6 +12,8 @@ const RegService = require('../models/reg-service'); |
|
|
const BindService = require('../models/bind-service');
|
|
|
const AuthHelper = require('../models/auth-helper');
|
|
|
const _ = require('lodash');
|
|
|
const config = global.yoho.config;
|
|
|
const moment = require('moment');
|
|
|
|
|
|
const Sources = {
|
|
|
qq: 'QQ',
|
...
|
...
|
@@ -87,32 +89,57 @@ const bind = { |
|
|
let openId = req.body.openId;
|
|
|
let areaCode = req.body.areaCode || '86';
|
|
|
let sourceType = req.body.sourceType;
|
|
|
let verify = req.body.verify || '';
|
|
|
let testCode = req.body.yohobuy;
|
|
|
|
|
|
if (_.isNumber(parseInt(phoneNum, 0)) && openId && areaCode && sourceType) {
|
|
|
BindService.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', {
|
|
|
isReg: result.data.is_register, // esline-disable-line
|
|
|
openId: openId,
|
|
|
sourceType: sourceType,
|
|
|
areaCode: areaCode,
|
|
|
phoneNum: phoneNum
|
|
|
});
|
|
|
|
|
|
data.data.isReg = result.data.is_register;
|
|
|
data.data.next = nextUrl;
|
|
|
if (verify) {
|
|
|
let captcha = _.get(req, 'session.captcha');
|
|
|
|
|
|
if ((captcha && verify.toString() === captcha) || (testCode === config.testCode)) {
|
|
|
BindService.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', {
|
|
|
isReg: result.data.is_register, // esline-disable-line
|
|
|
openId: openId,
|
|
|
sourceType: sourceType,
|
|
|
areaCode: areaCode,
|
|
|
phoneNum: phoneNum
|
|
|
});
|
|
|
|
|
|
data.data.isReg = result.data.is_register;
|
|
|
data.data.next = nextUrl;
|
|
|
|
|
|
_.set(req.session, 'bind.area', areaCode);
|
|
|
_.set(req.session, 'bind.phoneNum', phoneNum);
|
|
|
} else {
|
|
|
data.data = result.data;
|
|
|
}
|
|
|
|
|
|
res.json(data);
|
|
|
}).catch(next);
|
|
|
} else {
|
|
|
data.data = result.data;
|
|
|
let jsonData = {
|
|
|
code: 400,
|
|
|
message: '请将图片旋转到正确位置'
|
|
|
};
|
|
|
|
|
|
if (req.session.captchaValidCount === 0) {
|
|
|
req.session.captcha = null; // 验证码 用过就扔
|
|
|
jsonData.changeCaptcha = true;
|
|
|
}
|
|
|
|
|
|
return res.json(jsonData);
|
|
|
}
|
|
|
|
|
|
res.json(data);
|
|
|
}).catch(next);
|
|
|
_.set(req.session, 'captcha', null);
|
|
|
}
|
|
|
} else {
|
|
|
res.json({
|
|
|
code: 400,
|
...
|
...
|
@@ -122,13 +149,62 @@ const bind = { |
|
|
}
|
|
|
},
|
|
|
|
|
|
// 防刷
|
|
|
sendBefore: (req, res, next) => {
|
|
|
let count = _.get(req.session, 'bind.count');
|
|
|
let interval = _.get(req.session, 'bind.interval');
|
|
|
|
|
|
let now = Date.now();
|
|
|
|
|
|
// // 重发次数用完了, 会冻结5min
|
|
|
// // 1. 过了冻结期, count 重设为 5次
|
|
|
// // 2. 没过冻结期, end
|
|
|
// // 没有用完, 判断是否请求太频繁
|
|
|
let during = moment.duration(interval - now, 'ms').minutes();
|
|
|
let message = `请${during}分钟后再试`;
|
|
|
|
|
|
if (!count) {
|
|
|
if (interval > now) {
|
|
|
return res.json({
|
|
|
code: 400,
|
|
|
message: message,
|
|
|
during: Math.ceil((interval - now) / 1000)
|
|
|
});
|
|
|
} else {
|
|
|
_.set(req.session, 'bind.count', 5);
|
|
|
}
|
|
|
} else if (interval > now) {
|
|
|
return res.json({
|
|
|
code: 429,
|
|
|
message: message
|
|
|
});
|
|
|
}
|
|
|
|
|
|
next();
|
|
|
},
|
|
|
|
|
|
sendBindMsg: (req, res, next) => {
|
|
|
let phoneNum = req.body.phoneNum;
|
|
|
let areaCode = req.body.areaCode;
|
|
|
let phoneNum = _.get(req.session, 'bind.phoneNum');
|
|
|
let areaCode = _.get(req.session, 'bind.area');
|
|
|
|
|
|
if (req.xhr && _.isNumber(parseInt(phoneNum, 0)) && areaCode) {
|
|
|
BindService.sendBindMsg(areaCode, phoneNum).then(result => {
|
|
|
if (result && result.code) {
|
|
|
|
|
|
if (result.code === 200) {
|
|
|
|
|
|
_.set(req.session, 'bind.area', areaCode);
|
|
|
_.set(req.session, 'bind.phoneNum', phoneNum);
|
|
|
|
|
|
--req.session.bind.count;
|
|
|
|
|
|
if (!req.session.bind.count) {
|
|
|
_.set(req.session, 'bind.interval', Date.now() + 5 * 60 * 1000);
|
|
|
} else {
|
|
|
_.set(req.session, 'bind.interval', Date.now() + 60 * 1000);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
res.json(result);
|
|
|
} else {
|
|
|
res.json({ code: 400, message: '', data: '' });
|
...
|
...
|
|