Blame view

public/js/home/installment.starting-service.page.js 7.91 KB
lijing authored
1 2 3 4
let $ = require('yoho-jquery');
let tip = require('plugin/tip');
let bp = require('./burying-point');
let yohoApp = require('yoho-app');
Aiden Xu authored
5
lijing authored
6
let Timer = function() {
Aiden Xu authored
7 8 9 10
    this.counter = 0;
    this.countdownTimer = null;
};
lijing authored
11
let FormModel = function() {
Aiden Xu authored
12
    $.extend(this, {
Aiden Xu authored
13 14 15
        userName: '',
        identityCardNo: '',
        cardNo: '',
Aiden Xu authored
16 17
        bankName: '',
        bankCode: '',
Aiden Xu authored
18 19
        mobile: '',
        snsCheckCode: '',
王水玲 authored
20
        agreements: 'on'
Aiden Xu authored
21
    });
Aiden Xu authored
22 23
};
lijing authored
24
let formModel = new FormModel();
Aiden Xu authored
25 26 27 28

/**
 * 表单验证
 */
lijing authored
29 30 31
let validateForm = function() {
    let applyButton = $('#apply-button');
    let ret = false;
Aiden Xu authored
32 33 34 35

    if (formModel.userName &&
        formModel.identityCardNo &&
        formModel.cardNo &&
Aiden Xu authored
36
        formModel.bankCode &&
Aiden Xu authored
37 38 39
        formModel.mobile &&
        formModel.snsCheckCode &&
        formModel.agreements === 'on') {
徐炜 authored
40 41 42
        if (!applyButton.data('running')) {
            applyButton.removeClass('disabled');
        }
Aiden Xu authored
43
        ret = true;
Aiden Xu authored
44
    } else {
Aiden Xu authored
45 46
        applyButton.addClass('disabled');
        ret = true;
Aiden Xu authored
47
    }
Aiden Xu authored
48
徐炜 authored
49 50 51 52 53 54 55
    // 检查手机号码
    if (formModel.cardNo && formModel.cardNo.length >= 16 &&
        formModel.mobile && formModel.mobile.length === 11) {
        $('#send-sms').removeClass('disabled');
    } else {
        $('#send-sms').addClass('disabled');
    }
Aiden Xu authored
56
    return ret;
Aiden Xu authored
57
};
Aiden Xu authored
58
lijing authored
59
let checkCard = require('./bind-card-check');
徐炜 authored
60
lijing authored
61
let clearVerifyCode = function() {
Aiden Xu authored
62 63 64 65
    formModel.snsCheckCode = '';
    $('#sns-check-code').val('');
};
徐炜 authored
66 67
checkCard(formModel);
Aiden Xu authored
68 69 70
/**
 *  倒计时
 *
Aiden Xu authored
71 72
 * @param start 启动回调
 * @param tick 进度回调
Aiden Xu authored
73 74
 * @param complete 完成回调
 */
Aiden Xu authored
75
Timer.prototype.startCountdown = function(start, tick, complete) {
lijing authored
76
    let self = this;
Aiden Xu authored
77
Aiden Xu authored
78 79 80 81 82 83
    if (this.counter > 0 || this.countdownTimer) {
        return;
    } else {
        this.counter = 59;
    }
Aiden Xu authored
84 85 86 87

    // 启动回调
    if (start) {
        start.call(this);
Aiden Xu authored
88 89
    }
Aiden Xu authored
90 91 92 93
    if (tick) {
        tick.call(this, this.counter);
    }
Aiden Xu authored
94 95 96
    this.complete = complete;

Aiden Xu authored
97
    // 开始计时器
Aiden Xu authored
98 99
    this.countdownTimer = setInterval(function() {
        self.counter--;
Aiden Xu authored
100
Aiden Xu authored
101
        if (self.counter <= 0) {
Aiden Xu authored
102
            if (complete) {
Aiden Xu authored
103
                clearInterval(self.countdownTimer);
Aiden Xu authored
104
Aiden Xu authored
105
                // 重置计时器
Aiden Xu authored
106 107 108
                self.counter = 0;
                self.countdownTimer = null;
                complete.call(self);
Aiden Xu authored
109 110 111
            }
        }
Aiden Xu authored
112
        // 完成回调
Aiden Xu authored
113 114
        if (tick && self.counter > 0) {
            tick.call(self, self.counter);
Aiden Xu authored
115 116
        }
    }, 1000);
Aiden Xu authored
117 118

    return this;
Aiden Xu authored
119 120
};
Aiden Xu authored
121 122 123 124 125
Timer.prototype.reset = function() {
    if (this.complete) {
        this.complete();
    }
};
Aiden Xu authored
126 127 128 129 130

