global.js
11.8 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
/**
* 商品促销 model
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2017/4/6
*/
'use strict';
const _ = require('lodash');
const cheerio = require('cheerio');
const helpers = global.yoho.helpers;
const homeService = require('./home-service');
const globalApi = require('./global-api');
const headerModel = require('../../../doraemon/models/header');
const searchHandler = require('./search-handler');
const pager = require(`${global.utils}/pager`).setPager;
const productProcess = require(`${global.utils}/product-process`);
const GLOBAL_LIST_URI = '/product/global/list';
const _handelGlobalPathNav = (data, channel, page) => {
let rootName = '首页';
if (channel && _.isString(channel)) {
rootName = `${channel.toUpperCase()}首页`;
}
let pathNav = [{
href: channel === 'boys' ? '/' : helpers.urlFormat(`/${channel}/`),
name: rootName,
pathTitle: rootName
}, {
href: helpers.urlFormat(GLOBAL_LIST_URI),
name: '全球购',
pathTitle: '全球购'
}];
if (data.sort) {
pathNav.push({
href: helpers.urlFormat(GLOBAL_LIST_URI, data.sort.params),
name: data.sort.name,
pathTitle: data.sort.name
});
}
if (data.brand) {
pathNav.push({
name: data.brand.name,
pathTitle: data.brand.name
});
page = 'global-brand';
}
return {
pathNav: pathNav,
listType: page
};
};
const _checkSortEqual = (rp, params) => {
let status = true;
_.forEach(rp, (value, key) => {
if (status && params[key] !== value) {
status = false;
}
});
return status;
};
const _handelGlobalSort = (origin, params, originParams) => {
let all = [{
name: '全部品类',
href: `${searchHandler.handleFilterUrl(params, {}, {msort: true, misort: true, sort: true})}`
}];
let list = [];
let selectSort = {};
_.forEach(origin, value => {
let equalCategory = _checkSortEqual(value.relation_parameter, originParams);
let category = {
categoryId: value.category_id,
name: value.category_name,
num: value.node_count,
childList: [
{
categoryId: value.category_id,
name: `全部${value.category_name}`,
num: value.node_count,
href: `${searchHandler.handleFilterUrl(params, value.relation_parameter)}`,
childActive: _checkSortEqual(value.relation_parameter, originParams)
}
]
};
if (equalCategory) {
category.active = true;
Object.assign(selectSort, {
name: value.category_name,
params: value.relation_parameter
});
}
if (value.sub && value.sub.length === 1) {
category.childList.length = 0;
}
_.forEach(value.sub, subValue => {
let child = {
categoryId: subValue.category_id,
name: subValue.category_name,
num: subValue.node_count,
href: `${searchHandler.handleFilterUrl(params, subValue.relation_parameter)}`,
childActive: _checkSortEqual(subValue.relation_parameter, originParams)
};
category.childList.push(child);
if (child.childActive) {
category.active = true;
Object.assign(selectSort, {
name: subValue.category_name,
params: subValue.relation_parameter
});
}
});
list.push(category);
});
return {
allSort: {
all: all,
list: list
},
selectSort: selectSort
};
};
const _getProductSeoData = (detail) => {
let resData = {};
if (!_.isEmpty(detail)) {
let gn = detail.product_name,
bn = _.get(detail, 'brand_info.brand_name') || detail.product_source;
resData = {
title: `${bn} | ${gn}正品 | YOHO!BUY 有货`,
keywords: `${bn},${bn}专卖店,${bn}官方授权店,${bn}正品,${bn}打折,${bn}折扣店,${bn}真品,${bn}代购`,
description: `YOHO!BUY 有货-${bn}官方授权店,${gn}图片、报价、介绍。YOHO!BUY 有货${bn}专卖店提供${bn}正品、${bn}真品、 ${bn}打折、${bn}代购等。` // eslint-disable-line
};
}
return resData;
};
const getGlobalProductListData = (params, yoho) => {
let limitNum = params.limit ? params.limit - 1 : 59;
let dps = {};
params.page = params.page || 1;
if (params.brand) {
dps.brand = params.brand;
}
return Promise.props({
header: headerModel.requestHeaderData(yoho.channel),
fullList: globalApi.getGlobalProductListAsync(Object.assign({
physical_channel: yoho.channelNum,
limit: 1
}, dps)),
list: globalApi.getGlobalProductListAsync(Object.assign({
physical_channel: yoho.channelNum
}, params, {limit: limitNum})),
brand: dps.brand ? globalApi.getGlobalBrandInfoAsync(dps.brand) : {}
}).then(result => {
let resData = {};
Object.assign(resData, result.header);
if (result.list.code === 200) {
let listData = _.get(result.list, 'data', {});
let totalNum = _.get(listData, 'total', 0);
let tip = {
start: (params.page - 1) * limitNum + 1,
total: listData.total || '',
end: _.min([listData.total, limitNum * params.page])
};
// opts 显示新品、折扣
listData.filter = listData.filter || {};
Object.assign(listData.filter, {
new: 'Y',
specialoffer: 'Y'
});
resData.list = {
leftContent: _handelGlobalSort(_.get(result.fullList, 'data.filter.group_sort', []), dps, params),
filters: searchHandler.handleFilterDataAll(listData, params),
opts: searchHandler.handleOptsData(params, totalNum, listData.filter),
totalCount: totalNum,
footPager: Object.assign({tip: tip}, pager(_.get(listData, 'page_total', 0), params)),
goods: productProcess.processProductList(_.get(listData, 'product_list', []),
Object.assign({showDiscount: false, isGlobal: true}, params)),
hasNextPage: searchHandler.handleNextPage(params, totalNum),
latestWalk: 6 // 最近浏览记录
};
if (resData.list.filters && resData.list.filters.brand) {
_.set(resData.list, 'filters.brand.showAllBrands', true); // 设置首次展示品牌筛选所有品牌
}
// pathNav brand
let pathInfo = {};
let fillCondition = _.get(resData, 'list.filters.checkedConditions.conditions');
if (dps.brand) {
let brandName = _.get(result.brand, 'data.brand_name', '');
if (brandName) {
pathInfo.brand = {
name: brandName
};
// 添加品信息for seo
fillCondition = _.concat([{
name: brandName,
itemType: 'brand'
}], fillCondition);
}
}
// psthNav sort
let selectedSort = _.get(resData.list, 'leftContent.selectSort', {});
if (!_.isEmpty(selectedSort)) {
pathInfo.sort = selectedSort;
}
Object.assign(resData.list, _handelGlobalPathNav(pathInfo, yoho.channel, 'global-list'));
let seo = searchHandler.getListSeo(yoho.channel,
_.get(resData, 'list.leftContent.allSort.list'),
fillCondition);
Object.assign(resData, seo);
}
return resData;
});
};
const getGlobalProductDetailData = (skn, channelNum, channel) => {
return Promise.props({
header: headerModel.requestHeaderData(channel),
detail: globalApi.getGlobalProductDetailAsync(skn, channelNum),
html: globalApi.getGlobalProductHtmlAsync(skn, channelNum)
}).then(result => {
let resData = {};
let detailInfo, html = '';
if (+result.detail.code === 200) {
detailInfo = _.get(result, 'detail.data', {});
let brandInfo = detailInfo.brand_info || {};
resData.banner = {
bgColor: '#000',
homeUrl: helpers.urlFormat(GLOBAL_LIST_URI, {brand: brandInfo.brand_id}),
brandName: brandInfo.brand_name,
logo: brandInfo.brand_ico
};
resData.pathNav = _.concat(
homeService.getHomeChannelNav(channel),
[
{name: '全球购', href: helpers.urlFormat(GLOBAL_LIST_URI)},
{name: detailInfo.product_name || ''}
]
);
if (detailInfo.orign_price - detailInfo.final_price === 0) {
_.unset(detailInfo, 'formart_orign_price');
} else {
detailInfo.promotion = ((detailInfo.final_price / detailInfo.orign_price) * 10).toFixed(1);
// 只显示大于1折小于9折的折扣
if (detailInfo.promotion <= 1.0 || detailInfo.promotion >= 9.0) {
detailInfo.promotion = false;
}
}
if (detailInfo.market_price - detailInfo.sales_price === 0) {
_.unset(detailInfo, 'format_market_price');
}
if (!_.isEmpty(detailInfo.goods_list)) {
let colors = [];
_.forEach(detailInfo.goods_list, (value, index) => {
let size = [];
_.forEach(value.size_list || [], subVal => {
size.push({
sku: subVal.product_sku,
name: subVal.size_name,
soldOut: subVal.storage_number * 1 <= 0
});
});
if (_.isEmpty(value.images_list)) {
value.images_list = [{
image_url: value.color_image
}];
}
if (index === 0 && !detailInfo.mainThumb) {
value.focus = true;
detailInfo.mainThumb = _.get(value, 'images_list[0].image_url', value.color_image);
}
colors.push({
src: value.color_image,
name: value.color_name,
title: value.color_name,
imgList: value.images_list,
focus: value.focus,
size: size
});
});
detailInfo.colors = colors;
_.set(detailInfo, 'bundle.count', 1); // 设置默认数量为1
}
}
if (result.html && !_.get(result.html, 'code', '')) {
let $ = cheerio.load(result.html);
$('script').remove();
html = $('body').html();
if (html) {
html = html.replace(/http:/ig, '');
html = html.replace(/\.jpg\"/ig, '.jpg?imageslim"');
html = html.replace(/\.png\"/ig, '.png?imageslim"');
}
}
Object.assign(resData, result.header, {
goodsInfo: detailInfo,
detailHtml: html || ''
}, _getProductSeoData(detailInfo));
return resData;
});
};
module.exports = {
getGlobalProductListData,
getGlobalProductDetailData
};