Authored by htoooth

增加代码

... ... @@ -168,10 +168,7 @@ const local = {
}
});
} else {
let isRemember = req.body.isRemember;
let refer = req.cookies.refer;
if (isRemember) {
if (_.get(req, 'body.isRemember', false)) {
AuthHelper.rememberAccount({
area: req.body.areaCode || '86',
account: req.body.account,
... ... @@ -179,21 +176,23 @@ const local = {
}, req, res);
}
if (refer) {
refer = decodeURI(req.cookies.refer);
} else {
refer = `${config.siteUrl}/home`;
}
let refer = (function() {
if (_.has(req, 'cookies.refer')) {
return decodeURI(req.cookies.refer);
} else if (/sign|login|passport/.test(_.get(req, 'cookies.refer', ''))) {
return `${config.siteUrl}/home`;
} else {
return `${config.siteUrl}/home`;
}
}());
if (/sign|login/.test(refer)) {
refer = `${config.siteUrl}/home`;
}
user.session = refer;
user.href = refer;
AuthHelper.syncUserSession(user.uid, req, res).then(() => {
res.json({
code: 200,
data: user
data: {
session: refer,
href: refer
}
});
});
}
... ... @@ -228,7 +227,9 @@ const local = {
});
}
AuthHelper.sendSmsBySMS(area, mobile).then(res.json).catch(next);
AuthHelper.sendPasswordBySMS(area, mobile).then((result) => {
return res.json(result);
}).catch(next);
},
auth: (req, res, next) => {
let area = req.body.area || '86';
... ... @@ -242,7 +243,8 @@ const local = {
});
}
AuthHelper.verifySmsCodeBySMS(area, mobile, code).then((result) => {
AuthHelper.verifyPasswordBySMS(area, mobile, code).then((result) => {
console.log(result);
if (_.get(result, 'code', 200) !== 200) {
return Promise.reject('error');
}
... ...
... ... @@ -44,7 +44,7 @@ const Auth = {
return api.post('', param);
},
sendSmsBySMS(area, mobile) {
sendPasswordBySMS(area, mobile) {
let param = {
method: 'app.message.sendSms',
area: area,
... ... @@ -61,7 +61,7 @@ const Auth = {
mobile: mobile
});
},
verifySmsCodeBySMS(area, mobile, code) {
verifyPasswordBySMS(area, mobile, code) {
return api.get('', {
method: 'app.message.verifySmsCode',
area: area,
... ...
... ... @@ -60,7 +60,7 @@ var $accountInput = $('#account'),
return $.trim($captchaSmsInput.val());
},
$captchaSmsBtn = $('.change-captcha-sms'),
$captchaSMSToken = $('#captcha-sms-token-hide'),
$captchaSmsTokenHideInput = $('#captcha-sms-token-hide'),
// 区域选择
$countryCodeInput = $('#country-code-hide'),
... ... @@ -126,12 +126,14 @@ var LoginType = {
return defer.promise();
});
},
validateAccountLocal: validateAccountPasswordLocal, // eslint-disable-line
creditableToken: function() {
return getPasswordVal();
},
type: function() {
return 'password';
},
name: 'PasswordLogin',
validate: prePasswordLoginWithValidate // eslint-disable-line
},
SMSLogin: {
... ... @@ -158,12 +160,14 @@ var LoginType = {
return defer.promise();
});
},
validateAccountLocal: validateAccountSmsLocal, // eslint-disable-line
creditableToken: function() {
return getCaptchaSmsVal();
return $captchaSmsTokenHideInput.val();
},
type: function() {
return 'sms';
},
name: 'SMSLogin',
validate: preSmsLoginWithValidate // eslint-disable-line
}
};
... ... @@ -173,18 +177,27 @@ changeLoginTypeEvent.add(function(type) {
currentLogin = LoginType[type];
});
// 清除输入状态
// 重置状态
changeLoginTypeEvent.add(function() {
hideAccountTip(); // eslint-disable-line
hideCaptchaImgTip(); // eslint-disable-line
hideCaptchaSmsTip(); // eslint-disable-line
hidePasswordTip(); // eslint-disable-line
// clear input state
$accountInput.val('');
$passwordInput.val('');
$captchaImgInput.val('');
$captchaSmsInput.val('');
$captchaSmsTokenHideInput.val('');
});
// placeholder 变化
changeLoginTypeEvent.add(function(type) {
if (type === LoginType.SMSLogin.name) {
$accountInput.attr('placeholder', '手机号码');
} else {
$accountInput.attr('placeholder', '邮箱/手机号码');
}
});
/**************************************************************************/
... ... @@ -254,7 +267,7 @@ function hideCaptchaSmsTip() {
/**************************************************************************/
// 本地验证用户输入格式
function validateAccountLocal() {
function validateAccountPasswordLocal() {
var account = getAccountVal(),
countryCode = getAreaCodeVal(),
err;
... ... @@ -292,9 +305,35 @@ function validateAccountLocal() {
return defer.promise();
}
function validateAccountSmsLocal() {
var account = getAccountVal(),
err;
var defer = $.Deferred(); // eslint-disable-line
if (account !== '') {
if (/^[0-9]+$/.test(account)) {
defer.resolve();
} else {
defer.reject();
err = '手机号码不正确,请重新输入';
}
} else {
err = '请输入手机号';
}
if (defer.state() === 'resolved') {
hideAccountTip();
} else {
showAccountTip(err);
}
return defer.promise();
}
// 本地验证和网络验证
function validateAccount() {
return validateAccountLocal()
return currentLogin.validateAccountLocal()
.then(currentLogin.validateAccountAsync)
.then(hideAccountTip);
}
... ... @@ -434,7 +473,14 @@ function validateCaptchaSmsLocal() {
}
// 网络验证短信验证码
// Note: 这里的验证码只能成功验证一次,十分钟内有效
function validateCaptchaSmsAsync() {
var smsToken = $.trim($captchaSmsTokenHideInput.val());
if (!smsToken) {
return $.Deferred().resolve().promise; // eslint-disable-line
}
return $.ajax({
url: '/passport/login/sms/auth',
type: 'POST',
... ... @@ -448,10 +494,11 @@ function validateCaptchaSmsAsync() {
if (result.code === 200) {
hideCaptchaSmsTip();
$captchaSMSToken.val(result.token);
$captchaSmsTokenHideInput.val(result.token);
defer.resolve();
} else {
showCaptchaSmsTip('验证码不正确');
$captchaSmsTokenHideInput.val('');
defer.reject();
}
... ... @@ -609,10 +656,9 @@ function login() {
}
});
}).then(function(res) {
console.log(res);
if (res.code === 200) {
if (res.data) {
// 防止data.data为undefined时下行语句执行出错而导致脚本不能走到complete去处理authing
location.href = res.data.session;
}
} else {
... ... @@ -670,7 +716,7 @@ $(document).on('click', function(e) {
// 邮箱自动完成后失去焦点:1. 本地验证格式;2.服务器检查用户是否注册;3.检查验证用户次数
mailAc($accountInput, function() {
return validateAccountLocal().then(function() {
return validateAccountPasswordLocal().then(function() {
return currentLogin.validateAccountAsync().always(checkUserAuthCountAsync);
});
});
... ... @@ -710,7 +756,7 @@ $captchaSmsBtn.on('click', function() {
var val = getAccountVal();
if (val === '') {
showAccountTip('请输入账户名');
showAccountTip('请输入手机号');
return;
}
... ...