list-handler.js
30.6 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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
/**
* 列表页数据处理
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2017/11/24
*/
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const indexUrl = {
boys: helpers.urlFormat('/'),
girls: helpers.urlFormat('/woman'),
kids: helpers.urlFormat('/kids'),
lifestyle: helpers.urlFormat('/lifestyle')
};
const handleFilterUrl = require(`${global.utils}/parameter`).fullParamToMinPath;
const seoHandler = require('./seo-handler');
// 页面商品数量
const pageSizeType = [40, 100, 200];
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)
});
}
/**
* 根据页面设置面包屑导航
* @type {[type]}
*/
const handlePathNavData = (data, params, page, channel, baseUrl) => {
let rootName = '首页';
if (channel && _.isString(channel)) {
rootName = channel.toUpperCase() + rootName;
}
let pathNav = [{
href: indexUrl[channel],
name: rootName, // '首页',
pathTitle: rootName // '首页'
}];
switch (page) {
case 'search': // 搜索结果提示
if (params.query || params.keywords) {
params.query = params.query || params.keywords;
pathNav.push({
name: '“<span id="nav_keyword">' +
params.query + '</span>” 共<span id="nav_keyword_count">' +
data.total + '</span>个结果'
});
} else {
pathNav.push({
name: '所有商品'
});
}
break;
case 'brand': // 品牌
pathNav.push({
name: '品牌一览',
pathTitle: '品牌一览',
href: helpers.urlFormat('/brands')
}, {
name: data.brandNameEn + data.brandNameCn,
pathTitle: data.brandNameEn + data.brandNameCn
});
break;
case 'shop': // 店铺
pathNav.push({
name: data.brandName,
pathTitle: data.brandName
});
break;
case 'new': // 新品到着
pathNav.push({
name: '新品到着',
pathTitle: '新品到着'
});
break;
default: // eslint-disable-line
let navList = [];
const cateUrl = handleFilterUrl(baseUrl, {category_id: '{categoryId}'});
_.forEach(data, sort => {
let sortName = sort.category_name,
sortCategoryId = `${sort.category_id}`;
if (`${params.category_id}` === sortCategoryId) {
navList = [{
name: sortName,
href: cateUrl.replace('{categoryId}', sortCategoryId),
pathTitle: sortName
}];
return false;
} else if (!_.isEmpty(sort.sub)) {
_.each(sort.sub, misort => {
let misortName = misort.category_name;
if (`${params.category_id}` === `${misort.category_id}`) {
navList = [{
name: sortName,
href: cateUrl.replace('{categoryId}', sortCategoryId),
pathTitle: sortName
}, {
name: misortName,
href: cateUrl.replace('{categoryId}', misort.category_id),
pathTitle: misortName
}];
return false;
}
});
if (navList.length) {
return false;
}
}
});
if (navList.length) {
pathNav = _.concat(pathNav, navList);
}
break;
}
return {
pathNav: pathNav,
listType: page
};
};
/**
* 设置列表页相关页pathNav
* @type {[type]}
*/
const handleListPathNavData = (sorts, checked, params, channel, baseUrl) => {
let rootName = '首页';
channel = channel || 'boys';
if (channel && _.isString(channel)) {
rootName = channel.toUpperCase() + rootName;
}
let pathNav = [{
href: indexUrl[channel],
name: rootName, // '首页',
pathTitle: rootName, // '首页'
type: 'home'
}];
const kd = seoHandler.getSeoCheckedParamsName(sorts, checked);
// 无筛选参数时
if (_.isEmpty(kd)) {
return {pathNav};
}
const qs = {};
if (kd.sort) {
if (params.category_id) {
qs.category_id = params.category_id;
} else {
params.sort && (qs.sort = params.sort);
}
pathNav.push({
href: handleFilterUrl(baseUrl, qs),
name: kd.sort,
pathTitle: kd.sort,
type: 'sort'
});
}
if (params.brand && _.indexOf(params.brand, ',') < 0 && kd.brand) {
qs.brand = params.brand;
pathNav.push({
href: handleFilterUrl(baseUrl, qs),
name: kd.brand,
pathTitle: kd.brand,
type: 'brand'
});
}
pathNav.push({
name: `${kd.brand || ''}${kd.channel || ''}${kd.color || ''}${kd.size || ''}${kd.sort || ''}`,
useH1: true
});
return {pathNav};
};
/**
* 处理页面左侧分类筛选数据 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 = (params.style || '') + ',';
const styleFinds = _.filter(filter.style,
style => styleids.includes(`${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,
multiSeatUrl: handleFilterUrl(baseUrl, params, {brand: '{seat}'}),
seatUrl: href.replace('${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 = (params.style || '') + ',';
_.each(filter.style, style => {
styles.push({
name: style.style_name,
href: href.replace('${style}', style.style_id),
checked: styleids.includes(`${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
};
};
/**
* 处理 sort 排序数据
* @param params
* @param extra 什么都可以传进来,多个参数传Object
* @returns {{}}
*/
const handleOptSortData = (params, filter, baseUrl) => {
const href = handleFilterUrl(baseUrl, params, {order: '${order}'}, {page: true});
const sortType = [{
href: href.replace('${order}', ''),
name: '默认',
active: !params.order || params.order === 's_n_desc',
hasSortOrient: false
}, {
href: href.replace('${order}', 'h_v_desc'),
name: '人气',
active: params.order === 'h_v_desc',
hasSortOrient: false
}, {
href: params.order === 's_t_desc' ?
href.replace('${order}', 's_t_asc') :
href.replace('${order}', 's_t_desc'),
name: '新品',
active: params.order === 's_t_desc' || params.order === 's_t_asc',
hasSortOrient: true,
desc: params.order !== 's_t_desc'
}, {
href: params.order === 's_p_desc' ?
href.replace('${order}', 's_p_asc') :
href.replace('${order}', 's_p_desc'),
name: '价格',
active: params.order === 's_p_desc' || params.order === 's_p_asc',
hasSortOrient: true,
desc: params.order !== 's_p_desc'
}, {
href: params.order === 'p_d_desc' ?
href.replace('${order}', 'p_d_asc') :
href.replace('${order}', 'p_d_desc'),
name: '折扣',
active: params.order === 'p_d_desc' || params.order === 'p_d_asc',
hasSortOrient: true,
desc: params.order !== 'p_d_desc'
}];
const checks = [];
if (filter) {
if (filter.new || params.new === 'Y') {
checks.push({
name: '新品',
checked: params.new === 'Y',
href: handleFilterUrl(baseUrl, params, {new: 'Y'}, params.new === 'Y' ? {new: true} : {})
});
}
if (filter.specialoffer || params.specialoffer === 'Y') {
checks.push({
name: '打折',
checked: params.specialoffer === 'Y',
href: handleFilterUrl(baseUrl, params, {specialoffer: 'Y'}, params.specialoffer === 'Y' ? {specialoffer: true} : {}) // eslint-disable-line
});
}
if (filter.limited || params.limited === 'Y') {
checks.push({
name: '限量',
checked: params.limited === 'Y',
href: handleFilterUrl(baseUrl, params, {limited: 'Y'}, params.limited === 'Y' ? {limited: true} : {})
});
}
}
return {sortType, checks};
};
/**
* 处理 page 数据
* @param params
* @param extra 什么都可以传进来,多个参数传Object
* @returns {{}}
*/
const handlePageData = (params, total, baseUrl) => {
// 上下翻页数据处理
const pageSizeUrl = handleFilterUrl(baseUrl, params, {limit: '${limit}'}, {page: true});
const pageCounts = _.map(pageSizeType, size => {
return {
href: pageSizeUrl.replace('${limit}', size),
count: size
};
});
const curPage = _.parseInt(params.page || 1); // 当前页码数
const countPerPage = _.parseInt(params.limit || pageSizeType[0]) - 1; // 每页商品数量
const pageCount = _.ceil(total / countPerPage); // 全部页码数量
let preHref;
let nextHref;
const pageUrl = handleFilterUrl(baseUrl, params, {page: '${page}'});
if (curPage > 1) {
preHref = pageUrl.replace('${page}', curPage - 1);
}
if (curPage < pageCount) {
nextHref = pageUrl.replace('${page}', curPage + 1);
}
return {
pageCounts,
curPage,
countPerPage: countPerPage + 1,
pageCount,
preHref,
nextHref
};
};
/**
* 处理 opts 排序数据 new
* @param params
* @param extra 什么都可以传进来,多个参数传Object
* @returns {{}}
*/
const handleOptsData = (params, total, filter, baseUrl) => {
return Object.assign(
handleOptSortData(params, filter, baseUrl),
handlePageData(params, total, baseUrl));
};
/**
* 处理 下一页链接
* @param params
* @param extra 什么都可以传进来,多个参数传Object
* @returns {{}}
*/
const handleNextPage = (params, total, baseUrl) => {
let currentPage = parseInt((params.page ? params.page : 1), 10); // 当前页
let perPageCount = parseInt((params.limit ? params.limit : pageSizeType[0]) - 1, 10); // 每页商品数
let totalPage = _.ceil(total / perPageCount); // 总页数
if (currentPage >= totalPage) {
return null;
}
return {
href: handleFilterUrl(baseUrl, params, {page: currentPage + 1}),
src: '//img10.static.yhbimg.com/product/2014/01/15/11/01fa01614784f6239760f1b749663016f1.jpg?' +
'imageMogr2/thumbnail/235x314/extent/235x314/background/d2hpdGU=/position/center/quality/90'
};
};
/**
* 分页
* @param {[type]} total 总页数
* @param {[type]} params 筛选条件
* @param {[noNextBtn]} 列表是否有下一页的按钮,如果有则实际查询数比传递参数的少一个,入60 => 59
* @return {[type]} [description]
*/
const handlePagerData = (total, params, noNextBtn, baseUrl) => {
const curPage = _.parseInt(params.page || 1);
const pageSize = _.parseInt(params.limit || pageSizeType[0]) + (noNextBtn ? 0 : -1);
const totalPage = _.ceil(total / pageSize);
const tip = {
start: (curPage - 1) * pageSize + 1,
end: curPage * pageSize > total ? total : curPage * pageSize,
total
};
let prePage, nextPage;
const pageUrl = handleFilterUrl(baseUrl, params, {page: '${page}'});
if (curPage > 1) {
prePage = {
url: pageUrl.replace('${page}', curPage - 1)
};
}
if (curPage < totalPage) {
nextPage = {
url: pageUrl.replace('${page}', curPage + 1)
};
}
let pages = [];
let pagesNums = totalPage > 5 ? 5 : totalPage;
let offsetPage = curPage + _.round(0 - pagesNums / 2); // 遍历页码左侧数字
if (offsetPage <= 0) {
offsetPage = 1;
} else if (offsetPage + pagesNums > totalPage) {
offsetPage = totalPage - pagesNums + 1;
}
const preLimit = offsetPage - 1; // 页码1和遍历页码左侧的距离
const nextLimit = totalPage - (offsetPage - 1 + pagesNums); // 总页码和遍历页码右侧的距离
if (preLimit === 1) { // 如果左侧间距为1则显示这个页码
pagesNums++;
offsetPage--;
}
if (nextLimit === 1) { // 如果右侧间距为1则显示这个页码
pagesNums++;
}
if (preLimit >= 2) {
pages = _.concat(pages, {
num: 1,
url: pageUrl.replace('${page}', 1),
}, {
num: '...'
});
}
if (offsetPage <= 0) {
offsetPage = 1;
} else if (offsetPage + pagesNums > totalPage) {
offsetPage = totalPage - pagesNums + 1;
}
for (let i = 0; i < pagesNums; i++) {
pages.push({
num: offsetPage,
url: pageUrl.replace('${page}', offsetPage),
cur: curPage === offsetPage
});
offsetPage++;
}
if (nextLimit >= 2) {
pages = _.concat(pages, {
num: '...'
}, {
num: totalPage,
url: pageUrl.replace('${page}', totalPage),
});
}
return {tip, prePage, nextPage, pages};
};
/**
* 一周新品上架
* @type {[type]}
*/
const handleWeekNew = (data, params, baseUrl) => {
let list = [];
let dest = {
newSales: {
name: '',
list: []
}
};
const href = handleFilterUrl(baseUrl, params, {shelveTime: '{shelveTime}'});
_.forEach(data.recent, function(value, key) {
let t = _.split(key, '-'),
dayStart = new Date(t[0], _.parseInt(t[1]) - 1, t[2]).getTime() / 1000,
day = `${dayStart},${dayStart + 86400}`;
list.push({
name: `${t[1] || ''}月${t[2] || ''}日`,
href: href.replace('{shelveTime}', day),
active: day === params.shelveTime,
sort: +t.join('')
});
});
_.set(dest, 'newSales.list', _.sortBy(list, function(o) {
return -o.sort;
}));
return dest;
};
module.exports = {
handlePathNavData,
handleListPathNavData,
handleSortData,
handleFilterData,
handleOptsData,
handleNextPage,
handlePagerData,
handleWeekNew
};