cart-action.js 11.7 KB
/**
 * Created by yoho on 2017-01-05.
 */

var $ = require('yoho-jquery');
var dialog = require('../common/dialog');
var Dialog = dialog.Dialog;
var Alert = dialog.Alert;
var Confirm = dialog.Confirm;
var rDialog = require('./rdialog');
var RConfirm = rDialog.RConfirm;
var RAlert = rDialog.RAlert;

var $cartnewTips = $('.cartnew-tips'),
    $payWapper = $('.pay-wapper'),
    $cartnewSum = $('.cartnew-sum'),
    capi = require('./cart-api');

// 关闭温馨提示
$cartnewTips.find('.btn_close').click(function() {
    $cartnewTips.fadeOut();
});

// 滚动到第一个选中的商品
function scrollToFirst() {
    var $selected = $payWapper.find('li[data-role="pitem"] .cart-item-check.cart-item-checked:eq(0)');
    var top = 0;

    if ($selected.length > 0) {
        top = $selected.offset().top - 36;
        $('html,body').scrollTop(top);
    }

    return false;
}

function toastNoStore(parent) {

    var $tip = $('<sub class="out-of-stock">您勾选的商品库存不足</sub>').appendTo(parent);

    $tip.fadeIn();

    setTimeout(function() {
        $tip.fadeOut();
    }, 2000);
}

