list-handler.js
14.9 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/**
* 列表页数据处理
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2017/11/24
*/
'use strict';
const _ = require('lodash');
const handleFilterUrl = require('../utils/parameter').fullParamToMinPath;
const brandIndex = [{
index: 'all',
name: '全部'
}, {
index: '0-9',
name: '0~9'
}];
// 品牌索引数据处理
for (let i = 65; i < 91; i++) {
brandIndex.push({
index: String.fromCharCode(i).toLowerCase(),
name: String.fromCharCode(i)
});
}
/**
* 处理页面左侧分类筛选数据 new
* @param origin 分类原始数据,一般是接口返回数据中的 group_sort 字段
* @param params 当前 URL 中已有的参数
* @param extra 可以任意传值用来处理特殊情况
* @param {string} [baseUrl] 需要跳转非当前页面传的相对路径(可不传,默认为空字符串)
* @returns {{}}
*/
const handleSortData = (origin, params, originParams, baseUrl) => {
let leftContent = {
sort: { misort: [] },
checked: []
};
let allCount = 0;
let all = [{
name: '全部品类',
num: allCount,
href: handleFilterUrl(baseUrl, params, {}, {msort: true, misort: true, sort: true, category_id: true})
}], list = [];
// 生成占位符的url,不用每次遍历分类都拼接url,?category_id=${category_id}&gender=1,3
const href = handleFilterUrl(baseUrl, params, {category_id: '${category_id}'}, {
msort: true,
misort: true,
sort: true
});
const cid = `${originParams.category_id}`;
_.each(origin, cate => {
let cateActive = cid === `${cate.category_id}`;
let category = {
categoryId: cate.category_id,
name: cate.category_name,
active: cateActive
};
let childList = [{
categoryId: cate.category_id,
name: `全部${cate.category_name}`,
href: href.replace('${category_id}', cate.category_id),
childActive: cateActive
}];
_.each(cate.sub, subCate => {
let subCateActive = cid === `${subCate.category_id}`;
if (subCateActive) {
category.active = true;
}
childList.push({
categoryId: subCate.category_id,
name: subCate.category_name,
href: href.replace('${category_id}', subCate.category_id),
childActive: cid === `${subCate.category_id}`
});
});
category.childList = childList;
list.push(category);
});
leftContent.allSort = {all, list};
return leftContent;
};
class FilterTools {
constructor(origin, params, baseUrl) {
this.origin = origin;
this.params = params;
this.baseUrl = baseUrl;
if (+params.page < 2) {
_.unset(params, 'page');
}
}
handleFilterCheckedConditions() {
let {baseUrl, origin, params} = this;
const conditions = [];
const customPrice = {
min: '',
max: ''
};
const filter = _.get(origin, 'filter', {});
_.each(params, (v, k) => {
const condition = {};
if (k === 'price') {
if (filter.priceRange && filter.priceRange[params.price]) {
condition.name = filter.priceRange[params.price].replace('¥', '¥');
} else {
const prices = _.split(params.price, ',');
if (prices.length === 1 || (prices[0] && !prices[1])) {
condition.name = `¥${prices[0] || prices[1]}以上`;
customPrice.min = prices[0] || prices[1];
} else {
condition.name = `¥${prices[0] || 0}-${prices[1]}`;
customPrice.min = prices[0] || '';
customPrice.max = prices[1];
}
}
} else if (k === 'color') {
const colorFind = _.find(filter.color, c => c.color_id === _.parseInt(params.color));
if (colorFind) {
condition.name = colorFind.color_name;
condition.color = colorFind.color_value ? 'url(' + colorFind.color_value.replace('http://', '//') + ')' : '#' + colorFind.color_code.replace('#', '');
}
} else if (k === 'size') {
const sizeFind = _.find(filter.size, c => c.size_id === _.parseInt(params.size));
if (sizeFind) {
condition.name = sizeFind.size_name;
}
} else if (k === 'age_level') {
if (filter.ageLevel) {
const ageFind = _.find(filter.ageLevel, c => +c.id === _.parseInt(params.age_level));
if (ageFind) {
condition.name = ageFind.name;
}
}
} else if (k === 'gender') {
const genderFind = _.find(this.handleFilterGender(), 'checked');
if (genderFind) {
condition.name = genderFind.name;
condition.value = '1,2,3';
}
} else if (k === 'brand') {
let brandFinds;
if (filter.paramBrand) {
brandFinds = filter.paramBrand;
} else {
const brands = _.split(params.brand, ',');
brandFinds = _.filter(filter.brand, c => _.some(brands, id => _.parseInt(id) === c.id));
}
if (brandFinds.length) {
condition.totalName = _.join(_.map(brandFinds, brand => brand.brand_name), '、');
condition.name = brandFinds.length > 1 ?
(`${brandFinds[0].brand_name}、${brandFinds[1].brand_name.substring(0, 3)}...`) :
condition.totalName;
}
} else if (k === 'style') {
const styleids = _.split(params.style, ',');
const styleFinds = _.filter(filter.style,
style => _.some(styleids, id => _.parseInt(id) === style.style_id));
if (styleFinds.length) {
condition.name = styleFinds.length >= 2 ?
(`${styleFinds[0].style_name}、${styleFinds[1].style_name}`) :
styleFinds[0].style_name;
}
}
if (condition.name) {
const clearParams = Object.assign({}, params, { [k]: condition.value || '' });
condition.itemType = k;
condition.href = handleFilterUrl(baseUrl, clearParams);
conditions.push(condition);
return;
}
if (k === 'standard') {
const standard = _.get(origin, 'standard', []);
const standardList = _.split(params.standard, ',');
_.each(standardList, standardstr => {
const standardids = _.split(standardstr, '_');
if (standardids.length === 2) {
const pStandar = _.find(standard, s => _.parseInt(s.standard_id) === _.parseInt(standardids[0])); // eslint-disable-line
if (pStandar) {
const cStandar = _.find(pStandar.sub,
s => _.parseInt(s.standard_id) === _.parseInt(standardids[1]));
if (cStandar) {
const clearParams = Object.assign({}, params, { [k]: '' });
conditions.push({
itemType: pStandar.standard_name,
href: handleFilterUrl(baseUrl, clearParams,
{standard: _.join(_.filter(standardList, ids => ids !== standardstr), ',')}), // eslint-disable-line
name: cStandar.standard_name
});
}
}
}
});
}
});
return {conditions, customPrice};
}
handleFilterGender() { // 组装gender筛选数据
let {baseUrl, params} = this;
const href = handleFilterUrl(baseUrl, params, {gender: '${gender}'});
// 伪静态化路由参数转化时将gender做了转化 '1,3'=>'1', '2,3'=>'2', 业务中直接生成路由需做调整
return [{
name: 'BOYS',
href: href.replace('${gender}', '1'),
checked: params.gender === '1,3'
}, {
name: 'GIRLS',
href: href.replace('${gender}', '2'),
checked: params.gender === '2,3'
}];
}
handleFilterBrand() { // 组装brand筛选数据
let {baseUrl, origin, params} = this;
const filter = _.get(origin, 'filter', {});
const brands = {};
if (!filter.brand || !filter.brand.length) {
return false;
}
const defaultBrands = [];
const brandsShow = [];
const href = handleFilterUrl(baseUrl, params, {brand: '${brand}'});
const brandids = _.split(params.brand, ',');
_.each(filter.brand, brand => {
let brandItem = {
name: brand.brand_name,
href: href.replace('${brand}', _.replace(brand.id, '-', '__')), // 替换全球购品牌id中-
checked: _.some(brandids, bid => _.parseInt(bid) === +brand.id),
id: brand.id
};
if (brand.brand_alif) {
if (!_.isNaN(parseInt(brand.brand_alif, 10))) {
brandItem.index = '0-9';
} else {
brandItem.index = brand.brand_alif.toLowerCase();
}
}
brandsShow.push(brandItem);
if (brandsShow.length <= 10) {
defaultBrands.push(brandItem);
}
});
if (brandsShow.length > 9) {
_.assign(brands, {
showMore: true,
showMulti: true,
brandIndex: brandIndex || []
});
}
return _.assign(brands, {
default: defaultBrands,
brandsShow: brandsShow,
seatUrl: handleFilterUrl(baseUrl, params, {brand: '{seat}'})
});
}
handleFilterSizes() { // 组装sizes筛选数据
let {baseUrl, origin, params} = this;
const filter = _.get(origin, 'filter', {});
const sizes = [];
const href = handleFilterUrl(baseUrl, params, {size: '${size}'});
if (!params.category_id) {
return sizes;
}
_.each(filter.size, size => {
sizes.push({
name: size.size_name,
href: href.replace('${size}', size.size_id),
checked: size.size_id === _.parseInt(params.size)
});
});
return sizes;
}
handleFilterColors() {
let {baseUrl, origin, params} = this;
const filter = _.get(origin, 'filter', {});
const colors = [];
const href = handleFilterUrl(baseUrl, params, {color: '${color}'});
_.each(filter.color, color => {
colors.push({
name: color.color_name,
href: href.replace('${color}', color.color_id),
checked: color.color_id === _.parseInt(params.color),
rgb: color.color_value ?
'url(' + color.color_value.replace('http://', '//') + ')' :
'#' + color.color_code.replace('#', '')
});
});
return colors;
}
handleFilterAgeLevels() { // 组装ageLevels筛选数据
let {baseUrl, origin, params} = this;
const filter = _.get(origin, 'filter', {});
const ageLevels = [];
const href = handleFilterUrl(baseUrl, params, {age_level: '${age_level}'});
_.each(filter.ageLevel, age => {
ageLevels.push({
name: age.name,
href: href.replace('${age_level}', age.id),
checked: _.parseInt(age.id) === _.parseInt(params.age_level)
});
});
return ageLevels;
}
handleFilterPrices() { // 组装prices筛选数据
let {baseUrl, origin, params} = this;
const filter = _.get(origin, 'filter', {});
let prices = [];
const href = handleFilterUrl(baseUrl, params, {price: '${price}'});
_.each(filter.priceRange, (name, price) => {
prices.push({
name: name,
href: href.replace('${price}', price),
checked: price === params.price,
order: _.parseInt(_.split(price, ',')[0])
});
});
prices = _.sortBy(prices, (item) => {
return item.order;
});
return prices;
}
handleFilterStyles() { // 组装style筛选数据
let {baseUrl, origin, params} = this;
const filter = _.get(origin, 'filter', {});
let styles = [];
const href = handleFilterUrl(baseUrl, params, {style: '${style}'});
const styleids = _.split(params.style, ',');
_.each(filter.style, style => {
styles.push({
name: style.style_name,
href: href.replace('${style}', style.style_id),
checked: _.some(styleids, id => _.parseInt(id) === style.style_id),
id: style.style_id
});
});
return styles;
}
handleFilterStandars() { // 组装standars筛选数据
let {baseUrl, origin, params} = this;
const standards = _.get(origin, 'standard', {});
const filterStandars = [];
const href = handleFilterUrl(baseUrl, params, {standard: '${standard}'});
let standarParam = {};
_.each(_.split(params.standard || '', ','), item => {
const sps = _.split(item, '_');
if (sps.length === 2) {
standarParam[sps[0]] = sps[1];
}
});
_.each(standards, standard => {
if (standard.sub.length > 1) {
filterStandars.push({
name: standard.standard_name,
sub: _.map(standard.sub, sub => {
const standarParamItem = Object.assign({}, standarParam, {
[standard.standard_id]: sub.standard_id
});
const standarParamStr = _.join(_.map(standarParamItem, (v, k) => {
return `${k}_${v}`;
}), ',');
return {
name: sub.standard_name,
href: href.replace('${standard}', standarParamStr),
checked: _.parseInt(sub.standard_id) === _.parseInt(standarParam[standard.standard_id]),
id: sub.standard_id
};
})
});
}
});
return filterStandars;
}
}
/**
* 处理筛选数据
* @param origin 要处理的筛选数据 filter
* @param params 当前 URL 中已有的参数,处理选中状态使用
* @returns {{}}
*/
const handleFilterData = (origin, params, baseUrl) => {
// 清除所有选中数据
// 某些特殊带频道信息页面,清除性别,需将gender设为1,2,3 (2017-3 配合SEO进行URL改造)
let remainParams = {
gender: '1,2,3'
};
const tools = new FilterTools(origin, params, baseUrl);
params.id && (remainParams.id = params.id);
params.union_type && (remainParams.union_type = params.union_type);
const clearUrl = handleFilterUrl(baseUrl, remainParams);
const {conditions, customPrice} = tools.handleFilterCheckedConditions();
const size = tools.handleFilterSizes();
const gender = tools.handleFilterGender();
const brand = tools.handleFilterBrand();
const color = tools.handleFilterColors();
const style = tools.handleFilterStyles();
const standard = tools.handleFilterStandars();
const checkedConditions = {
clearUrl,
conditions
};
let seniorChose = [];
let price = [];
let priceSeatUrl;
if (origin.total >= 10) {
price = tools.handleFilterPrices(origin, params);
priceSeatUrl = handleFilterUrl(baseUrl, params, {price: '{seat}'}); // 带[占位符]的前端跳转url
}
let ageLevel = tools.handleFilterAgeLevels(origin, params);
if (ageLevel.length <= 1) {
ageLevel = [];
}
if (style.length > 1) {
seniorChose.push({
name: '风格',
showMulti: true,
follow: true,
sub: style,
attr: 'style',
seatUrl: handleFilterUrl(baseUrl, params, {style: '{seat}'}) // 带[占位符]的前端跳转url
});
seniorChose = seniorChose.concat(standard);
}
return {
checkedConditions,
size,
gender,
brand,
color,
ageLevel,
customPrice,
price,
priceSeatUrl,
seniorChose
};
};
module.exports = {
handleSortData,
handleFilterData
};