common.Login.js 4.5 KB
webpackJsonp([0],[
/* 0 */
/***/ function(module, exports, __webpack_require__) {

	'use strict';
	var $ = __webpack_require__(1);

	// 图像验证码URL
	var CAPTCHA_IMG_URL = 'captcha?t=',
	    $captchaImgInput = $('#captcha'),
	    $captchaImgWrapper = $('.captcha-wrap'),
	    $captchaImgPic = $captchaImgWrapper.find('#captcha-img'),
	    $captchaImgTip = $captchaImgInput.siblings('.err-tip');

	// click登录
	$('#login-btn').on('click', loginAsync);

	/** ************************************************************************/
	/*                                  登录函数                                */
	/** ************************************************************************/

	function loginAsync() {
	    return validateCaptchaImg().then(function() {
	        return $.ajax({
	            url: 'login/',
	            type: 'POST',
	            dataType: 'json',
	            data: {
	                user: $('#user').val(),
	                password: $('#password').val()
	            }
	        });
	    }).then(function(res) {
	        if (res.code === 200) {
	            location.href = '/';
	        } else if (res.code === 400){
	            if (res.data.needCaptcha) {
	                $captchaImgTip.removeClass('hide')
	                    .children('em');

	                $captchaImgInput.val('');
	                showCaptchaImgPic();
	            } else {
	                location.href = '/';
	            }
	        }
	    });
	}

	/** ************************************************************************/
	/*                                  验证码刷新                              */
	/** ************************************************************************/
	$captchaImgWrapper.on('click', '.change-captcha, .captcha-img', function() {
	    $captchaImgPic.attr('src', CAPTCHA_IMG_URL + $.now());
	});

	/** ************************************************************************/
	/*                                  显示验证码                              */
	/** ************************************************************************/
	function showCaptchaImgPic() {
	    $captchaImgPic.attr('src', CAPTCHA_IMG_URL + $.now());
	    $captchaImgInput.val('');
	    $captchaImgWrapper.removeClass('hide');
	}

	/** ************************************************************************/
	/*                                  验证验证码是否正确                       */
	/** ************************************************************************/
	// 图像验证码输入框事件
	$captchaImgInput.on('blur', validateCaptchaImg);

	function validateCaptchaImg() {
	    // 验证码不可见的时候验证通过
	    if ($captchaImgWrapper.is(':hidden')) {
	        return $.Deferred().resolve().promise(); //eslint-disable-line
	    }

	    return validateCaptchaImgLocal()
	        .then(validateCaptchaImgAsync);
	}

	function validateCaptchaImgAsync() {
	    return $.ajax({
	        url: 'checkCaptchaImg/',
	        type: 'POST',
	        dataType: 'json',
	        data: {
	            verifyCode: getCaptchaImgVal()
	        }
	    }).then(function(result) {
	        var defer = $.Deferred(); // eslint-disable-line

	        if (result.code === 200) {
	            hideCaptchaImgTip();

	            defer.resolve();
	        } else {
	            showCaptchaImgTip('验证码不正确');

	            defer.reject();
	        }

	        return defer.promise();
	    });
	}

	function validateCaptchaImgLocal() {
	    var captcha = getCaptchaImgVal(),
	        err;

	    var defer = $.Deferred(); // eslint-disable-line

	    if (captcha !== '') {
	        if (captcha.length !== 4) {
	            err = '请输入长度为4字符的验证码';

	            defer.reject();
	        } else {

	            defer.resolve();
	        }
	    } else {
	        err = '请输入验证码';

	        defer.reject();
	    }

	    if (defer.state() === 'resolved') {
	        hideCaptchaImgTip();
	    } else {
	        showCaptchaImgTip(err);
	    }

	    return defer.promise();
	}

	function getCaptchaImgVal() {
	    return $.trim($captchaImgInput.val());
	}

	function hideCaptchaImgTip() {
	    return errTipHide($captchaImgTip, $captchaImgInput);
	}

	function showCaptchaImgTip(msg) {
	    return errTipShow($captchaImgTip, $captchaImgInput, msg);
	}

	function errTipHide($tip, $input) {
	    $tip.addClass('hide');
	    //$input.removeClass('error');
	}

	function errTipShow($tip, $input, msg) {
	    $tip.removeClass('hide').children('em').empty().html(msg);
	    //$input.addClass('error');
	}

/***/ }
]);