cart-helper.js
13.5 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
/**
* Created by yoho on 2016-12-21.
*/
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const md5 = require('md5');
const PROMOTION_TYPE_TAG_MAP = {
Cashreduce: '满减', // 满减
Degressdiscount: '折扣', // 分件折扣
Cheapestfree: '满减', // 满X免1
Discount: '折扣', // 打折
Gift: '赠品',
Needpaygift: '加价购',
SpecifiedAmount: '满减', // X件X元
FreeShippingCost: '免运费'
};
const GOODS_TYPE_TAG_MAP = {
gift: '赠品',
price_gift: '加价购'
};
/**
* 从用户加入购物车的COOKIE取出购物车凭证
*/
const getShoppingKeyByCookie = (req) => {
return req.cookies._SPK || '';
};
/**
* 转换价格
*
* @param float|string $price 价格
* @return float|string 转换之后的价格
*/
const transPrice = (price) => {
return price ? (price * 1).toFixed(2) : '0.00';
};
/**
* 生成公开的TOKEN凭证
*
* @param string $string 字符串
* @return string
*/
const makeToken = (str) => {
return md5(md5(str + '#@!@#'));
};
/**
* 购物车商品
* @param array cartGoods 购物车商品列表
* @param bool isAdvanceCart 是否是预售购物车(和上市期有关)
* @param bool inValid 是否是不可用商品(失效商品),默认不是(有效商品)
* @param bool isOffShelves 是否卖光了, 默认否
* @param array analysis 第三方数据分析用的
* @return array 处理之后的购物车商品数据
*/
const formatCartGoods = (cartGoods, isAdvanceCart, inValid, isOffShelves, analysis) => {
return _.map(cartGoods, (it) => {
let goods = {
id: it.product_sku,
skn: it.product_skn,
pid: it.product_id,
isChecked: it.selected === 'Y',
productTitle: it.product_name,
imgCover: it.goods_images ? helpers.image(it.goods_images, 60, 60) : '',
productColor: it.factory_goods_name,
productSize: it.size_name,
productPrice: transPrice(it.last_vip_price), // self::transPrice($value['real_price']);
productNum: it.buy_number,
isVipPrice: it.sales_price !== it.last_vip_price && it.discount_tag === 'V',
isStuPrice: it.sales_price !== it.last_vip_price && it.discount_tag === 'S',
yohoIcon: it.get_yoho_coin || 0,
productSubtotal: transPrice(it.last_vip_price * it.buy_number),
promotionId: it.promotion_id || 0,
isLimitSkn: it.is_limit_skn === 'Y', // is_limit_skn=Y 不支持7天无理由退货
isAdvance: isAdvanceCart
};
// 已下架
if (isOffShelves) {
goods.isTipNoStore = true;
goods.tipMessage = '库存不足';
} else if (!inValid) { // 有效
if (it.buy_number > it.storage_number) {
goods.isTipNoStore = true; // 是否在结算时候显示库存不足
goods.tipMessage = '库存不足';
} else if (it.storage_number <= 2) {
goods.tipMessage = '余量有限';
}
} else { // 失效商品
goods.inValid = true;
}
// advanceBuy=>是否加价购,soldOut=>失效商品;
if (!it.goods_type) {
goods.inValid = true;
} else if (it.goods_type === 'gift' && it.isAdvanceBuy) { // gift=>是否赠品
goods.isGift = true;
goods.productPrice = transPrice(it.last_price);
goods.productSubtotal = it.subtotal ? transPrice(it.subtotal) : goods.productPrice;
} else if (it.goods_type === 'price_gift') { // price_gift=>是否加价购
goods.isPriceGift = true;
goods.productPrice = transPrice(it.last_price);
goods.productSubtotal = it.subtotal ? transPrice(it.subtotal) : goods.productPrice;
} else if (it.real_price === 0) { // 免单
goods.productPrice = transPrice(it.sales_price);
goods.xForOne = true;
// 分析用: 商品ID列表
if (_.isArray(_.get(analysis, 'ids'))) {
analysis.ids.push(it.product_id);
}
// 分析用: CRITEO
if (_.isArray(_.get(analysis, 'criteo'))) {
analysis.criteo.push({
id: it.product_skn,
quantity: Number(it.buy_number),
price: it.last_vip_price
});
}
} else {
// 分析用: 商品ID列表
if (_.isArray(_.get(analysis, 'ids'))) {
analysis.ids.push(it.product_id);
}
// 分析用: CRITEO
if (_.isArray(_.get(analysis, 'criteo'))) {
analysis.criteo.push({
id: it.product_skn,
quantity: Number(it.buy_number),
price: it.last_vip_price
});
}
}
goods.tag = GOODS_TYPE_TAG_MAP[it.goods_type];
// 商品类型:预售或普通
goods.goodsType = isAdvanceCart ? 'advance' : 'ordinary';
// 上市期
if (isAdvanceCart && it.expect_arrival_time) {
goods.preSellDate = it.expect_arrival_time;
}
// 商品链接
let cnAlphaBet = it.cn_alphabet ? it.cn_alphabet : md5(it.product_name);
goods.link = helpers.urlFormat(`/product/pro_${it.product_id}_${it.goods_id}/${cnAlphaBet}.html`, null, 'item');
return goods;
});
};
/**
* 购物车统计
*/
const formatShoppingCartData = (sc) => {
return {
discountAmount: sc.discount_amount, // 活动价
fastShoppingCost: sc.fast_shopping_cost,
gainYohoCoin: sc.gain_yoho_coin, // 获赠有货币个数
goodsCount: sc.goods_count,
isMultiPackage: sc.is_multi_package,
lastOrderAmount: sc.last_order_amount, // 商品金额总计
orderAmount: sc.order_amount, // 商品总价
// package_list,[],
promotionFormula: sc.promotion_formula,
promotionFormulaList: _.map(sc.promotion_formula_list, it => {
return {
promotion: it.promotion,
promotionAmount: it.promotion_amount
};
}),
selectedGoodsCount: sc.selected_goods_count,
shippingCost: sc.shipping_cost,
strDiscountAmount: sc.str_discount_amount,
strOrderAmount: sc.str_order_amount
};
};
/**
* 全局优惠头部信息
*/
const formatPromotionInfos = (infoList) => {
return _.map(infoList, it => {
return {
cutdownAmount: it.cutdown_amount,
promotionId: it.promotion_id,
promotionTitle: it.promotion_title,
promotionType: it.promotion_type,
tag: PROMOTION_TYPE_TAG_MAP[it.promotion_type]
};
});
};
/**
* 过时商品
* offShelvesList 过时商品列表
*
*/
const formatOffShelves = (offShelvesList, isAdvanceCart, analysis) => {
return formatCartGoods(offShelvesList, isAdvanceCart, false, true, analysis);
};
/**
* 售罄商品
*/
const formatSoldOuts = (soldOutsList, isAdvanceCart, analysis) => {
return formatCartGoods(soldOutsList, isAdvanceCart, false, false, analysis);
};
/**
* 格式化单个加价购商品
*/
const formatPriceGiftOne = (it) => {
let gift = {
// isShowGift: isGift, // 控制是否显示赠品
// isFold: true, // 控制是否[展开]
promotionId: it.promotion_id,
promotionTitle: it.promotion_title.replace('¥', '¥'), // + '(注:您看到的商品可能因为下单时间差已售完)', // subjoinType
maxSelectNumber: it.max_select_number,
promotionType: it.promotion_type,
tag: PROMOTION_TYPE_TAG_MAP[it.promotion_type],
isGift: 'Gift' === it.promotion_type,
isPriceGift: 'Needpaygift' === it.promotion_type
};
gift.goodsList = _.map(it.goods_list, (g) => {
let goods = {
id: g.product_id,
skn: g.product_skn,
subjoinTitle: g.product_name,
imgCover: g.goods_images ? helpers.image(g.goods_images, 60, 60) : '',
subjoinPrice: transPrice(g.last_price),
marketPrice: gift.isGift ? '' : transPrice(g.market_price),
yohoIcon: 0,
isGift: gift.isGift,
isPriceGift: gift.isPriceGift
};
// 商品链接
if (g.goods_id) {
let cnAlphaBet = g.cn_alphabet ? g.cn_alphabet : md5(g.product_name);
goods.subjoinLink = helpers.urlFormat(`/product/pro_${g.product_id}_${g.goods_id}/${cnAlphaBet}.html`,
null, 'item');
} else {
let uri = `/product/show_${g.product_id}_${g.product_skn}/${makeToken(g.product_skn)}.html`;
goods.subjoinLink = helpers.urlFormat(uri, null, 'item');
}
return goods;
});
return gift;
};
/**
* 可选加价购
* isGift 是否是赠品:true-赠品
*/
const formatPriceGifts = (giftList) => {
return _.map(giftList, (it) => {
return formatPriceGiftOne(it);
});
};
/**
* 购物车赠品
*/
/* const formatGifts = (giftList) => {
return formatPriceGifts(giftList);
};*/
const checkGoodsIsSel = (goodsList, promotionId, selectedGiftsList) => {
return _.map(goodsList, goods => {
_.forEach((selectedGiftsList || []), gift => {
if (goods.id === gift.id && promotionId === gift.promotionId) {
goods.isCheckedGift = true;
goods.checkedGiftPid = gift.pid;
goods.checkedGiftSkn = gift.skn;
return;
}
});
return goods;
});
};
/**
* 格式化promotion 里的数据
*/
const formatPromotion = (it, selectedGiftsList) => {
let status = Number(it.status);
let info = {
status: status,
// "status": 0, // 状态 0 未满足 10 已满足 [20 售光 30 更换 ]
conditionUnit: it.condition_unit, // 0满足,1 件,2金额
conditionValue: it.condition_value,
giftPrice: it.gift_price, // 赠品或加价购商品价格
giftGoodsList: formatCartGoods(it.gift_goods_List), // 可供选择的赠品或加价购商品列表
promotionId: it.promotion_id, // 促销id
promotionTitle: it.promotion_title, // "已满足[满30减10-dev30]",
promotionType: it.promotion_type, // 促销类型
// list.yohobuy.com?pp_id=10408
promotionPageUrl: helpers.urlFormat('', { pp_id: it.promotion_id }, 'list'),
tag: PROMOTION_TYPE_TAG_MAP[it.promotion_type],
isGift: it.promotion_type === 'Gift',
isPriceGift: it.promotion_type === 'Needpaygift',
isNotReach: status === 0, // 未满足
isReach: status === 10 || status === 30, // 满足条件
isEmpty: status === 20, // 已售完
isSelected: status === 30 // 已选择,可更换
};
// 判断赠品/加价购商品,是否被选中
info.giftGoodsList = checkGoodsIsSel(info.giftGoodsList, info.promotionId, selectedGiftsList);
if (status === 0) {
let tipTxt = `差${ -1 * info.conditionValue}`;
if (info.conditionUnit === 1) {
tipTxt += '件';
} else if (info.conditionUnit === 2) {
tipTxt += '元';
}
info.promotionTitle = tipTxt + info.promotionTitle;
} else if (status === 10) {
info.promotionTitle = '已满足' + info.promotionTitle;
}
if (info.giftGoodsList) {
info.giftGoodsListStr = JSON.stringify(info.giftGoodsList);
}
return info;
};
/**
* 优惠池头部优惠信息列表
*/
const formatPoolPromotionInfos = (infoList, selectedGiftsList) => {
return _.map(infoList, it => {
return formatPromotion(it, selectedGiftsList);
});
};
/**
* 格式化子优惠池信息,返回商品列表,子优惠列表放在每个商品中
*/
const formatSubPromotionPools = (pools, selectedGiftsList) => {
let goodsListPool = _.map(pools, p => {
let pool = {
goodsList: formatCartGoods(p.goods_list),
promotionInfos: formatPoolPromotionInfos(p.promotion_list, selectedGiftsList)
};
let goodsList = pool.goodsList;
if (goodsList && goodsList.length) {
return _.map(goodsList, g => {
g.promotionInfos = pool.promotionInfos;
return g;
});
}
return [];
});
// [ [goods, goods], [goods, ..], [] ] => [goods, goods ]
return _.flatten(goodsListPool);
};
/**
* giftsList, priceGifts 已选择的赠品和加价购,判断可选的赠品和加价购是否被选中
* 优惠池
*/
const formatPromotionPools = (pools, selectedGiftsList) => {
return _.map(pools, p => {
let pool = {
poolTitle: p.pool_title,
poolType: p.pool_type,
goodsList: formatCartGoods(p.goods_list),
promotionInfos: formatPoolPromotionInfos(p.promotion_list, selectedGiftsList)
};
if (p.sub_pool) {
let goodsWithPromotion = formatSubPromotionPools(p.sub_pool, selectedGiftsList);
if (_.isArray(goodsWithPromotion) && goodsWithPromotion.length) {
pool.goodsList = goodsWithPromotion.concat(pool.goodsList);
}
}
return pool;
});
};
module.exports = {
transPrice,
getShoppingKeyByCookie,
formatCartGoods,
// formatGifts,
formatPriceGiftOne,
formatPriceGifts,
formatShoppingCartData,
formatPromotion,
formatPromotionPools,
formatPromotionInfos,
formatOffShelves,
formatSoldOuts
};