index.page.js 4 KB
/**
 * 购物车商品
 * @author: feng.chen<feng.chen@yoho.cn>
 * @date: 2016/12/22
 */
'use strict';

let cookie = require('yoho-cookie');

require('cart/index.page.css');
require('../common');
let $ = require('yoho-jquery'),
    hbsTemplate = require('cart/index/index.hbs'),
    lazyLoad = require('yoho-jquery-lazyload'),
    loading = require('../plugin/loading');

let indexObj = {
    cartData: {},
    editMode: false,
    posting: false,
    init() {
        let self = this;

        self.setCartReferrer();
        $('.nav-btn').on('click', () => {
            self.navBtnClick();
        });
        $('.nav-back').on('click', () => {
            self.navBackClick();
        });
        let promise = self.refreshPage();

        if (promise) {
            promise.then(() => {
                require('./index/recommend-for-you').init(self);
            });
        }
    },
    setCartReferrer() {
        let referrer = document.referrer || window.queryString.referrer || '';
        let localUrl = window.location.href.replace(/\/?\?.*$/, '').replace(/https?:\/\//, '');
        let referrerPath = referrer.replace(/\/?\?.*$/, '').replace(/https?:\/\//, '');

        if (referrer && localUrl !== referrerPath && referrer.indexOf('orderEnsure') === -1 && referrer.indexOf('/gift') === -1 && referrer.indexOf('/advanceBuy') === -1 && referrer.indexOf('/product/index/index?promotion_id=') === -1) {
            cookie.set('_cartReferrer', referrer);
        }
    },
    navBtnClick() {
        let self = this;

        $('.main-wrap').toggleClass('edit');
        if ($('.main-wrap').hasClass('edit')) {
            self.editMode = true;
            $('.chk.edit').removeClass('checked');
        } else {
            self.editMode = false;
        }
    },
    navBackClick() {
        let referrer = cookie.get('_cartReferrer');

        if (referrer && referrer !== window.location.href) {
            cookie.remove('_cartReferrer');
            window.location.href = referrer;
        } else {
            window.location.href = '/?go=1';
        }
    },
    getCartData() {
        return $.ajax({
            url: '/cart/index/new/data',
            type: 'POST'
        });
    },
    refreshPage(data) {
        let self = this;

        if (!data) {
            loading.showLoading();
            return self.getCartData().then(cartData => {
                return self.renderPage(cartData.data, true).then(() => {
                    loading.hideLoading();
                });
            });
        }
        return self.renderPage(data);
    },
    renderPage(data, no_try) {
        let self = this;

        if ((!data || (!data.commonCart && !data.preSellCart)) && no_try !== true) {
            self.refreshPage();
            return;
        }
        let cartType = cookie.get('_cartType') || 'ordinary';

        // 普通购物车空,则显示预售购物车

        if (data.commonGoodsCount === 0 && data.ordinarySoldOut === 0) {
            data.cartNav = false;
            data.cartType = 'advance';
            self.cartData = data.preSellCart;
        } else if (data.presellGoodsCount === 0 && data.advanceSoldOut === 0) { // 预售购物车空,则显示普通购物车
            data.cartNav = false;
            data.cartType = 'ordinary';
            self.cartData = data.commonCart;
        } else { // 以上两个购物车中都有数据, 默认显示普通购物车
            data.cartNav = true;
            data.cartType = cartType || 'ordinary';
            self.cartData = data.commonCart;
        }
        cookie.set('_cartType', data.cartType);
        data.ordinaryCart = data.cartType === 'ordinary';
        data.advanceCart = !data.ordinaryCart;
        if (data.isEmptyCart) {
            $('.nav-btn').hide();
        } else {
            $('.nav-btn').show();
        }
        $('.cart-box').html(hbsTemplate(data));
        require('./index/cart').init(self);
        require('./index/good').init(self);
        lazyLoad($('img.lazy'));
        return Promise.resolve();
    }
};

$(() => {
    indexObj.init();
});