Authored by 毕凯

注册代码整理

... ... @@ -14,6 +14,37 @@ let cache = global.yoho.cache;
let cookie = global.yoho.cookie;
/**
* 检查图形验证码
*/
let checkCode = (req, res, next) => {
let verifyCode = _.trim(req.body.verifyCode);
if (verifyCode !== req.session.captcha) {
return res.json({
code: 400,
message: '验证码错误'
});
}
next();
};
/**
* 检查手机格式
*/
let checkMobileMiddleware = (req, res, next) => {
let mobile = +req.body.mobile;
let area = +req.body.area;
if (!_.isNumber(mobile) || !_.isNumber(area)) {
return res.json({
code: 400,
message: '手机号码格式不正确'
});
}
next();
};
/**
* 注册首页
*/
let index = (req, res, next) => {
... ... @@ -54,33 +85,31 @@ let checkMobile = (req, res, next) => {
let mobile = +req.body.mobile;
let area = +req.body.area;
if (!_.isNumber(mobile) || !_.isNumber(area)) {
data.message = '手机号码格式不正确';
return res.json(data);
}
// 判断手机号是否检查超过指定次数
let regCheckKey = 'regCheckMobileNum_' + passportHelper.makeAreaMobile(area, mobile);
let checkNum = req.session[regCheckKey];
checkNum = +(checkNum || 0);
req.session[regCheckKey] = ++checkNum;
if (checkNum > 500) {
data.message = '检查次数太多';
return res.json(data);
}
cache.get(regCheckKey).then((checkNum) => {
checkNum = +(checkNum || 0);
// 判断用户是否存在
return userService.findByMobileAsync(area, mobile).then((user) => {
if (!_.isEmpty(user)) {
data.message = '手机号码已经存在';
return cache.set(regCheckKey, ++checkNum).then(() => {
return checkNum;
});
}).then((checkNum) => {
if (checkNum > 500) {
data.message = '检查次数太多';
return res.json(data);
}
data.code = 200;
return res.json(data);
// 判断用户是否存在
return userService.findByMobileAsync(area, mobile).then((user) => {
if (!_.isEmpty(user)) {
data.message = '手机号码已经存在';
return res.json(data);
}
data.code = 200;
return res.json(data);
});
}).catch(next);
};
... ... @@ -88,15 +117,7 @@ let checkMobile = (req, res, next) => {
* 图形验证码校验
*/
let picCaptcha = (req, res) => {
let verifyCode = _.trim(req.body.verifyCode);
if (verifyCode !== req.session.captcha) {
return res.json({
code: 400,
message: '验证码错误'
});
}
// 图形验证码已经在中间件校验,如果走到这儿,说明通过校验
return res.json({
code: 200,
message: '验证码正确'
... ... @@ -116,57 +137,45 @@ let sendBindMsg = (req, res, next) => {
let mobile = +req.body.mobile;
let area = +req.body.area;
// 判断参数是否合法
if (!_.isNumber(mobile) || !_.isNumber(area)) {
data.message = '手机号码格式不正确';
return res.json(data);
}
let verifyCode = _.trim(req.body.verifyCode);
if (verifyCode !== req.session.captcha) {
data.message = '验证码不正确';
return res.json(data);
}
// 检查是否检查过
let regCheckKey = 'regCheckMobileNum_' + passportHelper.makeAreaMobile(area, mobile);
let checkNum = req.session[regCheckKey];
if (!checkNum) {
data.message = '发送失败';
return res.json(data);
}
// 校验是否发送过多
let sendCodeKey = `send_code_${area}_${mobile}`;
cache.get(sendCodeKey).then((sendCodeTimes) => {
if (!sendCodeTimes) {
sendCodeTimes = 0;
} else {
sendCodeTimes = +sendCodeTimes;
}
if (sendCodeTimes >= 10) {
data.message = '您已多次提交验证码,请尽快联系客服解决';
cache.get(regCheckKey).then((checkNum) => {
if (!checkNum) {
data.message = '发送失败';
return res.json(data);
}
if (sendCodeTimes >= 5) {
data.message = '您收到的验证码短信已超过本日限定最多次数,请您耐心等待';
return res.json(data);
}
return cache.get(sendCodeKey).then((sendCodeTimes) => {
if (!sendCodeTimes) {
sendCodeTimes = 0;
} else {
sendCodeTimes = +sendCodeTimes;
}
/* 向手机发送注册验证码 */
return regService.sendCodeToMobile(area, mobile).then((result) => {
return cache.set(sendCodeKey, sendCodeTimes + 1, 3600).then(() => {
if (result.code) {
return res.json(result);
} else {
data.message = '发送失败';
return res.json(data);
}
if (sendCodeTimes >= 10) {
data.message = '您已多次提交验证码,请尽快联系客服解决';
return res.json(data);
}
if (sendCodeTimes >= 5) {
data.message = '您收到的验证码短信已超过本日限定最多次数,请您耐心等待';
return res.json(data);
}
/* 向手机发送注册验证码 */
return regService.sendCodeToMobile(area, mobile).then((result) => {
return cache.set(sendCodeKey, sendCodeTimes + 1, 3600).then(() => {
if (result.code) {
return res.json(result);
} else {
data.message = '发送失败';
return res.json(data);
}
});
});
});
}).catch(next);
... ... @@ -181,17 +190,10 @@ let msgCaptcha = (req, res, next) => {
message: '',
data: ''
};
let area = +req.body.area;
let mobile = +req.body.mobile;
let code = +req.body.code; // 短信验证码
// 判断参数是否合法
if (!_.isNumber(mobile) || !_.isNumber(area)) {
data.message = '手机号码格式不正确';
return res.json(data);
}
regService.validMobileCode(area, mobile, code).then((result) => {
if (result.code) {
return res.json(result);
... ... @@ -211,16 +213,9 @@ let mobileRegister = (req, res, next) => {
message: '',
data: ''
};
let area = +req.body.area;
let mobile = +req.body.mobile;
// 判断参数是否合法
if (!_.isNumber(mobile) || !_.isNumber(area)) {
data.message = '手机号码格式不正确';
return res.json(data);
}
/* 判断是否是有效的注册方式,防注册机刷 */
let regExpireTime = req.session._REG_EXPIRE;
... ... @@ -229,13 +224,6 @@ let mobileRegister = (req, res, next) => {
return res.json(data);
}
let verifyCode = _.trim(req.body.verifyCode);
if (verifyCode !== req.session.captcha) {
data.message = '验证码不正确';
return res.json(data);
}
/* 判断密码是否符合规则 */
let code = +req.body.code; // 短信验证码
let password = req.body.password;
... ... @@ -271,33 +259,20 @@ let mobileRegister = (req, res, next) => {
let shoppingKey = cookie.getShoppingKey(req);
/* 手机注册: 调用注册接口,ip限制计数 */
/* 手机注册: 调用注册接口*/
return regService.regMobile(area, mobile, password, shoppingKey).then((regResult) => {
if (!regResult.code || regResult.code !== 200) {
data.message = '注册失败';
return res.json(data);
}
// 返回跳转到来源页面
let refer = req.cookies.refer;
if (refer) {
refer = decodeURI(req.cookies.refer);
} else {
refer = '/?go=1';
}
if (/signin|login/.test(refer)) {
refer = '/?go=1';
}
return authHelper.syncUserSession(regResult.data.uid, req, res).then(() => {
return res.json({
code: 200,
message: '注册成功',
data: {
href: helpers.urlFormat('/passport/reg/success', {
next: refer,
next: cookie.getRefer(req, '/?go=1'),
goShoppingUrl: config.siteUrl
})
}
... ... @@ -327,6 +302,8 @@ let success = (req, res, next) => {
};
module.exports = {
checkCode,
checkMobileMiddleware,
index,
success,
checkMobile,
... ...
... ... @@ -62,11 +62,11 @@ router.post('/autouserinfo/bindMobile', bind.bindMobile);
* 注册页面路由
*/
router.get('/reg/index', reg.index);
router.post('/reg/checkmobile', reg.checkMobile);
router.post('/reg/piccaptcha', reg.picCaptcha);
router.post('/reg/msgcaptcha', reg.msgCaptcha);
router.post('/reg/sendBindMsg', reg.sendBindMsg);
router.post('/reg/mobileregister', reg.mobileRegister);
router.post('/reg/checkmobile', reg.checkMobileMiddleware, reg.checkMobile);
router.post('/reg/piccaptcha', reg.checkCode, reg.picCaptcha);
router.post('/reg/msgcaptcha', reg.checkMobileMiddleware, reg.msgCaptcha);
router.post('/reg/sendBindMsg', reg.checkMobileMiddleware, reg.checkCode, reg.sendBindMsg);
router.post('/reg/mobileregister', reg.checkMobileMiddleware, reg.checkCode, reg.mobileRegister);
router.get('/reg/success', reg.success);
/**
... ...