Blame view

public/js/cart/index/cart.js 6.75 KB
陈峰 authored
1 2 3 4 5 6 7 8
/**
 * 购物车页面操作
 * @author: feng.chen<feng.chen@yoho.cn>
 * @date: 2016/12/29
 */

'use strict';
陈峰 authored
9
let $ = require('yoho-jquery'),
陈峰 authored
10
    cookie = require('yoho-cookie'),
郝肖肖 authored
11
    tip = require('plugin/tip'),
郝肖肖 authored
12
    loading = require('plugin/loading'),
郝肖肖 authored
13
    dialog = require('plugin/dialog');
陈峰 authored
14 15 16 17

let cartObj = {
    init(handle) {
        let self = this;
陈峰 authored
18
陈峰 authored
19 20
        self.handle = handle;
陈峰 authored
21
        $('.cart-nav').on('click', '.nav-item', function(e) {
陈峰 authored
22
            self.cartNavClick(e);
陈峰 authored
23
        });
陈峰 authored
24 25
        $('.more-box>.down-arrow').on('click', function(e) {
            self.arrowClick(e);
陈峰 authored
26
        });
陈峰 authored
27
        $('.btn-balance').on('click', function() {
陈峰 authored
28
            self.balanceClick();
陈峰 authored
29
        });
陈峰 authored
30 31
        $('.promo-item').on('click', function(e) {
            self.promoItemClick(e);
陈峰 authored
32
        });
陈峰 authored
33
        $('.all-gift-box').on('click', '.gift-item', (e) => {
陈峰 authored
34
            self.allGiftBoxClick(e);
陈峰 authored
35
        });
陈峰 authored
36 37 38 39 40
        self.initPresellTip();
    },
    arrowClick(e) {
        $(e.currentTarget).parent().toggleClass('down');
    },
陈峰 authored
41
    cartNavClick(e) {
陈峰 authored
42
        let self = this;
陈峰 authored
43
        let type = $(e.currentTarget).data('type');
陈峰 authored
44
45
        cookie.remove('_cartType');
陈峰 authored
46
        cookie.set('_cartType', type);
陈峰 authored
47 48 49
        self.handle.refreshPage('');
    },
    initPresellTip() {
陈峰 authored
50
        if (cookie.enabled() && cookie.get('_hasShowCartPresellTip') === 'y') {
陈峰 authored
51 52 53 54 55
            $('#presell-tip').removeClass('show').addClass('hide');
        } else {
            $('#presell-tip').removeClass('hide').addClass('show');
            setTimeout(function() {
                $('#presell-tip').removeClass('show').addClass('hide');
陈峰 authored
56
                cookie.set('_hasShowCartPresellTip', 'y');
陈峰 authored
57 58
            }, 3000);
        }
陈峰 authored
59
    },
陈峰 authored
60
    promoItemClick(e) {
61
        let self = this;
陈峰 authored
62 63 64 65
        let promotionId = $(e.currentTarget).data('id'),
            promotionType = $(e.currentTarget).data('type'),
            status = $(e.currentTarget).data('status'),
            promotionTitle = encodeURIComponent(`以下商品参加【${$(e.currentTarget).data('title')}】促销活动`);
66
陈峰 authored
67
        if (status === 20 || status === 0 || (promotionType !== 'Gift' && promotionType !== 'Needpaygift')) {
陈峰 authored
68 69
            window.location.href =
                `/product/index/index?promotion_id=${promotionId}&title=优惠活动商品&intro_text=${promotionTitle}`;
陈峰 authored
70 71
        } else {
            self.toPromotionPage(promotionId, promotionType, status);
陈峰 authored
72 73 74
        }
    },
    allGiftBoxClick(e) {
陈峰 authored
75 76
        let self = this;
        let promotionIds;
陈峰 authored
77
陈峰 authored
78 79 80
        if ($(e.currentTarget).find('.no-storage').length) {
            return;
        }
陈峰 authored
81
        if ($(e.currentTarget).hasClass('advanceBuy')) {
陈峰 authored
82 83 84 85 86 87
            if (self.handle.cartData.selectAdvanceBuy) {
                promotionIds = self.handle.cartData.selectAdvanceBuy.map(advanceBuy => {
                    return advanceBuy.promotion_id;
                });
                self.toPromotionPage(promotionIds, 'Needpaygift');
            }
陈峰 authored
88
        } else {
陈峰 authored
89 90 91 92 93 94
            if (self.handle.cartData.selectFreebie) {
                promotionIds = self.handle.cartData.selectFreebie.map(freebie => {
                    return freebie.promotion_id;
                });
                self.toPromotionPage(promotionIds, 'Gift');
            }
陈峰 authored
95 96
        }
    },
陈峰 authored
97 98 99 100 101 102 103 104 105 106 107 108 109 110
    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;
    },
