sms-login.page.js
2.29 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
'use strict';
var tip = require('plugin/tip');
var api = require('./api');
var $countrySelect,
$areaCode,
$nextBtn,
$resetBtn,
$phoneNum;
var page = {
init: function() {
this.domInit();
this.bindEvent();
},
domInit: function() {
$countrySelect = $('#country-select');
$areaCode = $('#area-code');
$nextBtn = $('#btn-next');
$phoneNum = $('#phone-num');
$resetBtn = $('.clear-input');
},
bindEvent: function() {
var self = this;
$countrySelect.on('change', function() {
$areaCode.text(this.value);
});
$phoneNum.on('input', function() {
self.toggleNextBtn();
});
$nextBtn.on('click', function() {
self.goNext();
});
$resetBtn.on('click', function() {
$phoneNum.val('');
$nextBtn
.prop('disabled', true)
.toggleClass('disable', true);
$resetBtn.hide();
});
},
// 切换$nextBtn disable状态
toggleNextBtn: function() {
var bool = Boolean($.trim($phoneNum.val()));
$nextBtn
.toggleClass('disable', !bool)
.prop('disabled', !bool);
$resetBtn.toggle(bool);
},
// 提交按钮
goNext: function() {
var areaCode = $countrySelect.val();
var phone = $.trim($phoneNum.val());
if ($nextBtn.prop('disabled')) {
return;
}
if (!api.phoneRegx[areaCode].test(phone)) {
tip.show('手机号码格式不正确, 请重新输入');
return;
}
$nextBtn.prop('disabled', true);
$.get('/passport/sms_login/token.json', {
area: areaCode.replace('+', ''),
mobile: phone
})
.done(function(data) {
if (data.code === 200) {
// $nextBtn.off();
location.href = data.redirect;
} else {
tip.show(data.message);
}
})
.fail(function() {
tip.show('出错了, 请重试');
})
.always(function() {
$nextBtn.prop('disabled', false);
});
}
};
$(function() {
page.init();
});