Blame view

public/js/cart/index.page.js 4.31 KB
1 2 3 4 5
/**
 * 购物车商品
 * @author: feng.chen<feng.chen@yoho.cn>
 * @date: 2016/12/22
 */
陈峰 authored
6
'use strict';
7
陈峰 authored
8
require('cart/index.page.css');
9
require('common');
lijing authored
10 11 12

let cookie = require('yoho-cookie');
陈峰 authored
13
let $ = require('yoho-jquery'),
陈峰 authored
14
    hbsTemplate = require('cart/index/index.hbs'),
陈峰 authored
15
    lazyLoad = require('yoho-jquery-lazyload'),
16
    loading = require('plugin/loading');
17 18

let indexObj = {
陈峰 authored
19
    cartData: {},
陈峰 authored
20
    editMode: false,
陈峰 authored
21
    posting: false,
22
    init() {
陈峰 authored
23
        let self = this;
陈峰 authored
24
25
        self.setCartReferrer();
陈峰 authored
26
        $('.nav-btn').on('click', () => {
27 28 29 30
            self.navBtnClick();
        });
        $('.nav-back').on('click', () => {
            self.navBackClick();
陈峰 authored
31
        });
陈峰 authored
32 33 34 35 36 37 38
        let promise = self.refreshPage();

        if (promise) {
            promise.then(() => {
                require('./index/recommend-for-you').init(self);
            });
        }
陈峰 authored
39
    },
40 41 42 43 44
    setCartReferrer() {
        let referrer = document.referrer || window.queryString.referrer || '';
        let localUrl = window.location.href.replace(/\/?\?.*$/, '').replace(/https?:\/\//, '');
        let referrerPath = referrer.replace(/\/?\?.*$/, '').replace(/https?:\/\//, '');
陈峰 authored
45 46 47
        if (referrer && localUrl !== referrerPath &&
            referrer.indexOf('orderEnsure') === -1 && referrer.indexOf('/gift') === -1 &&
            referrer.indexOf('/advanceBuy') === -1 && referrer.indexOf('/product/index/index?promotion_id=') === -1) {
48 49 50
            cookie.set('_cartReferrer', referrer);
        }
    },
51
    navBtnClick() {
陈峰 authored
52 53 54 55 56 57 58 59 60 61
        let self = this;

        $('.main-wrap').toggleClass('edit');
        if ($('.main-wrap').hasClass('edit')) {
            self.editMode = true;
            $('.chk.edit').removeClass('checked');
        } else {
            self.editMode = false;
        }
    },
62 63 64
    navBackClick() {
        let referrer = cookie.get('_cartReferrer');
65
        if (referrer && referrer !== window.location.href) {
66
            cookie.remove('_cartReferrer');
陈峰 authored
67 68 69 70 71
            if (referrer === document.referrer || referrer === window.queryString.referrer) {
                window.history.go(-1);
            } else {
                window.location.href = referrer;
            }
72 73 74 75
        } else {
            window.location.href = '/?go=1';
        }
    },
陈峰 authored
76 77 78 79 80 81
    getCartData() {
        return $.ajax({
            url: '/cart/index/new/data',
            type: 'POST'
        });
    },
陈峰 authored
82
    refreshPage(data) {
陈峰 authored
83 84
        let self = this;
陈峰 authored
85
        if (!data) {
陈峰 authored
86
            loading.showLoading();
陈峰 authored
87
            return self.getCartData().then(cartData => {
陈峰 authored
88 89 90
                return self.renderPage(cartData.data, true).then(() => {
                    loading.hideLoading();
                });
陈峰 authored
91
            });
陈峰 authored
92 93 94 95 96 97
        }
        return self.renderPage(data);
    },
    renderPage(data, no_try) {
        let self = this;
陈峰 authored
98
        if ((!data || (!data.commonCart && !data.preSellCart)) && no_try !== true) {
陈峰 authored
99
            return self.refreshPage();
陈峰 authored
100
        }
陈峰 authored
101
        let cartType = cookie.get('_cartType') || 'ordinary';
陈峰 authored
102
陈峰 authored
103
        // 普通购物车空,则显示预售购物车
陈峰 authored
104
陈峰 authored
105 106 107
        if (data.commonGoodsCount === 0 && data.ordinarySoldOut === 0) {
            data.cartNav = false;
            data.cartType = 'advance';
陈峰 authored
108
            self.cartData = data.preSellCart;
陈峰 authored
109
        } else if (data.presellGoodsCount === 0 && data.advanceSoldOut === 0) { // 预售购物车空,则显示普通购物车
陈峰 authored
110 111
            data.cartNav = false;
            data.cartType = 'ordinary';
陈峰 authored
112
            self.cartData = data.commonCart;
陈峰 authored
113
        } else { // 以上两个购物车中都有数据, 默认显示普通购物车
陈峰 authored
114 115
            data.cartNav = true;
            data.cartType = cartType || 'ordinary';
陈峰 authored
116
            self.cartData = data.commonCart;
陈峰 authored
117
        }
陈峰 authored
118
        cookie.set('_cartType', data.cartType);
陈峰 authored
119 120
        data.ordinaryCart = data.cartType === 'ordinary';
        data.advanceCart = !data.ordinaryCart;
陈峰 authored
121 122 123 124 125
        if (data.isEmptyCart) {
            $('.nav-btn').hide();
        } else {
            $('.nav-btn').show();
        }
陈峰 authored
126 127 128
        $('.cart-box').html(hbsTemplate(data));
        require('./index/cart').init(self);
        require('./index/good').init(self);
郭成尧 authored
129
        require('./index/bundle').init(self);
陈峰 authored
130 131 132
        $('.btn-edit').on('click', () => {
            self.navBtnClick();
        });
陈峰 authored
133
        lazyLoad($('img.lazy'));
陈峰 authored
134
        return Promise.resolve();
135
    }
陈峰 authored
136
};
137 138 139

$(() => {
    indexObj.init();
陈峰 authored
140
});