product-process.js
13.1 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
'use strict';
const _ = require('lodash');
const camelCase = global.yoho.camelCase;
const helpers = global.yoho.helpers;
const images = require(`${global.utils}/images`);
/**
* 根据性别来决定 默认图片获取字段 如果是 2、3
*
* 则优先从cover2 --》 cover1 -- 》 images_url
* 否则优先从cover1 --》 cover2 -- 》 images_url
*
*/
const procProductImg = (product, gender, newSort) => {
if (gender === '2,3') {
return product.cover2 || product.cover1 || product.imagesUrl || '';
}
if (newSort && gender === '1,2,3') {
return product.imagesUrl || '';
}
return product.cover1 || product.cover2 || product.imagesUrl || '';
};
/**
* 处理列表大图周边小图数据,拼接添加 href
* @param origin goods 原始数据
* @returns {{}} goods 输出数据,结构不变
*/
const handleGoodsListData = (origin) => {
_.forEach(origin, (value, key) => {
if (!_.isEmpty(value.goodsList)) {
_.forEach(value.goodsList, (subValue, subKey) => {
origin[key].goodsList[subKey].url =
helpers.urlFormat(`/product/pro_${value.productId}_${subValue.goodsId}/${value.cnAlphabet}.html`);
});
}
});
return origin;
};
/**
* 处理搜索返回同一skc分男女封分别返回问题
* @param array $images
* @return string 商品图片
*/
const procGoodsList = (goodsList) => {
let list = {};
if (!_.isEmpty(goodsList)) {
let goods = _.cloneDeep(goodsList);
_.forEach(goods, value => {
if (!list[value.productSkc]) {
list[value.productSkc] = value;
} else {
if (value.cover1) {
list[value.productSkc].cover1 = value.cover1;
}
if (value.cover2) {
list[value.productSkc].cover2 = value.cover2;
}
}
});
}
return _.toArray(list);
};
/**
* 处理
*
* 2、3: cover2 --> images_url -> cover1
* 1,3 :cover1 --> images_url -> cover2
* 其他: cover1 --> cover2 --> images_url
*
* @param array $images
* @return string 商品图片
*/
const procProductImgs = (item, gender) => {
let imgUrl = item.images_url ? item.images_url : '',
cover1 = item.cover_1 ? item.cover_1 : '',
cover2 = item.cover_2 ? item.cover_2 : '';
if (gender === '2,3') {
return cover2 ? cover2 : (imgUrl ? imgUrl : cover1);// eslint-disable-line
} else if (gender === '1,3') {
return cover1 ? cover1 : (imgUrl ? imgUrl : cover2);// eslint-disable-line
} else {
return cover1 ? cover1 : (cover2 ? cover2 : imgUrl);// eslint-disable-line
}
};
/**
* 商品搜索商品数据处理
*/
exports.processProductList = (list, options) => {
const pruductList = [];
options = Object.assign({
showTags: true,
showNew: true,
showSale: true,
showDiscount: true, // 显示折扣
newCoverSort: false, // 新封面排序
width: 290,
height: 388,
isApp: false,
showPoint: true,
gender: '2,3'
}, options);
list = camelCase(list);
_.forEach(list, (product) => {
// 商品信息有问题,则不显示
if (!product.productSkn || !product.goodsList.length) {
return;
}
// 如果库存为0,显示已抢完
if (product.storageNum === 0 && product.status === 1) {
product.isSaleOut = true;
}
// 市场价和售价一样,则不显示市场价, 不显示折扣信息
if (product.marketPrice === product.salesPrice) {
delete product.marketPrice;
} else if (options.showDiscount) {
product.discount = (product.salesPrice / product.marketPrice * 10).toFixed(1);
}
// 如果设置了默认图片,就取默认的图片
_.forEach(procGoodsList(product.goodsList), (goods) => {
// (颜色优先)
if (options.color) {
if (goods.colorId === options.color) {
product.defaultImages = procProductImg(goods, options.gender, options.newCoverSort);
return;
}
} else {
if (goods.isDefault === 'Y') {
product.defaultImages = procProductImg(goods, options.gender, options.newCoverSort);
return;
}
}
});
// 如果还未赋值,则取第一个skc产品的默认图片
if (!product.defaultImages) {
product.defaultImages = procProductImg(product.goodsList[0], options.gender, options.newCoverSort);
}
product = Object.assign(product, {
id: product.productSkn,
thumb: product.defaultImages
});
product.isSoonSoldOut = product.isSoonSoldOut === 'Y';
product.url = helpers.urlFormat(`/product/pro_${product.productId}_${product.goodsList[0].goodsId}/${product.cnAlphabet}.html`); // eslint-disable-line
// tar add 1606071146 品牌链接处理
product.brandUrl = helpers.urlFormat('', '', product.brandDomain);
// APP访问需要加附加的参数
// 备注:如果以后APP的接口太多,可以把这边参数提取出来,变成一个公共的方法来生成,便于以后管理维护
if (options.isApp) {
product.url += `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":'${product.productId}'}}`; // eslint-disable-line
}
if (options.showTags) {
product.tags = {};
product.tags.isNew = options.showNew && product.isNew === 'Y'; // 新品
product.tags.isDiscount = options.showSale && product.isDiscount === 'Y'; // 在售
product.tags.isLimited = product.isLimited === 'Y'; // 限量
product.tags.isYohood = product.isYohood === 'Y'; // YOHOOD
product.tags.midYear = product.midYear === 'Y'; // 年中
product.tags.yearEnd = product.yearEnd === 'Y'; // 年末
product.tags.isAdvance = product.isAdvance === 'Y'; // 再到着
// 打折与即将售完组合显示打折
if (product.isSoonSoldOut && product.tags.isDiscount) {
product.tags.isFew = false;
} else if (product.tags.isDiscount &&
(product.tags.isFew || product.tags.isLimited || product.tags.isYohood || product.tags.isAdvance)) {
// 打折与其它组合则隐藏打折
product.tags.isDiscount = false;
} else if (product.tags.isYohood && product.tags.isFew) {
// YOHOOD和新品组合显示YOHOOD
product.tags.isFew = false;
}
}
pruductList.push(product);
});
return handleGoodsListData(pruductList);
};
/**
* 处理筛选数据
* @param list
* @param string | options
* @return array 处理之后的筛选数据
*/
exports.processFilter = (list, options) => {
const filters = {
classify: []
};
const filtersType = {
brand: {
name: '所有品牌',
title: '品牌',
dataId: 'id',
subsName: 'brandName',
firstSub: 0,
dataType: 'brand'
},
color: {
name: '所有颜色',
title: '颜色',
dataId: 'colorId',
subsName: 'colorName',
firstSub: 0,
dataType: 'color'
},
discount: {
name: '所有商品',
title: '折扣',
dataId: 'key',
subsName: 'name',
firstSub: '0.1,0.9',
dataType: 'p_d'
},
gender: {
name: '所有性别',
title: '性别',
dataId: 'key',
subsName: 'flag',
firstSub: '1,2,3',
dataType: 'gender'
},
groupSort: {
name: '所有品类',
title: '品类',
dataId: 'relationParameter',
subsName: 'categoryName',
firstSub: 0,
dataType: 'sort'
},
priceRange: {
name: '所有价格',
title: '价格',
dataId: 'key',
subsName: 'flag',
firstSub: 0,
dataType: 'price'
},
size: {
name: '所有尺码',
title: '尺码',
dataId: 'sizeId',
subsName: 'sizeName',
firstSub: 0,
dataType: 'size'
}
};
options = Object.assign({
gender: '1,2,3', // 默认选择的性别,默认1,2,3表示所有
exclude: null // 需要排除的字段
}, options);
list = camelCase(list);
_.forEach(list, (item, key) => {
let classify = {
subs: []
};
if (key === 'group_sort') {
return;
}
if ((options.hideSize && key === 'size') || (options.hideSort && key === 'groupSort')) {
return;
}
classify.dataType = filtersType[key].dataType;
classify.name = filtersType[key].name;
classify.title = filtersType[key].title;
classify.subs.push({
chosed: true,
dataId: filtersType[key].firstSub,
name: filtersType[key].name
});
_.forEach(item, (sub, index) => {
let subs = {};
if (filtersType[key].dataId === 'key') {
subs.dataId = index;
} else if (filtersType[key].dataId === 'relationParameter') {
subs.dataId = sub.relationParameter['sort']; // eslint-disable-line
} else {
subs.dataId = sub[filtersType[key].dataId];
}
if (filtersType[key].subsName === 'flag') {
subs.name = sub;
} else {
subs.name = sub[filtersType[key].subsName];
if (key === 'discount') {
subs.name = subs.name + '折商品';
}
}
classify.subs.push(subs);
});
filters.classify.push(classify);
});
return filters;
};
// 处理单个商品
exports.processProduct = (productData, options) => {
let result = {},
flag = false;
options = Object.assign({
showTags: true,
showNew: true,
showSale: true,
width: 290,
height: 388,
showPoint: true
}, options);
// 商品信息有问题,则不显示
if (!productData.product_skn || !productData.goods_list[0]) {
return false;
}
// 市场价和售价一样,则不显示市场价
if (parseInt(productData.market_price, 0) === parseInt(productData.sales_price, 0)) {
productData.market_price = false;
}
// 设置默认图片
_.forEach(productData.goods_list, item => {
if (item.is_default === 'Y') {
productData.default_images = procProductImgs(item, options.gender);
flag = true;
}
});
if (!flag) {
productData.default_images = procProductImgs(productData.goods_list[0], options.gender);
}
result.id = productData.product_skn;
result.product_id = productData.product_id;
result.thumb = images.getImageUrl(productData.default_images, options.width, options.height);
result.name = productData.product_name;
result.price = !productData.market_price ? false : productData.market_price;
result.salePrice = productData.sales_price;
if (options.showPoint) {
result.price += '.00';
result.salePrice += '.00';
}
result.is_soon_sold_out = (productData.is_soon_sold_out === 'Y');
result.url = helpers.urlFormat(`/product/pro_${productData.product_id}_${productData.goods_list[0].goods_id}/${productData.cn_alphabet}.html`, null, 'list'); // eslint-disable-line
if (options.showTags) {
result.tags = {};
result.tags.is_new = options.showNew && productData.is_new && productData.is_new === 'Y'; // 新品
result.tags.is_discount = options.showSale && productData.is_discount && productData.is_discount === 'Y'; // 在售
result.tags.is_limited = productData.is_limited && productData.is_limited === 'Y'; // 限量
result.tags.is_yohood = productData.is_yohood && productData.is_yohood === 'Y'; // YOHOOD
result.tags.midYear = productData['mid-year'] && productData['mid-year'] === 'Y'; // 年中
result.tags.yearEnd = productData['year-end'] && productData['year-end'] === 'Y'; // 年末
result.tags.is_advance = productData.is_advance && productData.is_advance === 'Y'; // 再到着
if (result.is_soon_sold_out && result.tags.is_discount) {
result.tags.is_new = false;// 打折与即将售完组合显示打折
} else if (result.tags.is_discount &&
(result.tags.is_new || result.tags.is_limited || result.tags.is_yohood || result.tags.is_advance)) {
result.tags.is_discount = false;// 打折与其它组合则隐藏打折
} else if (result.tags.is_yohood && result.tags.is_new) {
result.tags.is_new = false;// YOHOOD和新品组合显示YOHOOD
}
}
return result;
};