/**
 * 点击发送短信事件
 */
$('#send-sms').click(function() {
lijing authored
131
    let self = this;
Aiden Xu authored
132
徐炜 authored
133 134 135 136 137
    // 数据不完整情况下不能发送验证码
    if ($(this).hasClass('disabled')) {
        return false;
    }
Aiden Xu authored
138 139 140 141
    if ($(this).data('running')) {
        return false;
    }
徐炜 authored
142 143
    $(self).data('running', true);
Aiden Xu authored
144 145 146 147
    $.get('/home/installment/starting-service/verify-code', {
        mobile: formModel.mobile
    }).then(function(result) {
        if (result.code === 200) {
Aiden Xu authored
148
            $(self).data('running', true);
Aiden Xu authored
149 150 151 152 153 154 155
            new Timer().startCountdown(function() {
            }, function(counter) {
                // 进度回调
                $('#send-sms').text(counter + 's');
            }, function() {
                // 倒计时结束后再次显示 "获取验证码"
                $('#send-sms').text('获取验证码');
Aiden Xu authored
156
                $(self).data('running', false);
Aiden Xu authored
157 158 159 160
            });

        } else {
            tip.show(result.message);
Aiden Xu authored
161
            $(self).data('running', false);
Aiden Xu authored
162
        }
徐炜 authored
163 164
    }).done(function() {
        $(self).data('running', false);
Aiden Xu authored
165 166 167 168 169
    });

    return false;
});
Aiden Xu authored
170
// 输入框改变时同时更新模型
徐炜 authored
171
/*
徐炜 authored
172
 $('input').on('change', function() {
lijing authored
173
 let name = $(this).attr('name');
Aiden Xu authored
174
徐炜 authored
175 176 177 178 179
 if ($(this).is(':checkbox')) {
 formModel[name] = $(this).is(':checked') ? $(this).val() : null;
 } else {
 formModel[name] = $(this).val();
 }
Aiden Xu authored
180
徐炜 authored
181 182 183
 validateForm();
 });
 */
Aiden Xu authored
184
徐炜 authored
185
// validateForm();
Aiden Xu authored
186
徐炜 authored
187
// 定时更新模型,解决各种浏览器奇葩问题终极办法
Aiden Xu authored
188
setInterval(function() {
徐炜 authored
189
    $('input').each(function() {
lijing authored
190
        let name = $(this).attr('name');
徐炜 authored
191 192 193 194 195 196 197

        if ($(this).is(':checkbox')) {
            formModel[name] = $(this).is(':checked') ? $(this).val() : null;
        } else {
            formModel[name] = $(this).val();
        }
    });
Aiden Xu authored
198 199
    validateForm();
}, 500);
Aiden Xu authored
200
Aiden Xu authored
201
Aiden Xu authored
202 203 204
/**
 * 表单提交
 */
