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

var 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') {
                $('#bank-name').text('暂不支持此银行卡');
                $('#bank-desc').show();
                $('#bank-icon').hide();
            }
        } else {
            tip.show(result.message);
            $('#bank-desc').hide();
        }
    });
}, 1000);

var checkCard = function(formModel) {
    // 银行卡格式化
    $('#cardNo').keyup(function() {
        var value = $(this).val();
        var 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) {
            $('#bank-desc').hide();
        }
    }).blur(function() {
        var cardNo = $(this).val().replace(/\s/g, '');

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

module.exports = checkCard;