let $ = require('yoho-jquery'); let tip = require('plugin/tip'); let bp = require('./burying-point'); let yohoApp = require('yoho-app'); let Timer = function() { this.counter = 0; this.countdownTimer = null; }; let FormModel = function() { $.extend(this, { userName: '', identityCardNo: '', cardNo: '', bankName: '', bankCode: '', mobile: '', snsCheckCode: '', agreements: 'on' }); }; let formModel = new FormModel(); /** * 表单验证 */ let validateForm = function() { let applyButton = $('#apply-button'); let ret = false; if (formModel.userName && formModel.identityCardNo && formModel.cardNo && formModel.bankCode && formModel.mobile && formModel.snsCheckCode && formModel.agreements === 'on') { if (!applyButton.data('running')) { 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; }; let checkCard = require('./bind-card-check'); let clearVerifyCode = function() { formModel.snsCheckCode = ''; $('#sns-check-code').val(''); }; checkCard(formModel); /** * 倒计时 * * @param start 启动回调 * @param tick 进度回调 * @param complete 完成回调 */ Timer.prototype.startCountdown = function(start, tick, complete) { let 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() { let self = this; // 数据不完整情况下不能发送验证码 if ($(this).hasClass('disabled')) { return false; } if ($(this).data('running')) { return false; } $(self).data('running', true); $.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); } }).done(function() { $(self).data('running', false); }); return false; }); // 输入框改变时同时更新模型 /* $('input').on('change', function() { let 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() { let 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() { let ret = false; let that = $(this); let asyncMode = yohoApp.isiOS; if ($(this).hasClass('disabled') || !validateForm()) { return false; } else { $(this).addClass('disabled').text('处理中...').data('running', true); } // 统计:点击下一步按钮时 bp.setContYas({ op: 'YB_INST_NEXT', appop: 'YB_H5_INST_NEXT_C' }, {}, true); $.ajax({ method: 'get', async: asyncMode, 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: asyncMode }); } else { clearVerifyCode(); tip.show(result.message); } }).then(function(result) { let params; that.removeClass('disabled').text('下一步').data('running', false); if (!result) { return; } params = { action: 'go.instalmentActivated', params: { status: result.data && result.data.status || 0 } }; if (result.code === 200 && result.data) { if (result.data.status === '2') { // 统计:开通成功时 bp.setContYas({ op: 'YB_INST_OPEN_SUCCESS', appop: 'YB_H5_INST_OPEN_SUCCESS_L' }, {}, true); } // 开通失败 if (result.data.status === '3') { params.params.failReason = result.data.failReason || '姓名、身份证、银行卡不匹配'; } if (asyncMode) { params.params.failReason = encodeURIComponent(params.params.failReason); yohoApp.invokeMethod('go.instalmentActivated', params.params); } else { that.attr('href', location.pathname + '?openby:yohobuy=' + encodeURIComponent(JSON.stringify(params))); } ret = true; } else if (result.code === 500) { // 接口可能超时返回审核中 by 孟令阶 params.params.status = '1'; if (asyncMode) { yohoApp.invokeMethod('go.instalmentActivated', params.params); } else { 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() { let value = $(this).val(), length = $(this).attr('maxlength') || 20; $(this).val(value.slice(0, length)); }); $('#agreements').click(function() { let 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'); $(window).load(function() { // 统计:进入开通分期表单页面时 bp.setContYas('YB_INST_OPEN_INFO', {}, true); });