Aiden Xu authored
205
$('#apply-button').click(function() {
lijing authored
206 207 208
    let ret = false;
    let that = $(this);
    let asyncMode = yohoApp.isiOS;
Aiden Xu authored
209 210 211

    if ($(this).hasClass('disabled') || !validateForm()) {
        return false;
徐炜 authored
212 213
    } else {
        $(this).addClass('disabled').text('处理中...').data('running', true);
Aiden Xu authored
214 215
    }
王水玲 authored
216
    // 统计:点击下一步按钮时
沈志敏 authored
217 218 219 220
    bp.setContYas({
        op: 'YB_INST_NEXT',
        appop: 'YB_H5_INST_NEXT_C'
    }, {}, true);
王水玲 authored
221
Aiden Xu authored
222 223
    $.ajax({
        method: 'get',
徐炜 authored
224
        async: asyncMode,
Aiden Xu authored
225 226 227 228 229
        url: '/home/installment/starting-service/check-verify-code',
        data: {
            mobile: formModel.mobile,
            code: formModel.snsCheckCode
        }
230 231 232 233 234 235
    }).then(function(result) {
        if (result.code === 200 && result.data.result === '1') {
            return $.ajax({
                method: 'post',
                url: '/home/installment/activate-service',
                data: formModel,
徐炜 authored
236
                async: asyncMode
237 238 239
            });
        } else {
            clearVerifyCode();
Aiden Xu authored
240
            tip.show(result.message);
241
        }
Aiden Xu authored
242
    }).then(function(result) {
lijing authored
243
        let params;
Aiden Xu authored
244
徐炜 authored
245 246
        that.removeClass('disabled').text('下一步').data('running', false);
Aiden Xu authored
247
        if (!result) {
徐炜 authored
248
            return;
Aiden Xu authored
249 250 251
        }

        params = {
王水玲 authored
252
            action: 'go.instalmentActivated',
王水玲 authored
253
            params: {
王水玲 authored
254
                status: result.data && result.data.status || 0
王水玲 authored
255 256 257
            }
        };
王水玲 authored
258
        if (result.code === 200 && result.data) {
王水玲 authored
259
            if (result.data.status === '2') {
王水玲 authored
260 261

                // 统计:开通成功时
沈志敏 authored
262 263 264 265
                bp.setContYas({
                    op: 'YB_INST_OPEN_SUCCESS',
                    appop: 'YB_H5_INST_OPEN_SUCCESS_L'
                }, {}, true);
王水玲 authored
266 267
            }
王水玲 authored
268 269 270 271 272
            // 开通失败
            if (result.data.status === '3') {
                params.params.failReason = result.data.failReason || '姓名、身份证、银行卡不匹配';
            }
徐炜 authored
273
            if (asyncMode) {
274
                params.params.failReason = encodeURIComponent(params.params.failReason);
徐炜 authored
275 276 277 278 279
                yohoApp.invokeMethod('go.instalmentActivated', params.params);
            } else {
                that.attr('href', location.pathname + '?openby:yohobuy=' + encodeURIComponent(JSON.stringify(params)));
            }
王水玲 authored
280
            ret = true;
Aiden Xu authored
281 282
        } else if (result.code === 500) {
            // 接口可能超时返回审核中 by 孟令阶
王水玲 authored
283
            params.params.status = '1';
徐炜 authored
284
            if (asyncMode) {
徐炜 authored
285 286 287 288
                yohoApp.invokeMethod('go.instalmentActivated', params.params);
            } else {
                that.attr('href', location.pathname + '?openby:yohobuy=' + encodeURIComponent(JSON.stringify(params)));
            }
Aiden Xu authored
289
            ret = true;
Aiden Xu authored
290 291
        } else {
            tip.show(result.message);
Aiden Xu authored
292
            clearVerifyCode();
Aiden Xu authored
293
        }
Aiden Xu authored
294
    });
Aiden Xu authored
295 296

    return ret;
Aiden Xu authored
297
});
298 299 300 301


// 使用H5标签后 maxlength 标签失效
$('input[maxlength]').keyup(function() {
lijing authored
302
    let value = $(this).val(),
303 304 305 306
        length = $(this).attr('maxlength') || 20;

    $(this).val(value.slice(0, length));
});
Aiden Xu authored
307 308

$('#agreements').click(function() {
lijing authored
309
    let params = {
Aiden Xu authored
310 311 312 313 314 315 316 317
        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)));
});
徐炜 authored
318 319

require('./overdue-notice');
王水玲 authored
320
王水玲 authored
321
$(window).load(function() {
王水玲 authored
322
    // 统计:进入开通分期表单页面时
沈志敏 authored
323
    bp.setContYas('YB_INST_OPEN_INFO', {}, true);
王水玲 authored
324
});