cart.js 6.06 KB
/**
 * 购物车页面操作
 * @author: feng.chen<feng.chen@yoho.cn>
 * @date: 2016/12/29
 */

'use strict';

let $ = require('yoho-jquery'),
    tip = require('../../plugin/tip'),
    dialog = require('../../plugin/dialog');

let cartObj = {
    init(handle) {
        let self = this;

        self.handle = handle;

        $('.cart-nav').on('click', 'li', function(e) {
            self.cartNavClick(e);
        });
        $('.more-box>.down-arrow').on('click', function(e) {
            self.arrowClick(e);
        });
        $('.btn-balance').on('click', function() {
            self.balanceClick();
        });
        $('.promo-item').on('click', function(e) {
            self.promoItemClick(e);
        });
        $('.all-gift-box').on('click', '.gift-item', (e) => {
            self.allGiftBoxClick(e);
        });
        self.initPresellTip();
    },
    arrowClick(e) {
        $(e.currentTarget).parent().toggleClass('down');
    },
    cartNavClick(e) {
        let self = this;
        let type = $(e.currentTarget).data('type');

        window.setCookie('cartType', type);
        self.handle.refreshPage('');
    },
    initPresellTip() {
        if (typeof window.cookie === 'function' && window.cookie('_hasShowCartPresellTip') === 'y') {
            $('#presell-tip').removeClass('show').addClass('hide');
        } else {
            $('#presell-tip').removeClass('hide').addClass('show');
            setTimeout(function() {
                $('#presell-tip').removeClass('show').addClass('hide');
                window.setCookie('_hasShowCartPresellTip', 'y');
            }, 3000);
        }
    },
    promoItemClick(e) {
        let self = this;
        let promotionId = $(e.currentTarget).data('id');
        let promotionType = $(e.currentTarget).data('type');
        let promotionTitle = encodeURIComponent($(e.currentTarget).data('title'));

        self.toPromotionPage({
            promotionType,
            promotionId,
            promotionTitle,
            status: $(e.currentTarget).data('status')
        });
    },
    toPromotionPage(opts) {
        let cartType = window.cookie('cartType') || 'ordinary';
        let title = '促销商品';

        if (opts.promotionType === 'Gift') {
            if (opts.status === 30) {
                window.location.href = `/cart/index/new/gift?promotion_id=${opts.promotionId}&title=${title}&intro_text=${opts.promotionTitle}&cartType=${cartType}&edit=1`;
            } else {
                window.location.href = `/cart/index/new/gift?promotion_id=${opts.promotionId}&title=${title}&intro_text=${opts.promotionTitle}&cartType=${cartType}`;
            }

        } else if (opts.promotionType === 'Needpaygift') {
            if (opts.status === 30) {
                window.location.href = `/cart/index/new/advanceBuy?promotion_id=${opts.promotionId}&title=${title}&intro_text=${opts.promotionTitle}&cartType=${cartType}&edit=1`;
            } else {
                window.location.href = `/cart/index/new/advanceBuy?promotion_id=${opts.promotionId}&title=${title}&intro_text=${opts.promotionTitle}&cartType=${cartType}`;
            }

        } else {
            window.location.href = `/product/index/index?promotion_id=${opts.promotionId}&title=${title}&intro_text=${opts.promotionTitle}&cartType=${cartType}`;
        }
    },
    allGiftBoxClick(e) {
        let cartType = window.cookie('cartType') || 'ordinary';

        if ($(e.currentTarget).hasClass('advanceBuy')) {
            window.location.href = '/cart/index/new/advanceBuy?cartType=' + cartType;
        } else {
            window.location.href = '/cart/index/new/gift?cartType=' + cartType;
        }
    },
    balanceClick() {
        let self = this;
        let cartType = window.cookie('cartType') || 'ordinary';

        if (window._yas && window._yas.sendCustomInfo) {
            let productId = Array.from($('.good-item.is-checked').map((i, e) => $(e).data('id')));

            setTimeout(function() {
                if (window._yas && window._yas.sendCustomInfo) {
                    window._yas.sendCustomInfo({
                        op: 'YB_SC_TOBUY_CLICK',
                        param: JSON.stringify({
                            C_ID: window.C_ID,
                            PRD_ID: productId.join(','),
                        })
                    }, true);
                }
            }, 200);
        }
        let lowStocks = $('.good-item.low-stocks.is-checked').length;

        if (lowStocks > 0) {
            tip.show(`所选商品中有${lowStocks}种库存不足的商品`);
            return false;
        }
        if (!$('.good-item.is-checked').length) {
            tip.show('请先勾选商品');
            return false;
        }
        if ($('.all-gift-box>.freebie').length || $('.promo-item[data-type="Gift"][data-status="10"]').length) {
            dialog.showDialog({
                dialogText: '您还未选择赠品,是否去选择赠品',
                hasFooter: {
                    leftBtnText: '我不要赠品',
                    rightBtnText: '去选择'
                }
            }, function() {
                self.toGiftPromotion(cartType);
            }, function() {
                self.toOrderEnsure(cartType);
            });
            return false;
        }
        self.toOrderEnsure(cartType);
    },
    toOrderEnsure(cartType) {
        window.location.href = '/cart/index/new/orderEnsure?cartType=' + cartType;
    },
    toGiftPromotion(cartType) {
        let self = this;
        let promotionEles = $('.promo-item[data-type="Gift"][data-status="10"]');

        if (promotionEles.length) {
            let promotionEle = promotionEles.eq(0);

            self.toPromotionPage({
                promotionType: $(promotionEle).data('type'),
                promotionId: $(promotionEle).data('id'),
                promotionTitle: encodeURIComponent($(promotionEle).data('title')),
                status: $(promotionEle).data('status')
            });
        } else if ($('.all-gift-box>.freebie').length) {
            window.location.href = '/cart/index/new/gift?cartType=' + cartType;
        }
    }
};

module.exports = cartObj;