order-ensure.js 7.39 KB
/**
 * 订单确认
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/11/12
 */

var $ = require('jquery'),
    lazyLoad = require('yoho.lazyload'),
    Hammer = require('yoho.hammer'),
    Handlebars = require('yoho.handlebars'),
    tip = require('../plugin/tip'),
    loading = require('../plugin/loading'),
    order = require('./order-info');

var dispatchModeHammer,
    dispatchTimeHammer,
    $invoice = $('.invoice'),
    $price = $('.price-cal'),
    $couponUse = $('.coupon-use.used'),
    $addressWrap = $('.address-wrap'),
    $coinCheck = $('.coin-check'),
    $coinUsed = $('.coin .used'),
    payType,
    priceTmpl = Handlebars.compile($('#tmpl-price').html()),
    queryString = $.queryString(),
    orderInfo = order.orderInfo;

lazyLoad();

if (window.getUid() !== orderInfo('uid')) {
    order.init();
    window.location.reload();
}

if ($couponUse.data('name') !== orderInfo('couponName')) {
    orderInfo('couponCode', null);
    orderInfo('couponName', null);
}

orderInfo('cartType', queryString.cartType || queryString.carttype || 'ordinary');

function dispacthTapEvt(e) {
    var $cur = $(e.target).closest('li');

    if ($cur.length === 0 || $cur.hasClass('chosed')) {
        return;
    }

    $cur.siblings('li.chosed').removeClass('chosed');
    $cur.addClass('chosed');
}

dispatchModeHammer = new Hammer(document.getElementsByClassName('dispatch-mode')[0]);
dispatchModeHammer.on('tap', dispacthTapEvt);

dispatchTimeHammer = new Hammer(document.getElementsByClassName('dispatch-time')[0]);
dispatchTimeHammer.on('tap', dispacthTapEvt);

$('.checkbox').on('touchstart', function() {
    var $this = $(this);

    if ($this.hasClass('icon-cb-checked')) {
        $this.removeClass('icon-cb-checked').addClass('icon-checkbox');
        return;
    }
    if ($this.hasClass('icon-checkbox')) {
        $this.removeClass('icon-checkbox').addClass('icon-cb-checked');
    }
});

$('.invoice').on('touchend', '.checkbox', function() {
    var $this = $(this);

    if ($this.hasClass('icon-cb-checked')) {
        $('.invoice').addClass('focus');
    }
    if ($this.hasClass('icon-checkbox')) {
        $('.invoice').removeClass('focus');
    }
});

function orderCompute() {
    $.ajax({
        method: 'POST',
        url: '/cart/index/orderCompute',
        data: {
            cartType: orderInfo('cartType') || 'ordinary',
            deliveryId: orderInfo('deliveryId'),
            paymentTypeId: orderInfo('paymentTypeId'),
            couponCode: orderInfo('couponCode'),
            yohoCoin: orderInfo('yohoCoin')
        }
    }).then(function(res) {
        var priceHtml;

        if ($.type(res) !== 'object') {
            window.location.reload();
        } else {
            /*if (res.order_amount) {
                res.order_amount = (+res.order_amount).toFixed(2);
            }
            if (res.discount_amount) {
                res.discount_amount = (+res.discount_amount).toFixed(2);
            }*/
            if (res.last_order_amount) {
                res.last_order_amount = (+res.last_order_amount).toFixed(2);
            }
            if (res.use_yoho_coin) {
                $coinCheck.find('em').html('- ¥ ' + res.use_yoho_coin);
                $coinUsed.html('已抵¥' + res.use_yoho_coin);
                $coinCheck.find('em').show();
                $coinUsed.show();
            }
            priceHtml = priceTmpl({
                cartPayData: res.promotion_formula_list,
                price: res.last_order_amount
            });

            $price.html(priceHtml);
        }
    }).fail(function() {
        window.location.reload();
    });
}

