order.page.js 5.65 KB
/**
 * [购物流程]结算页
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/7/8
 */

var $ = require('yoho-jquery'),
    lazyLoad = require('yoho-jquery-lazyload');

var Alert = require('../plugins/dialog').Alert;

var minusPlus = {
    minus: '&#xe630;',
    plus: '&#xe639;'
};

var $printPrice = $('#print-price');

var $invoice = $('#invoice-content');

var $coinRemarkContent = $('.coin-and-remark .coin .content');

var balanceTpl = require('../../tpl/shopping/balance.hbs'),
    yohoCoinTpl = require('../../tpl/shopping/yoho-coin.hbs');

var pkgCache = {};

var suredCoin = 0;

var $pkgList;

require('yoho-jquery-placeholder');

require('yoho-jquery-dotdotdot');

require('../plugins/check'); // before 地址和发票

require('./order/address'); // 地址
require('./order/invoice'); // 发票

lazyLoad($('img.lazy'));

// IE8 placeholder
$('[placeholder]').placeholder();

// dot
$('.brand-and-name .name').dotdotdot({
    wrap: 'letter'
});

function pkgPageControl($el, flag) {
    var id = $el.index(),
        $ul = $el.find('.package-goods'),
        theCache = pkgCache[id],
        curPage = theCache.cur ? theCache.cur : 1,
        page = Math.ceil(theCache.total / 5);

    curPage += flag;

    // 第一页或最后一页
    if (curPage < 1 || curPage > page) {
        return;
    }

    $ul.animate({
        marginLeft: -(curPage - 1) * $ul.parent('.package-goods-wrap').width()
    }, 200);

    theCache.cur = curPage;
}

// JIT拆单
if ($('.multi-package-row').length > 0) {
    $pkgList = $('.package-list');

    // 显示拆单详情面板
    $('.show-package').click(function(e) {
        $pkgList.toggleClass('hide');

        e.stopPropagation();
    });

    // 绑定document事件,去触发面板关闭
    $(document).on('click', function(e) {
        if ($pkgList.hasClass('hide') || $(e.target).closest('.package-list').length > 0) {
            return;
        }

        $pkgList.addClass('hide');
    });

    // 设置左右切换
    $('.package-goods').each(function() {
        $(this).width($(this).children('li').length * 120);
    });

    // 初始化cache
    $('.package-item').each(function() {
        pkgCache[$(this).index()] = {
            total: $(this).find('li').length
        };
    });

    $('.package-list').on('click', '.toggle-icon', function() {
        var $this = $(this);

        var flag = $this.hasClass('left-icon') ? -1 : 1;

        pkgPageControl($this.closest('.package-item'), flag);
    });
}

// 有货币、备注切换显示
$('.coin-ctrl, .remark-ctrl').click(function() {
    var $this = $(this),
        $icon = $this.hasClass('iconfont') ? $this : $this.siblings('.iconfont');

    if ($icon.hasClass('minus')) {

        // hide panel
        $icon.html(minusPlus.plus).removeClass('minus');
    } else {

        // show panel
        $icon.html(minusPlus.minus).addClass('minus');
    }

    $this.parent('.title').siblings('.content').toggleClass('hide');
});

function compute(coin) {
    $.ajax({
        type: 'POST',
        url: '/shopping/order/compute',
        data: {
            coin: coin
        }
    }).then(function(data) {
        var cost;

        if (data.code === 200) {
            cost = data.data.last_order_amount;

            cost = cost.toFixed(2);

            $('#balance-list').html(balanceTpl(data.data));
            $coinRemarkContent.html(yohoCoinTpl(data.data));
        }
    });
}

// 获取已使用的有货币
function getCoinUsed() {
    var coin = $('.coin-and-remark').find('#coin-sure').data('yoho_coin_num') || 0;

    return coin === '' ? 0 : parseInt(coin, 10); // 使用parseInt可以排除异常情况的转化影响
}

// 添加备注-是否打印价格
$printPrice.printPrice = 'N'; // 默认为false

$('.print-price-radio').check({
    type: 'radio',
    group: 'print-price',
    onChange: function(el, checked, value) {
        if (checked) {
            $printPrice.printPrice = value === 1 ? 'Y' : 'N';
        }
    }
});

// 订单提交
$('#balance-list').on('click', '#submit-order', function() {
    var reqParam = {
        address_id: $('.address.focus').data('id'),
        use_yoho_coin: suredCoin,
        remark: $('#remark-content').val(),
        isPrintPrice: $printPrice.printPrice
    };

    var $invoiceDetail;

    // 发票信息
    if ($invoice.find('.checked').length > 0) {
        $invoiceDetail = $invoice.find('.invoice-detail');

        $.extend(reqParam, {
            invoices_type: $invoiceDetail.data('type'),
            invoices_mobile: $invoiceDetail.data('mobile'),
            invoices_title: $invoiceDetail.data('title'),
            invoice_content: $invoiceDetail.data('content')
        });
    }

    $.ajax({
        type: 'POST',
        url: '/shopping/order/submit',
        data: reqParam
    }).then(function(data) {
        if (data.code === 200) {
            location.href = data.data.payUrl;
        } else {
            new Alert(data.message).show();
        }
    });
});

$coinRemarkContent.on('mouseover mouseout', '.yoho-coin-help', function(event) {

    if (event.type === 'mouseover') {
        $('.coin-help-dialog').removeClass('hide');
    } else if (event.type === 'mouseout') {
        $('.coin-help-dialog').addClass('hide');
    }

}).on('click', '#coin-sure', function() { // 确定有货币按钮

    if ($(this).hasClass('disable')) {
        return;
    }

    suredCoin = getCoinUsed();

    // 切换显示
    compute(suredCoin);

    // 隐藏有货币面板并重置子面板显示
    $('.coin-ctrl.iconfont').trigger('click');
}).on('click', '.cancel', function() { // 取消有货币按钮

    suredCoin = 0;
    compute(suredCoin);

    // 隐藏有货币面板并重置子面板显示
    $('.coin-ctrl.iconfont').trigger('click');
});