installment.starting-service.page.js
6.46 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
var $ = require('yoho-jquery');
var tip = require('../plugin/tip');
var Timer = function() {
this.counter = 0;
this.countdownTimer = null;
};
var FormModel = function() {
$.extend(this, {
userName: '',
identityCardNo: '',
cardNo: '',
bankName: '',
bankCode: '',
mobile: '',
snsCheckCode: '',
agreements: 'on'
});
};
var formModel = new FormModel();
/**
* 表单验证
*/
var validateForm = function() {
var applyButton = $('#apply-button');
var ret = false;
if (formModel.userName &&
formModel.identityCardNo &&
formModel.cardNo &&
formModel.bankCode &&
formModel.mobile &&
formModel.snsCheckCode &&
formModel.agreements === 'on') {
applyButton.removeClass('disabled');
ret = true;
} else {
applyButton.addClass('disabled');
ret = true;
}
// 检查手机号码
if (formModel.cardNo && formModel.cardNo.length >= 16 &&
formModel.mobile && formModel.mobile.length === 11) {
$('#send-sms').removeClass('disabled');
} else {
$('#send-sms').addClass('disabled');
}
return ret;
};
var checkCard = require('./bind-card-check');
const clearVerifyCode = function() {
formModel.snsCheckCode = '';
$('#sns-check-code').val('');
};
checkCard(formModel);
/**
* 倒计时
*
* @param start 启动回调
* @param tick 进度回调
* @param complete 完成回调
*/
Timer.prototype.startCountdown = function(start, tick, complete) {
var self = this;
if (this.counter > 0 || this.countdownTimer) {
return;
} else {
this.counter = 59;
}
// 启动回调
if (start) {
start.call(this);
}
if (tick) {
tick.call(this, this.counter);
}
this.complete = complete;
// 开始计时器
this.countdownTimer = setInterval(function() {
self.counter--;
if (self.counter <= 0) {
if (complete) {
clearInterval(self.countdownTimer);
// 重置计时器
self.counter = 0;
self.countdownTimer = null;
complete.call(self);
}
}
// 完成回调
if (tick && self.counter > 0) {
tick.call(self, self.counter);
}
}, 1000);
return this;
};
Timer.prototype.reset = function() {
if (this.complete) {
this.complete();
}
};
/**
* 点击发送短信事件
*/
$('#send-sms').click(function() {
var self = this;
// 数据不完整情况下不能发送验证码
if ($(this).hasClass('disabled')) {
return false;
}
if ($(this).data('running')) {
return false;
}
$.get('/home/installment/starting-service/verify-code', {
mobile: formModel.mobile
}).then(function(result) {
if (result.code === 200) {
$(self).data('running', true);
new Timer().startCountdown(function() {
}, function(counter) {
// 进度回调
$('#send-sms').text(counter + 's');
}, function() {
// 倒计时结束后再次显示 "获取验证码"
$('#send-sms').text('获取验证码');
$(self).data('running', false);
});
} else {
tip.show(result.message);
$(self).data('running', false);
}
});
return false;
});
// 输入框改变时同时更新模型
/*
$('input').on('change', function() {
var name = $(this).attr('name');
if ($(this).is(':checkbox')) {
formModel[name] = $(this).is(':checked') ? $(this).val() : null;
} else {
formModel[name] = $(this).val();
}
validateForm();
});
*/
// validateForm();
// 定时更新模型,解决各种浏览器奇葩问题终极办法
setInterval(function() {
$('input').each(function() {
var name = $(this).attr('name');
if ($(this).is(':checkbox')) {
formModel[name] = $(this).is(':checked') ? $(this).val() : null;
} else {
formModel[name] = $(this).val();
}
});
validateForm();
}, 500);
/**
* 表单提交
*/
$('#apply-button').click(function() {
var ret = false;
var that = $(this);
if ($(this).hasClass('disabled') || !validateForm()) {
return false;
}
$.ajax({
method: 'get',
async: false,
url: '/home/installment/starting-service/check-verify-code',
data: {
mobile: formModel.mobile,
code: formModel.snsCheckCode
}
}).then(function(result) {
if (result.code === 200 && result.data.result === '1') {
return $.ajax({
method: 'post',
url: '/home/installment/activate-service',
data: formModel,
async: false
});
} else {
clearVerifyCode();
tip.show(result.message);
}
}).then(function(result) {
var params;
if (!result) {
// return;
result = {
code: 500
};
}
params = {
action: 'go.instalmentActivated',
params: {
status: result.data && result.data.status || 0
}
};
if (result.code === 200 && result.data) {
that.attr('href', location.pathname + '?openby:yohobuy=' + encodeURIComponent(JSON.stringify(params)));
ret = true;
} else if (result.code === 500) {
// 接口可能超时返回审核中 by 孟令阶
params.params.status = '1';
that.attr('href', location.pathname + '?openby:yohobuy=' + encodeURIComponent(JSON.stringify(params)));
ret = true;
} else {
tip.show(result.message);
clearVerifyCode();
}
});
return ret;
});
// 使用H5标签后 maxlength 标签失效
$('input[maxlength]').keyup(function() {
var value = $(this).val(),
length = $(this).attr('maxlength') || 20;
$(this).val(value.slice(0, length));
});
$('#agreements').click(function() {
const params = {
action: 'go.instalmentProtocol',
params: {
protocolUrl: location.protocol + '//' + location.hostname + location.port + $(this).data('href')
}
};
$(this).attr('href', location.pathname + '?openby:yohobuy=' + encodeURIComponent(JSON.stringify(params)));
});
require('./overdue-notice');