function submitOrder() {
    var invoiceText = $invoice.find('[name="invoice-title"]').val() || orderInfo('invoiceText'),
        msg = $('#msg').find('input').val() || orderInfo('msg');

    if (orderInfo('invoice')) {
        if (!invoiceText) {
            tip.show('请输入发票抬头');
            return;
        }
        if (invoiceText.length > 30) {
            tip.show('发票抬头不得超过30个汉字');
            return;
        }
    }
    if (msg) {
        if (msg.length > 40) {
            tip.show('留言不得超过40个汉字');
            return;
        }
    }
    loading.showLoadingMask();
    $.ajax({
        method: 'POST',
        url: '/cart/index/orderSub',
        data: {
            addressId: orderInfo('addressId'),
            cartType: orderInfo('cartType') || 'ordinary',
            deliveryId: orderInfo('deliveryId'),
            deliveryTimeId: orderInfo('deliveryTimeId'),
            invoiceText: orderInfo('invoice') ? invoiceText : null,
            invoiceType: orderInfo('invoice') ? ($invoice.find('.invoice-type').val() ||
                orderInfo('invoiceType')) : null,
            msg: msg,
            paymentTypeId: orderInfo('paymentTypeId'),
            paymentType: orderInfo('paymentType'), //支付方式
            couponCode: orderInfo('couponCode'),
            yohoCoin: orderInfo('yohoCoin')
        }
    }).then(function(res) {
        var url;

        if (!res) {
            tip.show('网络出错');
            return;
        }
        if (res.code === 200) {
            if (payType === 2) {

                // 货到付款的进入订单页面
                url = '/home/orderDetail?order_code=' + res.data.order_code;
            } else {
                url = '/home/pay?order_code=' + res.data.order_code;
            }
            window.setCookie('order-info', '');
            window.location.href = url;
        } else if (res.message) {
            tip.show(res.message);
        }
    }).fail(function() {
        tip.show('网络出错');
    }).always(function() {
        loading.hideLoadingMask();
    });
}

// 界面点击,状态存 cookie
if (!orderInfo('addressId')) {
    orderInfo('addressId', $addressWrap.data('id'));
}

$('.dispatch-mode').on('touchend', 'li', function() {
    var $defaultMode = $('.dispatch-mode [data-id="1"]');

    if (!$addressWrap.data('support') && $(this).data('id') === 2) {
        dispacthTapEvt({
            target: $defaultMode
        });
        tip.show('当前地址不支持顺丰快递');
    } else {
        orderInfo('deliveryId', $(this).data('id'));
        orderCompute();
    }
});

$('.dispatch-time').on('touchend', 'li', function() {
    orderInfo('deliveryTimeId', $(this).data('id'));
});

$('.coin').on('touchend', function() {
    var $this = $(this);

    if ($this.find('.checkbox').hasClass('icon-cb-checked')) {
        orderInfo('yohoCoin', $this.data('yoho-coin'));
        $this.find('.can-use').hide();
    } else {
        orderInfo('yohoCoin', 0);
        $this.find('.coin-check em').hide();
        $this.find('.can-use').show();
        $this.find('.used').hide();
    }
    orderCompute();
});

$invoice.on('touchend', function() {
    var $this = $(this);

    orderInfo('invoice', $this.find('.checkbox').hasClass('icon-cb-checked'));
});

$invoice.find('[name="invoice-title"]').on('blur', function() {
    orderInfo('invoiceText', $(this).val());
}).end().find('.invoice-type').on('change', function() {
    orderInfo('invoiceType', $(this).val());
});

$('#msg').find('textarea').on('blur', function() {
    orderInfo('msg', $(this).val());
});

$('.pay-mode').on('click', 'li', function() {
    var $this = $(this);

    orderInfo('paymentTypeId', $this.data('pay-id'));
    orderInfo('paymentType', $this.data('pay-type'));
    payType = $this.data('pay-type');
    submitOrder();
});

$('form').on('submit', function() {
    return false;
});