var Cart = {
    addToCart: function(params) {

    },
    toggleSelectOne: function() { // 单选

        var $this = $(this);
        var $pitem = $this.closest('li[data-role="pitem"]');
        var item;

        if (!$this.hasClass('cart-item-checked') && $this.data('tipnostore')) { // 无库存提示
            toastNoStore($pitem);
            return;
        }

        $this.toggleClass('cart-item-checked');

        item = {
            product_sku: $pitem.data('id'),
            selected: $this.hasClass('cart-item-checked') ? 'Y' : 'N',
            buy_number: $pitem.data('productnum'),
            goods_type: $pitem.data('goodstype'),
            promotion_id: $pitem.data('promotionid') ? $pitem.data('promotionid') : 0
        };

        return capi.choiceOut(item);
    },
    toggleSelectAll: function() { // 全选

        var $this = $(this);
        var selected;
        var selectArray = [];

        $this.toggleClass('cart-item-checked');
        selected = $this.hasClass('cart-item-checked') ? 'Y' : 'N';

        $payWapper.find('li[data-role=pitem]').each(function() {
            var $t = $(this);

            if ($t.data('id')) {
                selectArray.push({
                    product_sku: $t.data('id'),
                    selected: selected,
                    buy_number: $t.data('productnum'),
                    goods_type: $t.data('goodstype'),
                    promotion_id: $t.data('promotionid') || 0
                });
            }
        });

        capi.choiceOut(selectArray);
    },
    del: function() {

        var $this = $(this);
        var $item = $this.closest('li[data-role="pitem"]');
        var selectArray = [];
        var content = '<div><span></span>删除商品</div><p>确定从购物车中删除此商品?</p>';
        var countJSON;

        selectArray.push({
            product_sku: $item.data('id'),
            buy_number: $item.data('productnum'),
            promotion_id: $item.data('promotionid') || 0
        });

        if (!$this.data('gift')) {
            countJSON = {
                productPrice: $item.find('.productPrice').text(),
                productTitle: $item.find('.pay-pro-info a').text(),
                link: $item.find('.pay-pro-info a').attr('href'),
                productNum: $item.data('productnum'),
                productSku: $item.data('id'),
                promotionId: $item.data('promotionid')
            };
        }

        new RConfirm(content, function() {
            capi.cartItemDel(selectArray, true, countJSON);
        });

        /* new RConfirm({
            content: content,
            cb: function() {
                capi.cartItemDel(selectArray, true, countJSON);
            }
        }).show();*/
    },
    delAll: function() {

        var selectArray = [];
        var PromotionArray = [];
        var content = '<div><span></span>删除商品</div><p>确定从购物车中删除所有选中商品?</p>';

        $payWapper.find('.cart-item-check[data-role=cart-item-check]').each(function() {

            var $chk = $(this);
            var $item = $chk.closest('[data-role=pitem]');
            if ($chk.hasClass('cart-item-checked')) {

                if ($item.data('id')) {

                    selectArray.push({
                        product_sku: $item.data('id'),
                        buy_number: $item.data('productnum'),
                        promotion_id: $item.data('promotionid') || 0
                    });

                    PromotionArray.push({
                        productPrice: $item.find('.productPrice').text(),
                        productTitle: $item.find('.pay-pro-info a').text(),
                        link: $item.find('.pay-pro-info a').attr('href'),
                        productNum: $item.data('productnum'),
                        productSku: $item.data('id'),
                        promotionId: $item.data('promotionid')
                    });
                }
            }
        });

        if (!$.isEmptyObject(selectArray)) {
            new Confirm({
                content: content,
                cb: function() {
                    capi.cartItemDel(selectArray, true, PromotionArray);
                }
            }).show();
        } else {
            new Alert('请至少选择一件商品').show();
        }
    },
    cleanAllDisable: function() {
        var selectArray = [];
        var PromotionArray = [];
        var content = '<div><span></span>删除商品</div><p>确定从购物车中删除所有失效商品?</p>';

        $payWapper.find('.cart-table-disabled[data-role=pitem]').each(function() {

            var $item = $(this);

            // var $chk = $item.find('[data-role=cart-item-check]');
            if ($item.data('id')) {

                selectArray.push({
                    product_sku: $item.data('id'),
                    buy_number: $item.data('productnum'),
                    promotion_id: $item.data('promotionid') || 0
                });

                PromotionArray.push({
                    productPrice: $item.find('.productPrice').text(),
                    productTitle: $item.find('.pay-pro-info a').text(),
                    link: $item.find('.pay-pro-info a').attr('href'),
                    productNum: $item.data('productnum'),
                    productSku: $item.data('id'),
                    promotionId: $item.data('promotionid')
                });
            }
        });

        if (!$.isEmptyObject(selectArray)) {
            new Confirm({
                content: content,
                cb: function() {
                    capi.cartItemDel(selectArray, true, PromotionArray);
                }
            }).show();
        } else {
            new Alert('请至少选择一件商品').show();
        }
    },
    toFav: function() {

        var $this = $(this);
        var $item = $this.closest('li[data-role="pitem"]');
        var item = {
            product_sku: $item.data('id'),
            buy_number: $item.data('productnum'),
            promotion_id: $item.data('promotionid') ? $item.data('promotionid') : 0
        };

        if ($this.hasClass('has-col-btn')) {
            return;
        }

        capi.cartItemDel(item);
    },
    toFavAll: function() {

        var selectArray = [];

        $payWapper.find('.cart-item-check').each(function() {

            var $item = $(this);
            var $chk = $item.find('.cart-item-check');
            if ($chk.hasClass('cart-item-checked')) {

                if ($item.data('id')) {
                    selectArray.push({
                        product_sku: $(this).data('id'),
                        buy_number: $(this).data('productnum'),
                        promotion_id: $item.data('promotionid') || 0
                    });
                }
            }
        });

        if (!$.isEmptyObject(selectArray)) {
            capi.cartItemDel(selectArray);
        } else {
            new Alert('请至少选择一件商品').show();
        }
    },
    modNum: function() {
        var $this = $(this);
        var countJSON = {};
        var oprType = $this.hasClass('minus') ? 'decreaseNum' : 'increaseNum';

        countJSON[oprType] = 1;

        if ($this.siblings('input').val() === '1' && $this.hasClass('minus')) {
            return false;
        }

        capi.cartItemNumChg($.extend(countJSON, {
            sku: $this.closest('li[data-role="pitem"]').data('id')
        }));
    },
    reAdd2Cart: function() {

        var $this = $(this);
        var $li = $this.closest('li');

        capi.addcart({
            productSku: $li.data('sku'),
            buyNumber: $li.data('num'),
            promotionId: $li.data('promotionid')
        });
    },
    reFav: function() {

        var $this = $(this);
        var $li = $this.closest('li');

        var item = {
            product_sku: $li.data('sku'),
            buy_number: $li.data('num'),
            promotion_id: $li.data('promotionid') || 0
        };

        capi.cartItemDel(item);
    },
    submit: function() {

        if ($('[data-role="gift-sel-btn"]').length) {
            alert('还有赠品未领取!');
        }

        capi.showMDialog('#Y_CartSelectDialog');
        // $('.cart-preSell-dialog').show();

        // $('.cart-togetherGoods').show();

        /* understock = '';
        if ($('.pay-wapper input:checked').parents('tr').find('.tipNoStore').length > 0) {
            shopName = $('.pay-wapper input:checked').parents('tr').find('.tipNoStore');
            $.each(shopName.parents('tr').find('.pay-pro-info a'), function() {
                understock += $(this).html();
            });
            new Alert(understock + '库存不足').show();
        } else {
            if ($('.zp').length > 0 && !$(this).attr('title')) {
                $(this).attr('title', '1');
                new Alert('您有赠品没有选择,请选择完再结算!').show();
            } else {
                if ($('input:checked').length > 0) {

                    // 添加埋点
                    var productId = [];
                    $('.pay-wapper input:checked').parents('tr').each(function() {
                        if ($(this).attr('data-pid')) {
                            productId.push($(this).attr('data-pid'));
                        }
                    });

                    // 结算点击埋点
                    window.addPoint('YB_SC_TOBUY_CLICK', {PRD_ID: productId.join(',')});


                    if ($('.pre-sell-box input:checked').length > 0) {
                        window.location.href = '/cart/index/orderEnsure?type=2';
                    } else {
                        window.location.href = '/cart/index/orderEnsure?type=1';
                    }
                } else {
                    new Alert('请至少选择一件商品').show();
                }
            }
        }*/
    }
};

$payWapper.on('click', 'li[data-role="pitem"] .cart-item-check', Cart.toggleSelectOne);    // 单选
$payWapper.on('click', '[data-role=cart-del-btn]', Cart.del);  // 删除商品
$payWapper.on('click', '[data-role=cart-mov2fav-btn]', Cart.toFav);  // 移入收藏夹
$payWapper.on('click', '.minus, .plus', Cart.modNum);   // 修改购物车数量
$cartnewSum.on('click', '.cart-item-check', Cart.toggleSelectAll); // 全选
$cartnewSum.on('click', '.delete-all-sel', Cart.delAll);    // 批量删除商品
$cartnewSum.on('click', '.remove-all-2fav', Cart.toFavAll);   // 批量移入收藏夹商品
$cartnewSum.on('click', '.clean-all-disable', Cart.cleanAllDisable);
$('#Y_SubmitBtn').on('click', Cart.submit);    // 结算

// 重新加入购物车
$('#Y_delReselWrap').on('click', '[data-role=readd2cart]', Cart.reAdd2Cart);
$('#Y_delReselWrap').on('click', '[data-role=reFav]', Cart.reFav);

module.exports = Cart;