...
|
...
|
@@ -10,25 +10,27 @@ var EventProxy = require('yoho-eventproxy'); |
|
|
var $phoneNumInput = $('#account'),
|
|
|
$passwordInput = $('#password'),
|
|
|
$imgCaptchaInput = $('#captcha'),
|
|
|
$smsCaptchaInput = $('#captcha-sms'),
|
|
|
$smsCodeWrap = $('.sms-code-wrap'),
|
|
|
$pwdWrap = $('.pwd-wrap'),
|
|
|
$captchaSmsTokenHideInput = $('#captcha-sms-token-hide'),
|
|
|
$loginBtn = $('#login-btn'),
|
|
|
$phone = $('#phone');
|
|
|
$phone = $('#phone'),
|
|
|
$smsBtn = $('#sms-btn'),
|
|
|
$regionCodeText = $('#country-code'),
|
|
|
$regionSelectList = $('#country-select-list'),
|
|
|
$regionSelectHeader = $('#country-select-header'),
|
|
|
$emailAutoComplete = $('#email-autocomplete');
|
|
|
|
|
|
var $loginTip = $loginBtn.siblings('.login-fail-tip'),
|
|
|
ep = new EventProxy();
|
|
|
|
|
|
var $regionCodeText = $('#country-code'),
|
|
|
$regionSelectList = $('#country-select-list'),
|
|
|
$regionSelectHeader = $('#country-select-header');
|
|
|
|
|
|
var $emailAutoComplete = $('#email-autocomplete');
|
|
|
|
|
|
var mailPhoneRegx = require('../common/mail-phone-regx');
|
|
|
var mailAc = require('../common/ac-email'); // 邮箱自动完成
|
|
|
|
|
|
var $remember = $('.remember-me');
|
|
|
|
|
|
var captchaUrl = '/passport/images?t='; // /passport/images?t=1454464125
|
|
|
|
|
|
var showCaptcha = false;
|
|
|
var $captchaWrap = $('.captcha-wrap'),
|
|
|
$captchaImg = $captchaWrap.find('#captcha-img');
|
|
|
|
...
|
...
|
@@ -45,13 +47,26 @@ var upDown = { |
|
|
|
|
|
var selectedIcon = '';
|
|
|
|
|
|
// 短信验证码的计数器,60s
|
|
|
var secondCount = 60;
|
|
|
|
|
|
// 短信验证码只能验证一次
|
|
|
var isSmsCheckedSuccessFlag = false;
|
|
|
|
|
|
var currLoginType = 'PasswordLogin';
|
|
|
|
|
|
require('yoho-jquery-placeholder');
|
|
|
|
|
|
function refreshCaptcha() {
|
|
|
$captchaImg.attr('src', captchaUrl + $.now());
|
|
|
$imgCaptchaInput.val('');
|
|
|
}
|
|
|
|
|
|
function errTip(ele, msg) {
|
|
|
var $errTip = ele.next('.tips');
|
|
|
var $errMsg = $errTip.find('.content');
|
|
|
|
|
|
$errMsg.text(msg);
|
|
|
$errMsg.html(msg);
|
|
|
return $errTip.removeClass('hide');
|
|
|
}
|
|
|
|
...
|
...
|
@@ -59,9 +74,19 @@ function hideTip(ele) { |
|
|
return ele.next('.tips').addClass('hide');
|
|
|
}
|
|
|
|
|
|
// 获取区域号
|
|
|
function getArea() {
|
|
|
return $regionCodeText.text().replace('+', '');
|
|
|
}
|
|
|
|
|
|
// 获取用户输入的手机号
|
|
|
function getMoblie() {
|
|
|
return $.trim($phoneNumInput.val());
|
|
|
}
|
|
|
|
|
|
// 验证账户名
|
|
|
function validateAccountLocal() {
|
|
|
var phoneNum = $.trim($phoneNumInput.val()),
|
|
|
var phoneNum = getMoblie(),
|
|
|
regionCode = $regionCodeText.text();
|
|
|
|
|
|
if (phoneNum !== '') {
|
...
|
...
|
@@ -96,19 +121,19 @@ function validateAccountLocal() { |
|
|
}
|
|
|
|
|
|
// 异步验证帐号是否存在
|
|
|
function validateAccountAsync() {
|
|
|
function validateAccountAsync(url) {
|
|
|
return $.ajax({
|
|
|
type: 'POST',
|
|
|
url: '/passport/login/user',
|
|
|
url: url,
|
|
|
data: {
|
|
|
phoneNum: $phoneNumInput.val(),
|
|
|
area: $regionCodeText.text().replace('+', '')
|
|
|
phoneNum: getMoblie(),
|
|
|
area: getArea()
|
|
|
}
|
|
|
}).then(function(data) {
|
|
|
if (data.code && data.code === 200) {
|
|
|
return true;
|
|
|
} else {
|
|
|
errTip($phoneNumInput, '账号不存在');
|
|
|
errTip($phoneNumInput, data.message);
|
|
|
return false;
|
|
|
}
|
|
|
});
|
...
|
...
|
@@ -118,9 +143,14 @@ function validateAccountAsync() { |
|
|
function validateAccount() {
|
|
|
return (function() {
|
|
|
var defer = $.Deferred(); // eslint-disable-line
|
|
|
var url = '/passport/login/user';
|
|
|
|
|
|
if (currLoginType === 'SMSLogin') {
|
|
|
url = '/passport/login/sms/checkuser';
|
|
|
}
|
|
|
|
|
|
if (validateAccountLocal()) {
|
|
|
validateAccountAsync().then(function(result) {
|
|
|
validateAccountAsync(url).then(function(result) {
|
|
|
if (result) {
|
|
|
defer.resolve(result);
|
|
|
} else {
|
...
|
...
|
@@ -161,16 +191,11 @@ function validatePasswordLocal() { |
|
|
}
|
|
|
}
|
|
|
|
|
|
// 验证验证码
|
|
|
// 图形验证验证码
|
|
|
function validateCaptchaLocal() {
|
|
|
var captcha = $.trim($imgCaptchaInput.val());
|
|
|
var length = captcha.length;
|
|
|
|
|
|
if ($captchaWrap.hasClass('hide')) {
|
|
|
ep.emit('captcha', true);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
switch (length) {
|
|
|
case 0:
|
|
|
errTip($imgCaptchaInput, '请输入验证码');
|
...
|
...
|
@@ -186,56 +211,70 @@ function validateCaptchaLocal() { |
|
|
}
|
|
|
}
|
|
|
|
|
|
// 密码错误次数,超过三次显示验证码
|
|
|
function showAccountErrTimes() {
|
|
|
$captchaWrap.removeClass('hide');
|
|
|
$captchaImg.attr('src', captchaUrl + $.now());
|
|
|
$imgCaptchaInput.val('');
|
|
|
}
|
|
|
|
|
|
// 登录
|
|
|
function login() {
|
|
|
// 异步验证图片验证码
|
|
|
function validateCaptchaImgAsync() {
|
|
|
return $.ajax({
|
|
|
url: '/passport/login/auth',
|
|
|
url: '/passport/images/check',
|
|
|
type: 'POST',
|
|
|
data: {
|
|
|
areaCode: $regionCodeText.text().replace('+', ''),
|
|
|
account: $.trim($phoneNumInput.val()),
|
|
|
password: $.trim($passwordInput.val()),
|
|
|
captcha: $.trim($imgCaptchaInput.val()),
|
|
|
isRemember: $remember.hasClass('checked') ? true : false
|
|
|
},
|
|
|
success: function(res) {
|
|
|
if (res.code === 200) {
|
|
|
if (res.data) {
|
|
|
verifyCode: $.trim($imgCaptchaInput.val())
|
|
|
}
|
|
|
}).then(function(result) {
|
|
|
if (result.code === 200) {
|
|
|
return true;
|
|
|
} else {
|
|
|
errTip($imgCaptchaInput, '验证码不正确');
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 防止data.data为undefined时下行语句执行出错而导致脚本不能走到complete去处理authing
|
|
|
location.href = res.data.refer;
|
|
|
}
|
|
|
} else {
|
|
|
if (res.data.errorType === 'captcha') {
|
|
|
$imgCaptchaInput.val('');
|
|
|
} else {
|
|
|
$loginTip.removeClass('hide').children('em').html(res.message);
|
|
|
$passwordInput.val('');
|
|
|
}
|
|
|
// 整合本地和异步验证信息
|
|
|
function validateCaptchaImg() {
|
|
|
if ($captchaWrap.hasClass('hide')) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 验证错误次数
|
|
|
if (res.data && res.data.needCaptcha) {
|
|
|
showAccountErrTimes();
|
|
|
return (function() {
|
|
|
var defer = $.Deferred(); // eslint-disable-line
|
|
|
|
|
|
if (validateCaptchaLocal()) {
|
|
|
validateCaptchaImgAsync().then(function(result) {
|
|
|
if (result) {
|
|
|
defer.resolve(result);
|
|
|
} else {
|
|
|
defer.reject(result);
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
defer.reject(false);
|
|
|
}
|
|
|
|
|
|
return defer.promise();
|
|
|
}()).then(function() {
|
|
|
hideTip($imgCaptchaInput);
|
|
|
ep.emit('captcha', true);
|
|
|
}).fail(function() {
|
|
|
ep.emit('captcha', false);
|
|
|
refreshCaptcha();
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 密码错误次数,超过三次显示验证码
|
|
|
function showAccountErrTimes() {
|
|
|
$captchaWrap.removeClass('hide');
|
|
|
showCaptcha = true;
|
|
|
$captchaImg.attr('src', captchaUrl + $.now());
|
|
|
$imgCaptchaInput.val('');
|
|
|
}
|
|
|
|
|
|
// 登录次数限制接口
|
|
|
function throttle() {
|
|
|
return $.ajax({
|
|
|
url: '/passport/login/account',
|
|
|
type: 'GET',
|
|
|
data: {
|
|
|
account: $.trim($phoneNumInput.val())
|
|
|
account: getMoblie()
|
|
|
}
|
|
|
});
|
|
|
}
|
...
|
...
|
@@ -266,23 +305,6 @@ function validate() { |
|
|
});
|
|
|
}
|
|
|
|
|
|
// 全部的本地验证
|
|
|
function loginAsync() {
|
|
|
return validateAccount().then(function() {
|
|
|
var defer = $.Deferred(); // eslint-disable-line
|
|
|
|
|
|
if (validateCaptchaLocal() && validatePasswordLocal()) {
|
|
|
defer.resolve(true);
|
|
|
} else {
|
|
|
defer.reject(false);
|
|
|
}
|
|
|
|
|
|
return defer.promise();
|
|
|
}).then(function() {
|
|
|
return login();
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 邮箱自动补全
|
|
|
mailAc($phoneNumInput, function() {
|
|
|
});
|
...
|
...
|
@@ -332,22 +354,22 @@ $regionSelectHeader.on('click', function() { |
|
|
changeHeader();
|
|
|
});
|
|
|
|
|
|
// 密码
|
|
|
// 密码失去焦点后校验
|
|
|
$passwordInput.on('blur', function() {
|
|
|
$passwordInput.removeClass('focus');
|
|
|
validatePasswordLocal();
|
|
|
}).on('focus', function() {
|
|
|
$passwordInput.addClass('focus');
|
|
|
|
|
|
hideTip($passwordInput);
|
|
|
});
|
|
|
|
|
|
// 验证码
|
|
|
// 图形验证码失去焦点|获取焦点的校验
|
|
|
$imgCaptchaInput.on('blur', function() {
|
|
|
$imgCaptchaInput.removeClass('focus');
|
|
|
validateCaptchaLocal();
|
|
|
validateCaptchaImg();
|
|
|
}).on('focus', function() {
|
|
|
$imgCaptchaInput.addClass('focus');
|
|
|
hideTip($imgCaptchaInput);
|
|
|
});
|
|
|
|
|
|
// 邮箱自动完成列表项点击
|
...
|
...
|
@@ -380,7 +402,7 @@ if (($phoneNumInput.val() !== '' || $phoneNumInput.val() === $phoneNumInput.attr |
|
|
$passwordInput.focus();
|
|
|
}
|
|
|
|
|
|
// 同时监听三个事件
|
|
|
// 同时监听三个事件-- 密码登录
|
|
|
ep.tail('phone', 'password', 'captcha', function(phoneAuth, passwordAuth, captchaAuth) {
|
|
|
if (phoneAuth && passwordAuth && captchaAuth) {
|
|
|
$loginBtn.removeClass('auth_ok');
|
...
|
...
|
@@ -389,6 +411,22 @@ ep.tail('phone', 'password', 'captcha', function(phoneAuth, passwordAuth, captch |
|
|
}
|
|
|
});
|
|
|
|
|
|
// 同时监听三个事件-- 短信验证码登录
|
|
|
ep.tail('phone', 'captcha', 'smsCode', function(phoneAuth, captchaAuth, smsCodeAuth) {
|
|
|
if (phoneAuth && captchaAuth && smsCodeAuth) {
|
|
|
$loginBtn.removeClass('auth_ok');
|
|
|
} else {
|
|
|
$loginBtn.addClass('auth_ok');
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 如果密码登录的图形验证码隐藏状态就直接通过校验
|
|
|
if (currLoginType === 'PasswordLogin') {
|
|
|
if ($captchaWrap.hasClass('hide')) {
|
|
|
ep.emit('captcha', true);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
ep.on('phone', function(auth) {
|
|
|
if (auth) {
|
|
|
hideTip($phoneNumInput);
|
...
|
...
|
@@ -407,21 +445,267 @@ ep.on('captcha', function(auth) { |
|
|
}
|
|
|
});
|
|
|
|
|
|
ep.on('smsCode', function(auth) {
|
|
|
if (auth && !$smsCodeWrap.hasClass('hide')) {
|
|
|
hideTip($smsCaptchaInput);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
/** ***********************************************手机验证码登录*********************************************/
|
|
|
// 倒计时 60秒
|
|
|
function disable60sSendSmsBtn() {
|
|
|
secondCount -= 1;
|
|
|
if (secondCount < 0) {
|
|
|
secondCount = 60;
|
|
|
$smsBtn.val('获取短信验证码')
|
|
|
.removeClass('second-progress')
|
|
|
.removeClass('disable');
|
|
|
} else {
|
|
|
$smsBtn.addClass('disable')
|
|
|
.addClass('second-progress')
|
|
|
.val(secondCount + '秒后可重新操作');
|
|
|
window.setTimeout(disable60sSendSmsBtn, 1000);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 发送短信验证码
|
|
|
function sendCaptchaSmsAsync() {
|
|
|
return $.ajax({
|
|
|
type: 'POST',
|
|
|
url: '/passport/login/sms/send',
|
|
|
data: {
|
|
|
area: getArea(),
|
|
|
mobile: getMoblie()
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 验证码输入本地校验
|
|
|
function validateCaptchaSmsLocal() {
|
|
|
var smsCaptcha = $.trim($smsCaptchaInput.val());
|
|
|
var length = smsCaptcha.length;
|
|
|
|
|
|
switch (length) {
|
|
|
case 0:
|
|
|
errTip($smsCaptchaInput, '请输入短信验证码');
|
|
|
return false;
|
|
|
case 4:
|
|
|
return true;
|
|
|
default:
|
|
|
errTip($smsCaptchaInput, '请输入长度为4字符的验证码');
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 异步校验短信验证码是否存在 Note: 这里的验证码只能成功验证一次,十分钟内有效
|
|
|
function validateCaptchaSmsAsync() {
|
|
|
var smsToken = $.trim($captchaSmsTokenHideInput.val());
|
|
|
|
|
|
if (isSmsCheckedSuccessFlag && smsToken !== '') {
|
|
|
return $.Deferred().resolve(true).promise(); // eslint-disable-line
|
|
|
}
|
|
|
|
|
|
return $.ajax({
|
|
|
url: '/passport/login/sms/auth',
|
|
|
type: 'POST',
|
|
|
data: {
|
|
|
area: getArea(),
|
|
|
mobile: getMoblie(),
|
|
|
code: $.trim($smsCaptchaInput.val())
|
|
|
}
|
|
|
}).then(function(data) {
|
|
|
if (data.code && data.code === 200) {
|
|
|
$captchaSmsTokenHideInput.val(data.token);
|
|
|
isSmsCheckedSuccessFlag = true;
|
|
|
return true;
|
|
|
} else if (data.code && data.code === 501) {
|
|
|
errTip($smsCaptchaInput, data.message);
|
|
|
$captchaSmsTokenHideInput.val('');
|
|
|
return false;
|
|
|
} else {
|
|
|
errTip($smsCaptchaInput, '验证码不正确');
|
|
|
$captchaSmsTokenHideInput.val('');
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 整合本地和异步验证信息--短信验证
|
|
|
function validateCaptchaSms() {
|
|
|
return (function() {
|
|
|
var defer = $.Deferred(); // eslint-disable-line
|
|
|
|
|
|
if (validateCaptchaSmsLocal()) {
|
|
|
validateCaptchaSmsAsync().then(function(result) {
|
|
|
if (result) {
|
|
|
defer.resolve(result);
|
|
|
} else {
|
|
|
defer.reject(result);
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
defer.reject(false);
|
|
|
}
|
|
|
|
|
|
return defer.promise();
|
|
|
}()).then(function() {
|
|
|
hideTip($smsCaptchaInput);
|
|
|
ep.emit('smsCode', true);
|
|
|
}).fail(function() {
|
|
|
ep.emit('smsCode', false);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 切换登录方式
|
|
|
function changeLoginType() {
|
|
|
var type = $(this).data('type');
|
|
|
|
|
|
if ($(this).hasClass('selected')) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
currLoginType = type;
|
|
|
|
|
|
if (type === 'PasswordLogin') {
|
|
|
$pwdWrap.removeClass('hide');
|
|
|
$smsCodeWrap.addClass('hide');
|
|
|
if (!showCaptcha) {
|
|
|
$captchaWrap.addClass('hide');
|
|
|
ep.emit('captcha', true);
|
|
|
} else {
|
|
|
refreshCaptcha();
|
|
|
}
|
|
|
hideTip($smsCaptchaInput);
|
|
|
} else if (type === 'SMSLogin') {
|
|
|
$pwdWrap.addClass('hide');
|
|
|
$smsCodeWrap.removeClass('hide');
|
|
|
$captchaWrap.removeClass('hide');
|
|
|
ep.emit('captcha', false);
|
|
|
$smsCaptchaInput.val('');
|
|
|
hideTip($passwordInput);
|
|
|
refreshCaptcha();
|
|
|
}
|
|
|
|
|
|
hideTip($phoneNumInput);
|
|
|
hideTip($imgCaptchaInput);
|
|
|
$(this).siblings().removeClass('selected');
|
|
|
$(this).addClass('selected');
|
|
|
}
|
|
|
|
|
|
$('.login-page .switch > div').off().on('click', changeLoginType);
|
|
|
|
|
|
// 短信验证码发送按钮
|
|
|
$smsBtn.on('click', function() {
|
|
|
// 已在发送短信时间范围内
|
|
|
if ($smsBtn.hasClass('second-progress')) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
validateAccount()
|
|
|
.then(validateCaptchaImg)
|
|
|
.then(function() {
|
|
|
disable60sSendSmsBtn();
|
|
|
return sendCaptchaSmsAsync();
|
|
|
});
|
|
|
});
|
|
|
|
|
|
// 短信验证码
|
|
|
$smsCaptchaInput.on('blur', function() {
|
|
|
$smsCaptchaInput.removeClass('focus');
|
|
|
validateCaptchaSms();
|
|
|
}).on('focus', function() {
|
|
|
$smsCaptchaInput.addClass('focus');
|
|
|
hideTip($smsCaptchaInput);
|
|
|
});
|
|
|
|
|
|
|
|
|
/** **************************************************登录事件********************************************/
|
|
|
// 登录
|
|
|
function login() {
|
|
|
return $.ajax({
|
|
|
url: '/passport/login/auth',
|
|
|
type: 'POST',
|
|
|
data: {
|
|
|
areaCode: getArea(),
|
|
|
account: getMoblie(),
|
|
|
password: currLoginType === 'PasswordLogin' ? $.trim($passwordInput.val()) : $captchaSmsTokenHideInput.val(), // eslint-disable-line
|
|
|
captcha: $.trim($imgCaptchaInput.val()),
|
|
|
isRemember: $remember.hasClass('checked') ? true : false,
|
|
|
loginType: currLoginType
|
|
|
},
|
|
|
success: function(res) {
|
|
|
if (res.code === 200) {
|
|
|
if (res.data) {
|
|
|
|
|
|
// 防止data.data为undefined时下行语句执行出错而导致脚本不能走到complete去处理authing
|
|
|
location.href = res.data.refer;
|
|
|
}
|
|
|
} else {
|
|
|
if (res.data.errorType === 'captcha') {
|
|
|
$imgCaptchaInput.val('');
|
|
|
} else {
|
|
|
$loginTip.removeClass('hide').children('em').html(res.message);
|
|
|
$passwordInput.val('');
|
|
|
}
|
|
|
|
|
|
// 验证错误次数
|
|
|
if (res.data && res.data.needCaptcha) {
|
|
|
showAccountErrTimes();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 全部的本地验证-- 密码登录
|
|
|
function loginAsync() {
|
|
|
return validateAccount()
|
|
|
.then(validateCaptchaImg)
|
|
|
.then(function() {
|
|
|
var defer = $.Deferred(); // eslint-disable-line
|
|
|
|
|
|
if (validatePasswordLocal()) {
|
|
|
defer.resolve(true);
|
|
|
} else {
|
|
|
defer.reject(false);
|
|
|
}
|
|
|
|
|
|
return defer.promise();
|
|
|
}).then(function() {
|
|
|
return login();
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 全部的本地验证-- 短信验证登录
|
|
|
function smsLoginAsync() {
|
|
|
return validateAccount()
|
|
|
.then(validateCaptchaImg)
|
|
|
.then(validateCaptchaSms)
|
|
|
.then(function() {
|
|
|
return login();
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 登录
|
|
|
$loginBtn.on('click', function() {
|
|
|
if ($loginBtn.hasClass('auth_ok')) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
loginAsync();
|
|
|
if (currLoginType === 'PasswordLogin') {
|
|
|
loginAsync();
|
|
|
} else {
|
|
|
smsLoginAsync();
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// Enter登录
|
|
|
$('input.va').on('keypress', function(e) {
|
|
|
if (e.which === 13) {
|
|
|
loginAsync();
|
|
|
if (currLoginType === 'PasswordLogin') {
|
|
|
loginAsync();
|
|
|
} else {
|
|
|
smsLoginAsync();
|
|
|
}
|
|
|
}
|
|
|
}); |
|
|
|
|
|
// 首次触发
|
|
|
$imgCaptchaInput.triggerHandler('blur'); |
...
|
...
|
|