bind-card.page.js 1.92 KB
/**
 * 新增银行卡
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/08/25
 */

var $ = require('yoho-jquery');
var tip = require('../plugin/tip');
var checkCard = require('./bind-card-check');
var applyButton = $('.open-btn');
var flag = true;
var formData = {
    cardNo: '',
    mobile: '',
    bankName: '',
    bankCode: ''
};

var validateForm = function() {
    var ret = false;

    if (formData.cardNo &&
        formData.mobile.length === 11 &&
        formData.bankName &&
        formData.bankCode) {
        applyButton.removeClass('disabled');
        ret = true;
    } else {
        applyButton.addClass('disabled');
        ret = false;
    }

    return ret;
};

checkCard(formData);

// 输入框改变时同时更新模型
$('input').on('input', function() {
    var name = $(this).attr('name');

    formData[name] = $(this).val();

    validateForm();
});

applyButton.on('click', function() {
    var self = this;

    if (!flag || $(this).hasClass('disabled') || !validateForm()) {
        return false;
    }

    if (!/^1[3|4|5|8|7][0-9]{9}$/.test(formData.mobile)) {
        tip.show('手机号输入不正确!');
        return false;
    }

    flag = false;

    $.ajax({
        type: 'GET',
        url: '/home/installment/post-account',
        data: formData,
        async: false,
        success: function(data) {
            var params = {
                action: 'go.bindCardResult',
                params: {
                    status: 0
                }
            };
            var url = location.href + '&openby:yohobuy=';

            if (data.code === 200) {
                params.params.status = 1;
            }

            url += encodeURIComponent(JSON.stringify(params));

            $(self).attr('href', url);
            return true;
        },
        error: function() {
            tip.show('网络断开连接了~');
            flag = true;
            return false;
        }
    });
});