Authored by htoooth

fix reg

... ... @@ -87,6 +87,7 @@ const requiredAPI = (req, res, next) => {
}
});
} else {
req.body.verifyCode = config.UNIVERSAL_CAPTCHA;
return next();
}
});
... ...
... ... @@ -35,7 +35,15 @@ const generate = (req, res, next) => {
req.ctx(CaptchaServiceModel).generateCaptcha(req.session.id).then((result) => {
res.type('png');
if (result.code === 200) {
request(result.data.url).pipe(res);
request({
url: result.data.url,
headers: {
'X-request-ID': req.reqID || '',
'X-YOHO-IP': req.yoho.clientIp || '',
'X-Forwarded-For': req.yoho.clientIp || '',
'User-Agent': 'yoho/nodejs'
}
}).pipe(res);
}
}).catch(next);
... ...
... ... @@ -6,12 +6,11 @@ const _ = require('lodash');
const Promise = require('bluebird');
const passportHelper = require('../models/passport-helper');
const RegService = require('../models/reg-service');
const UserService = require('../models/user-service');
const LoginService = require('../models/login-service');
const config = require('../../../config/common');
const simpleHeaderModel = require('../../../doraemon/models/simple-header');
let helpers = global.yoho.helpers;
let cache = global.yoho.cache;
let cookie = global.yoho.cookie;
/**
... ... @@ -99,45 +98,6 @@ let index = (req, res, next) => {
};
/**
* 检查手机号码是否注册过
* @Deprecated 因为安全原因,请不要使用该接口
*/
let checkMobile = (req, res, next) => {
let data = {
code: 400
};
let mobile = req.body.mobile;
let area = req.body.area || '86';
// 判断手机号是否检查超过指定次数
let regCheckKey = `regCheckMobileNum_${area}-${mobile}`;
cache.get(regCheckKey).then((checkNum) => {
checkNum = +(checkNum || 0);
return cache.set(regCheckKey, ++checkNum).then(() => {
return checkNum;
});
}).then((checkNum) => {
if (checkNum > 500) {
data.message = '检查次数太多';
return res.json(data);
}
// 判断用户是否存在
return req.ctx(UserService).findByMobileAsync(area, mobile).then((user) => {
if (!_.isEmpty(user)) {
data.message = '手机号码已经存在';
return res.json(data);
}
data.code = 200;
return res.json(data);
});
}).catch(next);
};
/**
* checkMobile的伪装,始终成功。
*/
let fakeCheckMobile = (req, res) => {
... ... @@ -147,17 +107,6 @@ let fakeCheckMobile = (req, res) => {
};
/**
* 图形验证码校验
*/
let picCaptcha = (req, res) => {
// 图形验证码已经在中间件校验,如果走到这儿,说明通过校验
return res.json({
code: 200,
message: '验证码正确'
});
};
/**
* 发送验证码
*/
let sendBindMsg = (req, res, next) => {
... ... @@ -201,7 +150,7 @@ let msgCaptcha = (req, res, next) => {
if (result.code) {
return res.json(result);
} else {
data.message = '验证码错误';
data.message = '短信验证码错误';
return res.json(data);
}
}).catch(next);
... ... @@ -222,47 +171,21 @@ let mobileRegister = (req, res, next) => {
return res.json(data);
}
/* 判断是否是有效的注册方式,防注册机刷 */
let regExpireTime = req.session.REG_EXPIRE_;
if (!regExpireTime || regExpireTime < Date.now()) {
data.message = '注册超时';
return res.json(data);
}
/* IP仅允许点击注册500次/时 */
let ipKey = 'ip_register_' + req.ip;
let ipTimes = yield cache.get(ipKey);
if (!ipTimes) {
ipTimes = 0;
} else {
ipTimes = +ipTimes;
}
if (ipTimes >= 500) {
data.message = '由于你IP受限无法注册';
return res.json(data);
}
yield cache.set(ipKey, ipTimes + 1, 3600);
let area = req.body.area;
let mobile = req.body.mobile;
let code = req.body.code; // 短信验证码
let password = req.body.password;
let inviteCode = req.body.inviteCode;
let clientIp = req.yoho.clientIp;
let result = yield req.ctx(RegService).validMobileCode(area, mobile, code); // 验证注册的标识码是否有效
if (!result.code || result.code !== 200) {
data.message = '验证码错误';
data.message = '短信验证码错误';
return res.json(data);
}
/* 手机注册: 调用注册接口*/
let regResult = yield req.ctx(RegService).regMobileAes(
area, mobile, password, code, cookie.getShoppingKey(req), inviteCode, clientIp
area, mobile, password, code, cookie.getShoppingKey(req), inviteCode
);
if (!regResult.code || regResult.code !== 200) {
... ... @@ -314,8 +237,6 @@ module.exports = {
checkPassword,
index,
success,
checkMobile,
picCaptcha,
sendBindMsg,
msgCaptcha,
mobileRegister,
... ...
... ... @@ -4,6 +4,9 @@
'use strict';
const passportHelper = require('./passport-helper');
const Api = require('./reg-api');
const CaptchaServiceModel = require('./captcha-img-service');
const _ = require('lodash');
const REGISTER_LEFT_BANNER_CODE = 'c479ec90120cae7f96e52922b4917064'; // 注册左边的banner
... ... @@ -12,13 +15,23 @@ module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.api = new Api(ctx);
this.captchaService = new CaptchaServiceModel(ctx);
this.sendCodeToMobile = this.api.sendCodeToMobile.bind(this.api);
this.validMobileCode = this.api.validMobileCode.bind(this.api);
this.regMobileAes = this.api.regMobileAes.bind(this.api);
}
getRegData() {
return passportHelper.getLeftBannerAsync(REGISTER_LEFT_BANNER_CODE);
}
async validMobileCode(area, mobile, code) {
let result = await this.api.validMobileCode(area, mobile, code);
let captchaNeeded = await this.captchaService.try();
_.set(result, 'data.needCaptcha', captchaNeeded);
return result;
}
};
... ...
... ... @@ -13,6 +13,7 @@ var $registerPage = $('.register-page'),
$pwdTip1 = $pwdTips.find('#pwd-tip1'),
$errTip = $('#err-tip'),
$registerBtn = $('#register-btn'),
$captchaImageWrap = $('#captcha-img'),
captchaImage = new Captcha('#captcha-img', {checkURI: ''}).init();
var $sendCaptcha = $('#send-captcha'),
... ... @@ -55,6 +56,10 @@ $pwd.focus(function() {
// IE8 placeholder
$('[placeholder]').placeholder();
function showCaptcha() {
$captchaImageWrap.removeClass('hide');
}
// 存储校验信息
validateResult = [
{
... ... @@ -63,11 +68,6 @@ validateResult = [
status: false // 当前的状态
},
{
id: 'captcha-img',
message: '',
status: false
},
{
id: 'msg-captcha',
message: '',
status: false
... ... @@ -84,11 +84,6 @@ validateResult = [
}
];
captchaImage.onRefresh(function() {
validateResult[1].message = 'err';
validateResult[1].status = false;
});
// 刷新图形验证码
function refreshPic() {
captchaImage.refresh();
... ... @@ -96,24 +91,11 @@ function refreshPic() {
// 手机号ajax校验
function phoneAjaxFn(page, callback) {
var url,
data;
if (page === 'reg') {
url = '/passport/reg/checkmobile';
data = {
mobile: $pn.val(),
area: $region.text().split('+')[1]
};
} else if (page === 'third') {
url = '/passport/autouserinfo/bindCheck';
data = {
mobile: $pn.val(),
area: $region.text().split('+')[1],
openId: $('#open-id').val(),
sourceType: $('#source-type').val()
};
}
var url = '/passport/reg/checkmobile';
var data = {
mobile: $pn.val(),
area: $region.text().split('+')[1]
};
validateResult[0].message = 'err';
validateResult[0].status = false;
... ... @@ -138,8 +120,6 @@ function phoneAjaxFn(page, callback) {
// 如果手机号发生改变则刷新验证码并且更新状态
refreshPic();
validateResult[1].message = 'err';
validateResult[1].status = false;
}
oldPhone = $pn.val();
... ... @@ -159,26 +139,10 @@ function phoneAjaxFn(page, callback) {
});
}
// 图形验证码ajax校验
function picCaptchaAjaxFn(callback) {
return captchaImage.check().then(function() {
validateResult[1].message = '';
validateResult[1].status = true;
}).fail(function() {
validateResult[1].message = 'err';
validateResult[1].status = false;
}).always(callback);
}
// 短信验证码ajax校验
function msgCaptchaAjaxFn(page, callback) {
var url;
if (page === 'reg') {
url = '/passport/reg/msgcaptcha';
} else if (page === 'third') {
url = '/passport/autouserinfo/checkBindMsg';
}
var url = '/passport/reg/msgcaptcha';
return $.ajax({
type: 'POST',
... ... @@ -199,14 +163,16 @@ function msgCaptchaAjaxFn(page, callback) {
switch (data.code) {
case 200:
validateResult[2].message = '';
validateResult[2].status = true;
validateResult[1].message = '';
validateResult[1].status = true;
return defer.resolve().promise();
case 400:
validateResult[2].message = '短信验证码错误';
validateResult[2].status = false;
return defer.reject().promise();
default:
validateResult[1].message = data.message;
validateResult[1].status = false;
if (data.data && data.data.needCaptcha) {
showCaptcha();
}
return defer.reject().promise();
}
});
... ... @@ -247,59 +213,39 @@ function validateRule(page, $element, callback) {
}
}
// 图形验证码校验
} else if ($element.hasClass('w330')) {
// 并且手机号正确
if (validateResult[0].status) {
return picCaptchaAjaxFn(callback);
} else {
validateResult[1].message = 'err';
validateResult[1].status = false;
return defer.reject().promise();
}
// 短信验证码校验
} else if ($element.hasClass('msg-captcha')) {
if (val === '') {
validateResult[2].message = '请输入短信验证码';
validateResult[2].status = false;
validateResult[1].message = '请输入短信验证码';
validateResult[1].status = false;
return defer.reject().promise();
} else if (val.length <= 3) {
validateResult[2].message = '短信验证码错误';
validateResult[2].status = false;
validateResult[1].message = '验证码错误';
validateResult[1].status = false;
return defer.reject().promise();
} else {
// 并且图形验证码正确
if (validateResult[1].status) {
return msgCaptchaAjaxFn(page, callback);
} else {
validateResult[2].message = '短信验证码错误';
validateResult[2].status = false;
return defer.reject().promise();
}
return msgCaptchaAjaxFn(page, callback);
}
// 密码校验
} else if ($element.hasClass('pwd')) {
if (val === '') {
validateResult[3].message = '请输入密码';
validateResult[3].status = false;
validateResult[2].message = '请输入密码';
validateResult[2].status = false;
return defer.reject().promise();
} else if (val.length < 6 || val.length > 20) {
validateResult[3].message = '密码只支持6-20位字符';
validateResult[3].status = false;
validateResult[2].message = '密码只支持6-20位字符';
validateResult[2].status = false;
return defer.reject().promise();
} else if (!pwdReg.test($element.val())) {
validateResult[3].message = '密码须字母和数字组合';
validateResult[3].status = false;
validateResult[2].message = '密码须字母和数字组合';
validateResult[2].status = false;
return defer.reject().promise();
} else {
validateResult[3].message = '';
validateResult[3].status = true;
validateResult[2].message = '';
validateResult[2].status = true;
return defer.resolve().promise();
}
}
... ... @@ -507,18 +453,13 @@ exports.init = function(page) {
// 点击发送验证码
$sendCaptcha.click(function() {
var t,
url;
var t;
var url = '/passport/reg/sendBindMsg';
if ($(this).hasClass('disable')) {
return;
}
if (page === 'reg') {
url = '/passport/reg/sendBindMsg';
} else if (page === 'third') {
url = '/passport/autouserinfo/sendBindMsg';
}
// 1.验证图形验证码是否正确
// 2.验证手机号被注册过
... ... @@ -527,11 +468,6 @@ exports.init = function(page) {
showErrTip(); // 显示错误提示
showBorder(); // 显示红色边框
}).then(function() {
return validateRule(page, $('#captcha-img'), function() {
showErrTip(); // 显示错误提示
showBorder(); // 显示红色边框
});
}).then(function() {
return $.ajax({
type: 'POST',
url: url,
... ... @@ -595,13 +531,7 @@ exports.init = function(page) {
// ajax表单提交
$registerBtn.click(function() {
var url;
if (page === 'reg') {
url = '/passport/reg/mobileregister';
} else if (page === 'third') {
url = '/passport/autouserinfo/bindMobile';
}
var url = '/passport/reg/mobileregister';
if ($(this).hasClass('disable')) {
return;
... ...