cart-handle.js
4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
function AllGoodsSelect() {
let isSelectAll = true;
this.check = function(list) {
(list || []).map(item => {
if (!item.bLackStorage && item.goods_type !== 'gift' && item.selected !== 'Y') {
isSelectAll = false;
}
});
};
this.val = function() {
return isSelectAll;
};
return this;
}
function _processPromotion(promotionList, isSub) {
if (!promotionList || !promotionList.length) {
return false;
}
promotionList.map(promotion => {
let statuString = '';
switch (+promotion.status) {
case 0:
statuString = '去凑单';
break;
case 10:
if (promotion.promotion_type == 'Gift') { //Cashreduce, Degressdiscount, Cheapestfree, Discount,Gift,Needpaygift,SpecifiedAmount
statuString = '领赠品';
} else if (promotion.promotion_type == 'Needpaygift') {
statuString = '去换购';
}
break;
case 20:
statuString = '已抢光';
promotion.soldOut = true;
break;
case 30:
statuString = '更换';
break;
default:
break;
}
promotion.statuString = statuString;
if (isSub) {
let typeString = '';
switch (promotion.promotion_type) {
case 'Gift':
typeString = '赠品';
break;
case 'Needpaygift':
typeString = '加价购';
break;
case 'Cashreduce':
typeString = '满减';
break;
default:
typeString = '折扣';
break;
}
promotion.typeString = typeString;
}
});
return promotionList;
}
function _processGiftList(list, isGift) {
let result = {
isGift,
title: isGift ? '赠品' : '全场加价购',
statusString: '已抢光'
};
if (!list || !list.length) {
return false;
}
let statusCount10 = 0;//已满足
let statusCount20 = 0;//已售罄
let promotionArr = [];
list.map(item => {
switch (+item.status) {
case 10:
statusCount10++;
promotionArr.push(item.promotion_id);
break;
case 20:
statusCount20++;
break;
default:
break;
}
});
if (statusCount10 === 0 && statusCount20 !== list.length) {
return false;
}
result.promotion_ids = promotionArr.join(',');
if (promotionArr.length > 0) {
result.statusString = isGift ? '领赠品' : '去换购';
result.showArrow = true;
}
return result;
}
/**
* 购物车数据
* @param params
* @returns {*}
*/
const formatOrdinaryCartData = (data) => {
data = data || {};
data = data.ordinary_cart_data || {};
let ordinaryCart = {};
let isEmptyCart = true;
// 免邮提示
if (data.shipping_cost_prompt) {
ordinaryCart.shippingCostTips = data.shipping_cost_prompt.shipping_cost_tips || '';
}
// 降价提示
if (data.price_down_prompt) {
ordinaryCart.priceDownTips = data.price_down_prompt.price_down_tips || '';
}
const allGoodsSelect = new AllGoodsSelect();
// 商品池
let goodsPoolList = data.goods_pool_list;
if (goodsPoolList && goodsPoolList.length) {
goodsPoolList.map(item => {
if (item.pool_batch_no && item.pool_id) {
//套餐商品参数转换
item.int_pool_buy_number = parseInt(item.pool_buy_number, 10);
item.int_pool_storage_number = parseInt(item.pool_storage_number, 10);
}
item.isPromotionExpanded = false;
item.goods_list && allGoodsSelect.check(item.goods_list);
item.promotion_list = _processPromotion(item.promotion_list);
if (item.sub_pool && item.sub_pool.length) {
item.sub_pool.map(spItem => {
spItem.promotion_list = _processPromotion(spItem.promotion_list, true);
spItem.goods_list && allGoodsSelect.check(spItem.goods_list);
})
}
});
isEmptyCart = false;
ordinaryCart.goodsPoolList = goodsPoolList;
}
// 商品列表
let goodsList = data.goods_list;
if (goodsList && goodsList.length) {
isEmptyCart = false;
allGoodsSelect.check(item.goods_list);
ordinaryCart.goodsList = goodsList;
}
// 加价购&赠品
let gGiftAndPriceGiftList = [];
let giftItem = _processGiftList(data.g_gift_list, true);
let priceGiftItem = _processGiftList(data.g_price_gift_list);
giftItem && gGiftAndPriceGiftList.push(giftItem);
priceGiftItem && gGiftAndPriceGiftList.push(priceGiftItem);
if (gGiftAndPriceGiftList.length) {
ordinaryCart.gGiftAndPriceGiftList = gGiftAndPriceGiftList;
}
//失效商品
ordinaryCart.invalidGoodsList = [...(data.off_shelves_goods_list || []), ...(data.sold_out_goods_list || [])];
if (ordinaryCart.invalidGoodsList.length) {
isEmptyCart = false;
}
ordinaryCart.isValidGoodsSelectAll = allGoodsSelect.val();
ordinaryCart.shoppingCartData = data.shopping_cart_data;
if (data.promotion_info && data.promotion_info.length) {
ordinaryCart.promotionInfo = data.promotion_info;
}
console.log(ordinaryCart);
return {
isEmptyCart,
...ordinaryCart
};
}
export default {
formatOrdinaryCartData
};