cart-handle.js
6.24 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
function AllGoodsSelect() {
let index = 0;
let isSelectAll = true;
let lowStorage = false;
let goodsList = [];
this.check = function(list) {
(list || []).map(item => {
goodsList.push(item);
// 购物车商品唯一key
item.indexKey = `${item.product_sku}_${index++}`;
if (item.buy_number - item.storage_number > 0) {
lowStorage = true;
} else if (item.goods_type !== 'gift' && item.selected !== 'Y') {
isSelectAll = false;
}
});
};
this.isSelectAll = function() {
return isSelectAll;
};
this.hasLowStorage = function() {
return lowStorage;
};
this.getAllGoodsList = function() {
return goodsList;
};
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:
// Cashreduce, Degressdiscount, Cheapestfree, Discount,Gift,Needpaygift,SpecifiedAmount
if (promotion.promotion_type === 'Gift') {
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, needAllGoods) => {
data = data || {};
data = data.ordinary_cart_data || {};
let ordinaryCart = {
shippingCostTips: '',
priceDownTips: ''
};
let isEmptyCart = true;
let pid = 10;
// 免邮提示
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);
item.promotionMapId = pid++;
if (item.sub_pool && item.sub_pool.length) {
item.sub_pool.map(spItem => {
spItem.goods_list && allGoodsSelect.check(spItem.goods_list);
spItem.promotion_list = _processPromotion(spItem.promotion_list, true);
spItem.promotionMapId = pid++;
});
}
});
isEmptyCart = false;
}
ordinaryCart.goodsPoolList = goodsPoolList;
// 商品列表
let goodsList = data.goods_list;
if (goodsList && goodsList.length) {
isEmptyCart = false;
allGoodsSelect.check(goodsList);
}
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);
ordinaryCart.gGiftAndPriceGiftList = gGiftAndPriceGiftList;
// 失效商品
ordinaryCart.invalidGoodsList = [...(data.off_shelves_goods_list || []), ...(data.sold_out_goods_list || [])];
if (ordinaryCart.invalidGoodsList.length) {
isEmptyCart = false;
}
ordinaryCart.shoppingCartData = data.shopping_cart_data;
ordinaryCart.matchGiftIds = data.match_gift_ids;
ordinaryCart.isSelectAll = allGoodsSelect.isSelectAll();
ordinaryCart.hasLowStorage = allGoodsSelect.hasLowStorage();
ordinaryCart.promotionMoreStatus = {};
needAllGoods && (ordinaryCart.allGoodsList = allGoodsSelect.getAllGoodsList());
ordinaryCart.promotionInfo = data.promotion_info || [];
return {
isEmptyCart,
...ordinaryCart
};
};
const changeGoodsListData = (list, selected) => {
let glist = [];
if (['Y', 'N'].indexOf(selected) < 0) {
selected = false;
}
(list || []).map(item => {
if (!item.lowStorage && item.goods_type !== 'gift') {
glist.push({
buy_number: item.buy_number || 0,
goods_type: item.goods_type || '',
product_sku: item.product_sku || '',
promotion_id: item.promotion_id || 0,
selected: selected || item.selected,
activity_id: item.bundle_activity_id || 0,
batch_no: item.batch_no || 0,
});
}
});
return glist;
};
export default {
formatOrdinaryCartData,
changeGoodsListData
};