cart-process.js
14.3 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
* 购物车处理类
*/
'use strict';
const helpers = global.yoho.helpers;
const _ = require('lodash');
const productProcess = require('./product-process');
const regPromoTitle = /^【[^】]+】(.*)/;
const transPrice = (price, isSepcialZero) => {
return (price > 0 || isSepcialZero) ? parseFloat(price).toFixed(2) : 0;
};
const formatPromotionTitle = (promo) => {
let title;
if (promo.status === 0) {
if (promo.condition_unit === 1) {
title = `差${transPrice(Math.abs(promo.condition_value))}件立享`;
} else if (promo.condition_unit === 2) {
title = `差¥${transPrice(Math.abs(promo.condition_value))}立享`;
}
} else {
title = '已满足';
}
let match = regPromoTitle.exec(promo.promotion_title);
let promotionTitle = match !== null && match.length > 1 ? match[1] : promo.promotion_title;
return `${title}【${promotionTitle}】`;
};
const formatPromotionOpt = (promo) => {
if (promo.status === 0) {
return '去凑单';
}
if (promo.status === 10) {
if (promo.promotion_type === 'Needpaygift') {
return '去换购';
}
if (promo.promotion_type === 'Gift') {
return '领赠品';
}
return '';
}
if (promo.status === 20) {
return '已抢光';
}
if (promo.status === 30) {
return '更换';
}
};
/**
* 格式化加价购和赠品商品
*
* @param array $advanceGoods 加价购商品列表
* @param int $count 计商品件数
* @return array $arr 处理之后的加价购商品数据
*/
const formatAdvanceGoods = (gifts, isGift) => {
return _.map(gifts, gift => {
return {
promotionId: gift.promotion_id,
promotionTitle: gift.promotion_title,
isGift: isGift,
goods: _.map(gift.goods_list, good => {
return {
id: good.product_skn,
name: good.product_name,
thumb: good.goods_images ? helpers.image(good.goods_images, 120, 160) : '',
price: transPrice(good.last_price),
marketPrice: isGift ? '0.00' : transPrice(good.market_price),
count: good.storage_number
};
})
};
});
};
/**
* 格式化购物车商品
*
* @param array $cartGoods 购物车商品列表
* @param bool $isAdvanceCart 是否是预售购物车(和上市期有关)
* @param boolean $isValid 是否是可用商品(非失效商品),默认是
* @param boolean $inValidLow 是否失效类型的库存不足,默认否
* @return array 处理之后的购物车商品数据
*/
const formatCartGoods = (goodData, isAdvanceCart, isValid, inValidLow) => {
if (isValid === undefined) {
isValid = true;
}
if (inValidLow === undefined) {
inValidLow = false;
}
let result = {
id: goodData.product_sku,
skn: goodData.product_skn,
name: goodData.product_name,
thumb: goodData.goods_images ? helpers.image(goodData.goods_images, 120, 160) : '',
color: goodData.color_name,
size: goodData.size_name,
checked: goodData.selected === 'Y',
price: transPrice(goodData.last_vip_price),
isVipPrice: goodData.sales_price !== goodData.last_vip_price && goodData.discount_tag === 'V',
isStudents: goodData.sales_price !== goodData.last_vip_price && goodData.discount_tag === 'S',
count: goodData.buy_number,
promotion_id: _.toNumber(goodData.promotion_id) === 0 ? '' : goodData.promotion_id
};
goodData.storage_number = _.parseInt(goodData.storage_number);
goodData.min_buy_number = _.parseInt(goodData.min_buy_number);
goodData.buy_number = _.parseInt(goodData.buy_number);
if (goodData.min_buy_number && goodData.min_buy_number > 0) {
result.minNumber = goodData.min_buy_number;
}
result.maxNumber = goodData.storage_number;
result.minSelectNum = goodData.buy_number <= goodData.min_buy_number || goodData.buy_number === 1;
result.maxSelectNum = goodData.buy_number >= goodData.storage_number && goodData.goods_type === 'ordinary';
if (isValid) {
// 库存不足
result.lowStocks = goodData.buy_number > goodData.storage_number;
} else { // 失效商品
result.inValid = true;
result.inValidLow = inValidLow;
}
// gift=>是否赠品,advanceBuy=>是否加价购,soldOut=>失效商品;
if (!goodData.goods_type) {
result.inValid = true;
} else if (goodData.goods_type === 'gift' && !goodData.isAdvanceBuy) {
result.isGift = true;
result.salesPrice = transPrice(goodData.sales_price);
result.price = transPrice(goodData.last_price);
result.maxNumber = 1;
} else if (goodData.goods_type === 'price_gift') {
result.showCheckbox = true;
result.isAdvanceBuy = true;
result.salesPrice = transPrice(goodData.sales_price);
result.price = transPrice(goodData.last_price);
result.maxNumber = 1;
} else {
result.showCheckbox = true;
}
// 上市期
if (isAdvanceCart && goodData.expect_arrival_time) {
result.appearDate = goodData.expect_arrival_time;
}
// 商品链接
result.link = helpers.urlFormat(`/product/show_${goodData.product_skn}.html`);
return result;
};
const procPriceGiftData = (data, promotionType) => {
let result = {};
let arrays = _.get(data, 'arrays', []);
// 赠品和加价购
result.gifts = formatAdvanceGoods(arrays, promotionType === 'Gift');
if (result.gifts.length === 1) {
result.promotionTitle = result.gifts[0].promotionTitle;
}
return result;
};
const getPromotionFlag = (promo) => {
switch (promo.promotion_type) {
case 'Cashreduce': // 满减
case 'SpecifiedAmount': // X件X元
case 'Cheapestfree':// 满X免1
return '满减';
case 'Degressdiscount':// 分件折扣
case 'Discount':// 打折
return '折扣';
case 'Gift':// 赠品
return '赠品';
case 'Needpaygift':// 加价购
return '加价购';
default:
return '';
}
};
const formatPromotion = (promo) => {
return {
status: promo.status,
conditionUnit: promo.condition_unit,
conditionValue: promo.condition_value,
giftPrice: promo.gift_price,
promotionId: promo.promotion_id,
promotionOriginTitle: promo.promotion_title,
promotionTitle: formatPromotionTitle(promo),
promotionType: promo.promotion_type,
alreadyMatch: promo.alreadyMatch,
optTitle: formatPromotionOpt(promo),
promotionFlag: getPromotionFlag(promo),
noStorage: promo.status === 20
};
};
/**
* 处理购物车商品、加价购商品、赠品详情数据
*
* @param array $productData 要处理的商品数据
* @param int $num 购买数目
* @return array $data 处理之后的数据
* @internal param null $mnum 量贩最小购买数量
*/
const procGoodsDetail = (productData, num) => {
let data = {};
let sizeInfo = productProcess.processSizeInfo(productData);
Object.assign(data, sizeInfo);
data.productSkn = productData.product_skn;
if (_.has(productData, 'special_price')) { // 加价购或者赠品的销售价字段
data.price = productData.format_market_price;
data.salePrice = productData.format_sales_price;
} else { // 购物车商品的销售价字段
data.price = productData.market_price > productData.sales_price ? productData.format_market_price : false;
data.salePrice = '¥' + transPrice(productData.sales_price);
}
if (_.has(productData, 'storage_sum')) {
data.storage = productData.storage_sum;
}
data.num = 1;
if (num) {
data.num = _.parseInt(num);
}
data.colorName = '颜色';
data.sizeName = '尺码';
return data;
};
const procCartData = (data, isAdvanceCart) => {
if (isAdvanceCart === undefined) {
isAdvanceCart = true;
}
let result = {};
// 购买的可用商品列表
result.goods = _.get(data, 'goods_list', []).map(good => {
let gr = formatCartGoods(good, isAdvanceCart);
if (gr.isGift) {
gr.noEdit = true;
}
return gr;
});
result.goodPools = _.get(data, 'goods_pool_list', []).map(pool => {
return {
isBrand: pool.pool_type <= 1,
isPromotion: pool.pool_type === 2,
poolTitle: pool.pool_title,
goods: _.get(pool, 'goods_list', []).map(good => {
return formatCartGoods(good, isAdvanceCart);
}),
promotions: _.get(pool, 'promotion_list', []).map(promo => {
return formatPromotion(promo);
}),
promotionMore: _.get(pool, 'promotion_list', []).length > 1,
sub_pool: _.get(pool, 'sub_pool', []).map(subPool => {
return {
isBrand: subPool.pool_type <= 1,
isPromotion: subPool.pool_type === 2,
goods: _.get(subPool, 'goods_list', []).map(good => {
return formatCartGoods(good, isAdvanceCart);
}),
promotions: _.get(subPool, 'promotion_list', []).map(promo => {
return formatPromotion(promo);
}),
promotionMore: _.get(subPool, 'promotion_list', []).length > 1
};
})
};
});
// 失效商品列表,库存为0
result.notValidGoods = _.get(data, 'sold_out_goods_list', []).map(good => {
return formatCartGoods(good, isAdvanceCart, false, true);
});
// 下架的商品列表
result.offShelveGoods = _.get(data, 'off_shelves_goods_list', []).map(good => {
return formatCartGoods(good, isAdvanceCart, false);
});
// 赠品和加价购商品
if (_.get(data, 'g_gift_list', []).length || _.get(data, 'g_price_gift_list', []).length) {
result.freebieOrAdvanceBuy = true;
// 赠品
result.freebie = data.g_gift_list.filter(freebie => freebie.status !== 30 && freebie.status !== 0);
result.selectFreebie = result.freebie.filter(freebie => freebie.status === 10);
result.giftHasStorage = _.some(result.freebie, freebie => freebie.status === 10);
// 加价购
result.advanceBuy = data.g_price_gift_list.filter(advanceBuy => advanceBuy.status !== 30 && advanceBuy.status !== 0);
result.selectAdvanceBuy = result.advanceBuy.filter(advanceBuy => advanceBuy.status === 10);
result.advanceHasStorage = _.some(result.advanceBuy, advanceBuy => advanceBuy.status === 10);
}
result.matchGifts = _.get(data, 'match_gift_ids', []);
// 已参加的活动
if (data.promotion_info && data.promotion_info.length > 0) {
result.promotionInfo = data.promotion_info.map(promotion => {
return {id: promotion.promotion_id, name: promotion.promotion_title};
});
}
// 计算正常商品且有库存的总数
let goodCount = _.sum(result.goods
.filter(good => good.lowStocks === false)
.map(good => {
return _.parseInt(good.count, 10);
})) + // 普通商品
_.sum(result.goodPools
.map(goodPool => {
return _.sum(_.get(goodPool, 'sub_pool', [])
.map(subPool => {
return _.sum(_.get(subPool, 'goods', [])// 子促销池中的商品
.filter(good => good.lowStocks === false)
.map(good => {
return _.parseInt(good.count, 10);
}));
})) +
_.sum(_.get(goodPool, 'goods', []) // 大促销池中的商品
.filter(good => good.lowStocks === false)
.map(good => {
return _.parseInt(good.count, 10);
}));
}));
// 结算数据
result.formulaPrice = data.shopping_cart_data.promotion_formula;
result.count = data.shopping_cart_data.selected_goods_count;
result.isAllSelected = (goodCount <= data.shopping_cart_data.selected_goods_count) && (data.shopping_cart_data.selected_goods_count > 0);
result.sumPrice = transPrice(data.shopping_cart_data.last_order_amount);
result.hasNoSaleGoods = result.notValidGoods.length || result.offShelveGoods.length;
return result;
};
const processData = (data, cartType) => {
if (cartType === undefined) {
cartType = 'all';
}
let cart = data.data;
let result = {};
let ordinaryCount = _.get(cart, 'ordinary_cart_data.shopping_cart_data.goods_count', 0);
let advanceCount = _.get(cart, 'advance_cart_data.shopping_cart_data.goods_count', 0);
let ordinarySoldOut = _.get(cart, 'ordinary_cart_data.sold_out_goods_list', []);
let advanceSoldOut = _.get(cart, 'advance_cart_data.sold_out_goods_list', []);
// 普通购物车和预售购物车都为空
if (ordinaryCount === 0 && advanceCount === 0 && !ordinarySoldOut.length && !advanceSoldOut.length) {
result.isEmptyCart = true;
return result;
}
/* 普通购物车 */
result.commonGoodsCount = ordinaryCount;
result.ordinarySoldOut = ordinarySoldOut.length;
result.commonCart = procCartData(cart.ordinary_cart_data, false);
/* 预售购物车 */
result.presellGoodsCount = advanceCount;
result.advanceSoldOut = advanceSoldOut.length;
result.preSellCart = procCartData(cart.advance_cart_data);
return result;
};
/**
* 获取处理量贩数据
* @param $productSkn
* @return array
*/
const handleBundleInfo = (apiResult) => {
let result = {};
if (apiResult && apiResult.code === 200 && apiResult.data) {
if (_.has(apiResult.data, 'bundleInfo')) {
result.num = apiResult.data.bundleInfo.bundleCount;
result.discount = _.has(apiResult.data.bundleInfo, 'discount') ? apiResult.data.bundleInfo.discount : false;
result.promotionPhrase = apiResult.data.bundleInfo.promotionPhrase;
}
}
return result;
};
module.exports = {
processData,
procGoodsDetail,
handleBundleInfo,
procPriceGiftData
};