common.Login.js
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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');
}
/***/ }
]);