bind-card-check.js 1.92 KB
/**
 * 银行卡校验
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/08/25
 */
let $ = require('yoho-jquery');
let tip = require('plugin/tip');
let debounce = require('lodash/debounce');

let debounceFn = debounce(function(cardNo, formModel) {
    formModel.bankCode = '';
    formModel.bankName = '';

    // 获取银行信息
    $.get('/home/installment/bank-info', {cardNo: cardNo}).then(function(result) {
        if (result.code === 200) {
            if (result.data.bankCode) {
                formModel.bankCode = result.data.bankCode;
                formModel.bankName = result.data.bankName;

                // 设置银行名称
                $('#bank-name').text(result.data.bankName);

                // 设置银行图标
                $('#bank-icon').show().attr('src',
                    window.STATIC_RESOURCE_PATH +
                    '/img/home/bank-icons/' + result.data.bankCode + '.png');
                $('#bank-desc').show();
            }

            // 返回1不支持, 孟令阶
            if (result && result.data && result.data.businessSupport === '1') {
                formModel.bankCode = '';
                formModel.bankName = '';

                $('#bank-name').text('暂不支持此银行卡');
                $('#bank-desc').show();
                $('#bank-icon').hide();
            }
        } else {
            tip.show(result.message);
            $('#bank-desc').hide();
        }
    });
}, 1000);

let checkCard = function(formModel) {
    // 银行卡格式化
    $('#cardNo').on('input', function() {
        let value = $(this).val();
        let cardNo = $(this).val().replace(/\s/g, '');

        $(this).val(value.replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ')).trigger('change');

        if (cardNo && cardNo.length >= 16) {
            debounceFn(cardNo, formModel);
        } else {
            $('#bank-desc').hide();
        }
    });
};

module.exports = checkCard;