陈峰 authored
111
    balanceClick() {
112
        let self = this;
陈峰 authored
113
        let cartType = cookie.get('_cartType') || 'ordinary';
陈峰 authored
114
陈峰 authored
115
        if (window._yas && window._yas.sendCustomInfo) {
陈峰 authored
116
            let productId = Array.from($('.good-item.is-checked').map((i, e) => $(e).data('id')));
陈峰 authored
117
陈峰 authored
118 119 120 121 122
            setTimeout(function() {
                if (window._yas && window._yas.sendCustomInfo) {
                    window._yas.sendCustomInfo({
                        op: 'YB_SC_TOBUY_CLICK',
                        param: JSON.stringify({
陈峰 authored
123
                            C_ID: window.C_ID,
陈峰 authored
124 125 126
                            PRD_ID: productId.join(','),
                        })
                    }, true);
陈峰 authored
127
                }
陈峰 authored
128 129 130
            }, 200);
        }
        let lowStocks = $('.good-item.low-stocks.is-checked').length;
陈峰 authored
131
陈峰 authored
132 133 134 135
        if (lowStocks > 0) {
            tip.show(`所选商品中有${lowStocks}种库存不足的商品`);
            return false;
        }
136 137 138 139
        if (!$('.good-item.is-checked').length) {
            tip.show('请先勾选商品');
            return false;
        }
郭成尧 authored
140 141
        if (cookie.get('_cartType') === 'ordinary' && !cookie.get('_realyGift') &&
            self.handle.cartData.matchGifts && self.handle.cartData.matchGifts.length) {
陈峰 authored
142 143 144 145 146 147 148
            dialog.showDialog({
                dialogText: '您还未选择赠品,是否去选择赠品',
                hasFooter: {
                    leftBtnText: '我不要赠品',
                    rightBtnText: '去选择'
                }
            }, function() {
毕凯 authored
149
                cookie.set('_realyGift', true); // 提示去选择后不再提示
陈峰 authored
150
                self.toOrderEnsureGift();
陈峰 authored
151
            }, function() {
152
                self.toOrderEnsure(cartType);
陈峰 authored
153 154 155
            });
            return false;
        }
156 157 158
        self.toOrderEnsure(cartType);
    },
    toOrderEnsure(cartType) {
159
        if (this.isEnsure) {
郝肖肖 authored
160 161 162
            return false;
        }
163
        this.isEnsure = true;
郝肖肖 authored
164 165
        loading.showLoading();
陈峰 authored
166
        cookie.get('_realyGift') && cookie.remove('_realyGift');
陈峰 authored
167
        cookie.get('order-info') && cookie.remove('order-info');
168
郝肖肖 authored
169 170 171 172 173 174 175
        $.ajax({
            type: 'get',
            url: '/cart/index/new/orderEnsure',
            timeout: 10000, // 10s overtime
            data: {
                cartType: cartType
            },
176 177
            success: (result) => {
                this.isEnsure = false;
郝肖肖 authored
178 179 180 181 182 183
                loading.hideLoading();

                if (result.error) {
                    tip.show(result.message);
                    return false;
                }
184
郝肖肖 authored
185 186
                window.location.href = '/cart/index/new/orderEnsure?cartType=' + cartType;
            },
187 188
            error: () => {
                this.isEnsure = false;
郝肖肖 authored
189
                loading.hideLoading();
190
                tip.show('网络出现了问题,请稍后再试...');
郝肖肖 authored
191
            }
192
        });
193
    },
陈峰 authored
194
    toOrderEnsureGift() {
195 196
        let self = this;
陈峰 authored
197 198
        if (self.handle.cartData.matchGifts && self.handle.cartData.matchGifts.length) {
            self.toPromotionPage(self.handle.cartData.matchGifts, 'Gift');
199
        }
陈峰 authored
200
    }
陈峰 authored
201
};
陈峰 authored
202
陈峰 authored
203
module.exports = cartObj;