product-process.js
11.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
/**
* 商品处理
* @author:
* @date:
*/
'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) => {
if (gender === '2,3') {
return product.cover2 || product.cover1 || 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;
};
/**
* 商品搜索商品数据处理
*/
exports.processProductList = (list, options) => {
const pruductList = [];
options = Object.assign({
showTags: true,
showNew: true,
showSale: true,
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 {
product.discount = (product.salesPrice / product.marketPrice * 10).toFixed(1);
}
// 判别默认的商品是否将默认的图片URL赋值到skn
let flag = false;
// 如果设置了默认图片,就取默认的图片
_.forEach(product.goodsList, (goods) => {
if (flag) {
return;
}
if (goods.isDefault === 'Y') {
product.defaultImages = procProductImg(goods);
flag = true;
}
});
// 如果还未赋值,则取第一个skc产品的默认图片
if (!flag) {
product.defaultImages = procProductImg(product.goodsList[0]);
}
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) {
let tags = {};
Object.assign(tags, {
isNew: options.showNew && product.isNew === 'Y', // 新品
isDiscount: options.showSale && product.isDiscount === 'Y', // 在售
isLimited: product.isLimited === 'Y', // 限量
isYohood: product.isYohood === 'Y', // YOHOOD
midYear: product.midYear === 'Y', // 年中
yearEnd: product.yearEnd === 'Y', // 年末
isAdvance: product.isAdvance === 'Y' // 再到着
});
// 打折与即将售完组合显示打折
if (product.isSoonSoldOut && tags.isDiscount) {
tags.isFew = false;
} else if (tags.isDiscount &&
(tags.isFew || tags.isLimited || tags.isYohood || tags.isAdvance)) {
// 打折与其它组合则隐藏打折
tags.isDiscount = false;
} else if (tags.isYohood && tags.isFew) {
// YOHOOD和新品组合显示YOHOOD
tags.isFew = false;
}
product.tags = tags;
}
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 = {};
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 = item.images_url;
}
});
if (!productData.default_images) {
productData.default_images = productData.goods_list[0].images_url;
}
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 = parseInt(result.price, 10).toFixed(2);
result.salePrice = parseInt(result.salePrice, 10).toFixed(2);
}
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) {
let tags = {};
Object.assign(tags, {
is_new: options.showNew && productData.is_new && productData.is_new === 'Y', // 新品
is_discount: options.showSale && productData.is_discount && productData.is_discount === 'Y', // 在售
is_limited: productData.is_limited && productData.is_limited === 'Y', // 限量
is_yohood: productData.is_yohood && productData.is_yohood === 'Y', // YOHOOD
midYear: productData['mid-year'] && productData['mid-year'] === 'Y', // 年中
yearEnd: productData['year-end'] && productData['year-end'] === 'Y', // 年末
is_advance: productData.is_advance && productData.is_advance === 'Y' // 再到着
});
if (result.is_soon_sold_out && tags.is_discount) {
tags.is_new = false;// 打折与即将售完组合显示打折
} else if (tags.is_discount &&
(tags.is_new || tags.is_limited || tags.is_yohood || tags.is_advance)) {
tags.is_discount = false;// 打折与其它组合则隐藏打折
} else if (tags.is_yohood && tags.is_new) {
tags.is_new = false;// YOHOOD和新品组合显示YOHOOD
}
result.tags = tags;
}
return result;
};