installment.starting-service.page.js
3.38 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
const $ = require('yoho-jquery');
const Timer = function() {
this.counter = 0;
this.countdownTimer = null;
};
const FormModel = function() {
Object.assign(this, {
userName: '',
identityCardNo: '',
cardNo: '',
mobile: '',
snsCheckCode: '',
agreements: ''
});
};
const formModel = new FormModel();
/**
* 倒计时
*
* @param start 启动回调
* @param tick 进度回调
* @param complete 完成回调
*/
Timer.prototype.startCountdown = function(start, tick, complete) {
if (this.counter > 0 || this.countdownTimer) {
return;
} else {
this.counter = 59;
}
// 启动回调
if (start) {
start.call(this);
}
if (tick) {
tick.call(this, this.counter);
}
// 开始计时器
this.countdownTimer = setInterval(()=> {
this.counter--;
if (this.counter <= 0) {
if (complete) {
clearInterval(this.countdownTimer);
// 重置计时器
this.counter = 0;
this.countdownTimer = null;
complete.call(this);
}
}
// 完成回调
if (tick && this.counter > 0) {
tick.call(this, this.counter);
}
}, 1000);
};
/**
* 点击发送短信事件
*/
$('#send-sms').click(function() {
new Timer().startCountdown(function() {
$.get('/home/installment/starting-service/verify-code', {
mobile: formModel.mobile
}).then((result)=> {
if (result.code === 200) {
formModel.verifyId = result.data.verifyId;
}
});
}, function(counter) {
// 进度回调
$('#send-sms').text(counter + 's');
}, function() {
// 倒计时结束后再次显示 "获取验证码"
$('#send-sms').text('获取验证码');
});
return false;
});
/**
* 银行卡格式化
*/
$('#cardNo').keyup(function() {
const value = $(this).val();
$(this).val(value.replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ')).trigger('change');
}).change(function() {
const cardNo = $(this).val().replace(/\s/, '');
$.get('/home/installment/bank-info', {cardNo: cardNo}).then((result)=> {
if (result.code === 200) {
$('#bank-name').text(result.data.bankName);
$('#bank-desc').show();
}
});
});
/**
* 表单验证
*/
const validateForm = function() {
const applyButton = $('#apply-button');
if (formModel.userName &&
formModel.identityCardNo &&
formModel.cardNo &&
formModel.mobile &&
formModel.snsCheckCode &&
formModel.agreements === 'on') {
applyButton.prop('disabled', false);
} else {
applyButton.prop('disabled', true);
}
};
// 输入框改变时同时更新模型
$('input').on('change', function() {
const name = $(this).attr('name');
if ($(this).is(':checkbox')) {
formModel[name] = $(this).is(':checked') ? $(this).val() : null;
} else {
formModel[name] = $(this).val();
}
validateForm();
});
validateForm();
setInterval(()=> {
validateForm();
}, 500);
/**
* 表单提交
*/
$('#apply-form').submit(function() {
$.post('/home/installment/activate-service', formModel).then((result)=> {
if (result.code === 200) {
location.href = '';
}
});
return false;
});