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

'use strict';

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

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

        self.handle = handle;

        $('.cart-nav').on('click', '.nav-item', 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');

        cookie.remove('_cartType');
        cookie.set('_cartType', type);
        self.handle.refreshPage('');
    },
    initPresellTip() {
        if (cookie.enabled() && cookie.get('_hasShowCartPresellTip') === 'y') {
            $('#presell-tip').removeClass('show').addClass('hide');
        } else {
            $('#presell-tip').removeClass('hide').addClass('show');
            setTimeout(function() {
                $('#presell-tip').removeClass('show').addClass('hide');
                cookie.set('_hasShowCartPresellTip', 'y');
            }, 3000);
        }
    },
    promoItemClick(e) {
        let self = this;
        let promotionId = $(e.currentTarget).data('id'),
            promotionType = $(e.currentTarget).data('type'),
            status = $(e.currentTarget).data('status'),
            promotionTitle = encodeURIComponent(`以下商品参加【${$(e.currentTarget).data('title')}】促销活动`);

        if (status === 20 || status === 0 || (promotionType !== 'Gift' && promotionType !== 'Needpaygift')) {
            window.location.href =
                `/product/index/index?promotion_id=${promotionId}&title=优惠活动商品&intro_text=${promotionTitle}`;
        } else {
            self.toPromotionPage(promotionId, promotionType, status);
        }
    },
    allGiftBoxClick(e) {
        let self = this;
        let promotionIds;

        if ($(e.currentTarget).find('.no-storage').length) {
            return;
        }
        if ($(e.currentTarget).hasClass('advanceBuy')) {
            if (self.handle.cartData.selectAdvanceBuy) {
                promotionIds = self.handle.cartData.selectAdvanceBuy.map(advanceBuy => {
                    return advanceBuy.promotion_id;
                });
                self.toPromotionPage(promotionIds, 'Needpaygift');
            }
        } else {
            if (self.handle.cartData.selectFreebie) {
                promotionIds = self.handle.cartData.selectFreebie.map(freebie => {
                    return freebie.promotion_id;
                });
                self.toPromotionPage(promotionIds, 'Gift');
            }
        }
    },
    toPromotionPage(promotioIds, promotionType, status) {
        let href;
        let cartType = cookie.get('_cartType') || 'ordinary';

        if (promotionType === 'Gift') {
            href = `/cart/index/new/gift?promotion_ids=${promotioIds}&cartType=${cartType}`;
        } else {
            href = `/cart/index/new/advanceBuy?promotion_ids=${promotioIds}&cartType=${cartType}`;
        }
        if (status === 30) {
            href += '&edit=1';
        }
        window.location.href = href;
    },
    balanceClick() {
        let self = this;
        let cartType = cookie.get('_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 (cookie.get('_cartType') === 'ordinary' && !cookie.get('_realyGift') &&
            self.handle.cartData.matchGifts && self.handle.cartData.matchGifts.length) {
            dialog.showDialog({
                dialogText: '您还未选择赠品,是否去选择赠品',
                hasFooter: {
                    leftBtnText: '我不要赠品',
                    rightBtnText: '去选择'
                }
            }, function() {
                cookie.set('_realyGift', true); // 提示去选择后不再提示
                self.toOrderEnsureGift();
            }, function() {
                self.toOrderEnsure(cartType);
            });
            return false;
        }
        self.toOrderEnsure(cartType);
    },
    toOrderEnsure(cartType) {
        if (this.isEnsure) {
            return false;
        }

        this.isEnsure = true;
        loading.showLoading();

        cookie.get('_realyGift') && cookie.remove('_realyGift');
        cookie.get('order-info') && cookie.remove('order-info');

        $.ajax({
            type: 'get',
            url: '/cart/index/new/orderEnsureAjax',
            timeout: 10000, // 10s overtime
            data: {
                cartType: cartType
            },
            success: (result) => {
                this.isEnsure = false;
                loading.hideLoading();

                if (result.error) {
                    tip.show(result.message);
                    return false;
                }

                window.location.href = '/cart/index/new/orderEnsure?cartType=' + cartType;
            },
            error: () => {
                this.isEnsure = false;
                loading.hideLoading();
                tip.show('网络出现了问题,请稍后再试...');
            }
        });
    },
    toOrderEnsureGift() {
        let self = this;

        if (self.handle.cartData.matchGifts && self.handle.cartData.matchGifts.length) {
            self.toPromotionPage(self.handle.cartData.matchGifts, 'Gift');
        }
    }
};

module.exports = cartObj;