cart-process.js
18.4 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/**
* 购物车处理类
*/
'use strict';
const helpers = global.yoho.helpers;
const _ = require('lodash');
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 = `差${Math.abs(promo.condition_value)}件立享`;
} else if (promo.condition_unit === 2) {
title = `差¥${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 = (advanceGood, isGift) => {
return {
promotionId: advanceGood.promotion_id,
promotionTitle: advanceGood.promotion_title,
goods: _.get(advanceGood, 'goods_list', []).map(good => {
return {
id: good.product_skn,
name: good.product_name,
thumb: good.goods_images ? helpers.image(good.goods_images, 120, 160) : '',
price: transPrice(advanceGood.last_price),
marketPrice: isGift ? '0.00' : transPrice(advanceGood.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 = true, 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
};
if (goodData.min_buy_number && goodData.min_buy_number > 1) {
result.minNumber = parseInt(goodData.min_buy_number, 10);
}
result.maxNumber = parseInt(goodData.storage_number, 10);
result.minSelectNum = parseInt(goodData.buy_number, 10) <= parseInt(goodData.min_buy_number, 10) || parseInt(goodData.buy_number, 10) === 1;
result.maxSelectNum = parseInt(goodData.buy_number, 10) >= parseInt(goodData.storage_number, 10) && 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) => {
let result = {};
if (data.promotion_type === 'Gift') {
// 赠品
result.freebie = formatAdvanceGoods(data, true);
} else if (data.promotion_type === 'Needpaygift') {
// 加价购
result.advanceBuy = formatAdvanceGoods(data);
}
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,
giftGoodsList: _.get(promo, 'gift_goods_List', []).map(gift => {
return formatAdvanceGoods(gift);
}),
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)
};
};
/**
* 处理购物车商品、加价购商品、赠品详情数据
*
* @param array $productData 要处理的商品数据
* @param int $num 购买数目
* @return array $data 处理之后的数据
* @internal param null $mnum 量贩最小购买数量
*/
const procGoodsDetail = (productData, num) => {
let data = {};
data.name = productData.product_name;
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, 10);
}
// 商品选择
if (_.has(productData, 'goods_list')) {
let goodsList = productData.goods_list;
let sizeName = '',
colors = [],
colorList = [],
sizes = [],
sizeList = {},
allSizeList = {}, // 所有尺码列表
thumbImageList = [],
colorNum = 0,
totalStorageNum = 0, // 总库存数
colorStorageGroup = {}; // 颜色分组的库存总数集合, 多个之间用/分隔
goodsList.forEach(val => {
// 商品的尺码列表
if (!colorStorageGroup[val.product_skc]) {
colorStorageGroup[val.product_skc] = {};
}
if (_.has(val, 'size_list')) {
// 尺码
if (!sizeList[val.product_skc]) {
sizeList[val.product_skc] = [];
}
val.size_list.forEach(one => {
sizeName = one.size_name;
sizeList[val.product_skc].push({
id: one.size_id,
skuId: one.product_sku,
goodsId: val.goods_id,
colorId: val.color_id,
name: one.size_name,
sizeNum: parseInt(one.storage_number, 10)
});
// 所有尺码列表,赋值用于前端展示默认尺码的时候 判断出没有库存则显示灰色
allSizeList[sizeName] = !_.has(allSizeList, sizeName) ? {
id: one.size_id,
storage: one.storage_number
} : allSizeList[sizeName];
colorNum += parseInt(one.storage_number, 10);
colorStorageGroup[val.product_skc][sizeName] = parseInt(one.storage_number, 10);
});
// 颜色分组
colorList.push({
id: val.color_id,
skcId: val.product_skc,
name: val.factory_goods_name || val.color_name,
colorNum: colorNum,
goodsName: productData.product_name
});
}
// 缩略图
thumbImageList.push({
img: helpers.image(val.color_image, 60, 60)
});
// 商品库存总数
totalStorageNum += colorNum;
});
// 遍历所有尺码,构建颜色显示数据
let i = 1;
if (!sizes[0]) {
sizes[0] = {
size: []
};
}
if (!colors[0]) {
colors[0] = {
color: []
};
}
_.forEach(allSizeList, (value, sName) => {
// 默认尺码
sizes[0].size.push({
name: sName,
sizeNum: _.toNumber(value.storage) > 0 ? true : false,
id: value.id
});
// 各个颜色的尺码, 每行显示一个尺码对应的颜色
if (!colors[i]) {
colors[i] = {
color: []
};
}
colorList.forEach(colorArr => {
colors[i].color.push({
id: colorArr.id,
skcId: colorArr.skcId,
name: colorArr.name,
colorNum: _.has(colorStorageGroup, `${colorArr.skcId}.${sName}`) ? parseInt(colorStorageGroup[colorArr.skcId][sName], 10) : 0,
goodsName: colorArr.goodsName
});
});
colors[i].id = value.id;
++i;
});
// 遍历所有颜色, 构建尺码显示数据
i = 1;
if (!sizes[i]) {
sizes[i] = {
color: []
};
}
colorList.forEach(value => {
// 各个尺码的颜色,每行显示一个颜色的对应尺码
sizes[i] = {
size: sizeList[value.skcId],
colorId: value.skcId
};
// 默认颜色
colors[0].color.push(value);
++i;
});
data.thumbs = thumbImageList;
data.colors = colors;
data.sizes = sizes;
data.totalNum = totalStorageNum;
data.colorName = '颜色';
data.sizeName = '尺码';
}
return data;
};
const procCartData = (data, onlyGift = false, onlyAdvanceBuy = false, isAdvanceCart = true) => {
let result = {};
if (onlyGift) {
// 赠品
result.freebie = data.gift_list.map(good => {
return formatAdvanceGoods(good, true);
});
return result;
}
if (onlyAdvanceBuy) {
// 加价购
result.advanceBuy = data.price_gift.map(good => {
return formatAdvanceGoods(good, true);
});
return 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 (data.gift_list.length || data.price_gift.length) {
result.freebieOrAdvanceBuy = true;
// 赠品
result.freebie = data.gift_list.map(good => {
return formatAdvanceGoods(good);
});
result.giftCount = result.freebie.length;
// 加价购
result.advanceBuy = data.price_gift.map(good => {
return formatAdvanceGoods(good);
});
result.advanceBuyCount = result.advanceBuy.length;
}
// 已参加的活动
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 = 'all', onlyGift = false, onlyAdvanceBuy = false) => {
let cart = data.data;
let result = {};
if (onlyGift || onlyAdvanceBuy) { // 加价购或者赠品数据
return procCartData(cart.ordinary_cart_data, onlyGift, onlyAdvanceBuy);
}
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, onlyGift, onlyAdvanceBuy, false);
/* 预售购物车 */
result.presellGoodsCount = advanceCount;
result.advanceSoldOut = advanceSoldOut.length;
result.preSellCart = procCartData(cart.advance_cart_data, onlyGift, onlyAdvanceBuy);
return result;
};
/**
* 获取处理量贩数据
* @param $productSkn
* @return array
*/
const handleBundleInfo = (apiResult) => {
let result = {};
if (apiResult && apiResult.code === 200 && apiResult.data) {
if (_.has(apiResult.data, 'bundleInfo')) {
result.mnum = 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
};