product-process-simple.js
5.96 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
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const logger = global.yoho.logger;
/**
* 商品搜索商品数据处理
*/
exports.processProductList = (list, options) => {
const pruductList = [];
options = Object.assign({
showTags: true,
showNew: true,
showSale: true,
showFew: true,
showLimit: true,
showDiscount: true, // 显示折扣
newCoverSort: false, // 新封面排序
width: 290,
height: 388,
isApp: false,
showPoint: true,
gender: '2,3',
from: {} // 来源
}, options);
_.forEach(list, (product) => {
// 全球购接口与普通接口返回商品差异属性预处理
if (options.isGlobal && product) {
Object.assign(product, {
goods_list: [{
images_url: product.default_images,
status: 1
}],
sales_price: product.final_price || product.orign_price,
market_price: null,
tbl_country_name: product.country_name,
tbl_brand_id: product.brand_id
});
}
// 商品信息有问题,则不显示
if (!product || !product.product_skn || !_.get(product, 'goods_list.length', 0)) {
return;
}
let proInfo = {
skn: product.product_skn,
product_name: product.product_name,
market_price: product.market_price,
sales_price: product.sales_price,
is_few: product.is_soon_sold_out === 'Y'
};
// 市场价和售价一样,则不显示市场价, 不显示折扣信息
if (proInfo.market_price <= proInfo.sales_price) {
delete proInfo.market_price;
} else if (options.showDiscount) {
proInfo.discount = (proInfo.sales_price / proInfo.market_price * 10).toFixed(1);
}
// 商品链接
if (product.is_global === 'Y') {
proInfo.url = helpers.urlFormat(`/product/global/${product.product_skn}.html`, null);
} else if (product.product_skn) {
proInfo.url = helpers.getUrlBySkc(product.product_skn);
}
// 店铺链接
if (product.is_global === 'Y' && product.tbl_brand_id) {
proInfo.brandUrl = helpers.urlFormat('/product/global/list', {brand: product.tbl_brand_id});
} else if (product.shop_id * 1) {
Object.assign(proInfo, {
brand_name: product.shop_name,
brandUrl: helpers.urlFormat('', {shopId: product.shop_id},
product.shop_domain || 'default-domain')
});
}
let defaultColorImg,
goodsList = [];
// 处理商品颜色封面
_.forEach(_.orderBy(product.goods_list, ['is_default'], ['desc']), goods => {
if (goods.is_default === 'Y' && !defaultColorImg) {
defaultColorImg = goods.images_url; // 颜色默认封面
}
if (+goods.status) {
goodsList.push({
images_url: goods.images_url,
color_name: goods.color_name,
url: proInfo.url,
status: 1
});
}
});
Object.assign(proInfo, {
goods_list: goodsList,
thumb: product.default_images || defaultColorImg
});
// 处理标签
if (options.showTags) {
let tags = [],
isfew = false;
if (product.is_global === 'Y') {
tags.push({
is_global: true,
plane: product.tbl_plane === 'Y',
name: product.tbl_country_name
});
}
_.get(product, 'tags', []).forEach((value) => {
let tag = {};
switch (value) {
case 'is_soon_sold_out': // 即将售磬
options.showFew && (tag.is_few = true, isfew = true);
break;
case 'is_solded': // 已售磬
product.is_solded = true;
break;
case 'is_new': // 新品NEW
options.showNew && (tag.is_new = true);
break;
case 'is_discount': // SALE
options.showSale && (tag.is_sale = true);
break;
case 'is_limited': // 限量
options.showLimit && (tag.is_limit = true);
break;
case 'is_yohood': // YOHOOD
tag.is_new_festival = true;
break;
case 'is_advance': // 再到着
tag.is_re_new = true;
break;
case 'midYear':// 年中热促
tag.is_year_mid_promotion = true;
break;
case 'yearEnd':// 年终大促
tag.is_year_end_promotion = true;
break;
case 'is_presell':// 预售
tag.is_presell = true;
break;
default:
break;
}
tags.push(tag);
});
proInfo.tags = tags;
isfew ? proInfo.is_few = isfew : delete proInfo.is_few;
}
if (options.query && _.isString(proInfo.product_name)) {
try {
let qreg = new RegExp(options.query.replace('\\', '\\\\'), 'ig');
proInfo.product_name = proInfo.product_name.replace(qreg, '<span style="color:#c00;">$&</span>');
} catch (e) {
logger.debug(`product_name replace query fail:${e.toString()}`);
}
}
pruductList.push(proInfo);
});
return pruductList;
};