search-handler.js
32.5 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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
/*
* @Author: sefon
* @Date: 2016-06-02 15:50:47
* @Last Modified by: Targaryen
* @Last Modified time: 2016-06-22 18:36:26
*/
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const queryString = require('querystring');
// 打折、新品、限量
const checksName = {
new: '新品',
specialoffer: '打折',
limited: '限量'
};
/**
* 处理用于筛选的 URL , 拼接 URL 参数
* @param originParam 当前 URL 中的参数
* @param newParam 要拼接的 参数
* @returns {string}
*/
const handleFilterUrl = (originParam, newParam, delParam) => {
let dest = '?';
let tempOriginParam = {};
delParam = delParam || {};
tempOriginParam = Object.assign(tempOriginParam, originParam, newParam);
delete tempOriginParam.uid;
_.forEach(tempOriginParam, function(value, key) {
if (!delParam[key]) {
dest += `${key}=${value}&`;
}
});
return _.trim(dest, '&');
};
/**
* 处理选中数据
* @param {[type]} params [description]
* @param {[type]} origin [description]
* @param {[type]} param [description]
* @return {[type]} [description]
*/
const handleCheckedData = (params, origin, param) => {
let dest = [];
if (!_.isEmpty(origin)) {
_.forEach(origin, value => {
if (value.checked) {
let tempPatam = _.cloneDeep(params);
// 删除选中
delete tempPatam[param];
dest.push({
name: value.name,
href: handleFilterUrl(tempPatam)
});
}
});
}
return dest;
};
/**
* 处理品牌筛选数据
* @param params
* @param origin
* @returns {{}}
*/
const handleBrandCheckedData = (params, origin) => {
let dest = [];
// 分拆品牌参数
let brands = _.split(params.brand, ',');
let intBrands = [];
_.forEach(brands, value => {
intBrands.push(parseInt(value, 10));
});
let checkedCount = 0;
// 遍历品牌数据,如果在参数中,那么加 checked,将此数据加入到 checked 数组中
if (!_.isEmpty(origin)) {
_.forEach(origin, (value) => {
if (typeof _.find(intBrands, o => {
return _.isEqual(o, value.id);
}) !== 'undefined') {
let checked = {
name: value.name
}; // push 到已选择数组
// 为了不污染原始数据
let tempIntBrands = _.cloneDeep(intBrands);
// 去掉当前选项
_.remove(tempIntBrands, n => {
return n === value.id;
});
// 拼接参数
let brand = '';
// 建议保留,需要品牌可以单个删除时放开注释即可
// _.forEach(tempIntBrands, subValue => {
// brand += subValue + ',';
// });
// 清除参数,保留没有去掉的参数
let tempParam = {
brand: brand
};
checked.href = handleFilterUrl(params, tempParam);
if (checkedCount === 0) {
dest[0] = checked;
} else if (checkedCount === 1) {
dest[0].name += '、' + checked.name.substring(0, 3) + '...';
}
checkedCount++;
// 建议保留,需要品牌可以单个删除时放开注释即可
// dest.push(checked);
}
});
}
return dest;
};
/**
* 新品、折扣、限量
*/
const hadleChecks = (name, params) => {
let tempParam = _.cloneDeep(params);
tempParam[name] = 'Y';
let checksOpts = {
name: checksName[name],
checked: false,
href: handleFilterUrl(tempParam)
};
if (params[name] && params[name] === 'Y') {
checksOpts.checked = true;
delete tempParam[name];
checksOpts.href = handleFilterUrl(tempParam);
}
return checksOpts;
};
/**
* 获取standard
*/
const getStandard = (qStandard, addOne, delOne) => {
let s = [];
qStandard = Object.assign({}, qStandard);
if (addOne && addOne.k) {
qStandard[addOne.k] = addOne.v;
}
if (delOne && delOne.k && qStandard[delOne.k] && qStandard[delOne.k] === delOne.v) {
qStandard;
}
_.each(qStandard, (val, key) => {
if (!delOne || !delOne.k || delOne.v !== val) {
s.push([key, val].join('_'));
}
});
return s.join(',');
};
/**
* 处理 opts 排序数据
* @param params
* @param total
* @param extra 什么都可以传进来,多个参数传Object
* @returns {{}}
*/
exports.handleOptsData = (params, total, extra) => {
let dest = {
sortType: [],
checks: []
};
// 用来标记是否是折扣专场,折扣专场只需要前三个排序参数
let count = (extra === 'discont') ? 3 : 4;
for (let i = 0; i < count; i++) {
let opt = {};
switch (i) {
case 0:
opt.href = handleFilterUrl(params, {order: ''});
if (extra === 'discont') { // 如果是折扣专场
opt.name = '全部';
if (_.isEmpty(params.order) || params.order === 's_t_desc') {
opt.active = true;
} else {
opt.active = false;
}
} else {
opt.name = '默认';
if (_.isEmpty(params.order) || params.order === 's_n_desc') {
opt.active = true;
} else {
opt.active = false;
}
}
break;
case 1:
if (params.order !== 's_t_desc' && params.order !== 's_t_asc') {
opt.href = handleFilterUrl(params, {order: 's_t_desc'});
opt.hasSortOrient = true;
} else {
opt.hasSortOrient = true;
opt.active = true;
if (params.order === 's_t_desc') {
opt.href = handleFilterUrl(params, {order: 's_t_asc'});
opt.desc = false;
} else {
opt.href = handleFilterUrl(params, {order: 's_t_desc'});
opt.desc = true;
}
}
opt.name = '最新';
break;
case 2:
if (params.order !== 's_p_desc' && params.order !== 's_p_asc') {
opt.href = handleFilterUrl(params, {order: 's_p_desc'});
opt.hasSortOrient = true;
} else {
opt.hasSortOrient = true;
opt.active = true;
if (params.order === 's_p_desc') {
opt.href = handleFilterUrl(params, {order: 's_p_asc'});
opt.desc = false;
} else {
opt.href = handleFilterUrl(params, {order: 's_p_desc'});
opt.desc = true;
}
}
opt.name = '价格';
break;
case 3:
if (params.order !== 'p_d_desc' && params.order !== 'p_d_asc') {
opt.href = handleFilterUrl(params, {order: 'p_d_desc'});
opt.hasSortOrient = true;
} else {
opt.hasSortOrient = true;
opt.active = true;
if (params.order === 'p_d_desc') {
opt.href = handleFilterUrl(params, {order: 'p_d_asc'});
opt.desc = false;
} else {
opt.href = handleFilterUrl(params, {order: 'p_d_desc'});
opt.desc = true;
}
}
opt.name = '折扣';
break;
default:
break;
}
dest.sortType.push(opt);
}
// 上下翻页数据处理
dest.pageCounts = [{
href: handleFilterUrl(params, {limit: 60}),
count: 60
}, {
href: handleFilterUrl(params, {limit: 100}),
count: 100
}, {
href: handleFilterUrl(params, {limit: 200}),
count: 200
}];
dest.curPage = _.isEmpty(params.page) ? 1 : params.page; // 当前页码数
// 每页商品数量
dest.countPerPage = _.isEmpty(params.limit) ? 60 : params.limit;
// 全部页码数量
dest.pageCount = parseInt(total / (_.isEmpty(params.limit) ? 60 : params.limit), 10) + 1;
// 每页多少商品
let paramsLimit = parseInt((_.isEmpty(params.limit) ? 60 : params.limit), 10);
// 上一页下一页
let preHref = (!_.isEmpty(params.page) && parseInt(params.page, 10) > 1) ?
parseInt(params.page, 10) - 1 : 1;
let nextHref = (!_.isEmpty(params.page)) ? parseInt(params.page, 10) + 1 : 2;
params.page = _.isEmpty(params.page) ? 1 : params.page;
if (dest.pageCount > 1 && (parseInt(params.page, 10) !== 1) &&
(parseInt(params.page, 10) !== dest.pageCount)) {
dest.preHref = handleFilterUrl(params, {page: preHref});
dest.nextHref = handleFilterUrl(params, {page: nextHref});
} else if (dest.pageCount > 1 && (parseInt(params.page, 10) === 1 || _.isEmpty(params.page))) {
dest.nextHref = handleFilterUrl(params, {page: nextHref});
} else if (dest.pageCount > 1 && (parseInt(params.page, 10) === dest.pageCount)) {
dest.preHref = handleFilterUrl(params, {page: preHref});
}
// 全部商品数
dest.totalCount = total;
// 商品开始结束数
dest.start = (!_.isEmpty(params.page) ? (params.page - 1) : 0) * paramsLimit + 1;
if (total < paramsLimit) {
dest.end = total;
} else {
dest.end = (dest.pageCount === parseInt(params.page, 10)) ?
total : parseInt(dest.start, 10) + paramsLimit - 1;
}
if (extra) {
// 新品
if (extra.new === 'Y') {
dest.checks.push(hadleChecks('new', params));
}
// 打折
if (extra.specialoffer === 'Y') {
dest.checks.push(hadleChecks('specialoffer', params));
}
// 限量
if (extra.limited === 'Y') {
dest.checks.push(hadleChecks('limited', params));
}
}
return dest;
};
/**
* 处理页面左侧分类筛选数据
* @param origin 分类原始数据,一般是接口返回数据中的 group_sort 字段
* @param params 当前 URL 中已有的参数
* @param extra 可以任意传值用来处理特殊情况
* @returns {{}}
*/
exports.handleSortData = (origin, params) => {
let leftContent = {};
let list = [],
allCount = 0;
leftContent.allSort = {};
leftContent.allSort.all = [{
name: '全部品类',
num: allCount,
href: handleFilterUrl(params, {}, {msort: true, misort: true})
}];
//
_.forEach(origin, value => {
let category = {
name: value.sort_name,
num: value.count,
childList: [
{
name: `全部${value.sort_name}`,
num: value.count,
href: handleFilterUrl(params, {msort: value.sort_id}, {misort: true}),
childActive: (params.msort === value.sort_id) && !params.misort
}
],
href: handleFilterUrl(params, {msort: value.sort_id}),
active: params.msort === value.sort_id
};
allCount += parseInt(value.count, 10);
_.forEach(value.sub, subValue => {
category.childList.push({
name: subValue.sort_name,
num: subValue.count,
href: handleFilterUrl(params, {msort: value.sort_id, misort: subValue.sort_id}),
childActive: params.misort === subValue.sort_id
});
if (params.misort === subValue.sort_id) {
category.active = true;
}
});
list.push(category);
});
leftContent.allSort.list = list;
return leftContent;
};
/**
* 店铺品类聚合
* @param data 分类数据
* @returns {{}}
*/
exports.setShopSort = (data, params) => {
let resData = {};
if (!_.isEmpty(data)) {
let list = [];
_.forEach(data, value => {
_.forEach(value.sub, subValue => {
list.push({
name: subValue.sort_name,
href: handleFilterUrl(params, {msort: value.sort_id, misort: subValue.sort_id}),
curMenu: params.misort === subValue.sort_id
});
});
});
if (list.length) {
_.set(resData, 'goodsMenu.menuList', list);
}
}
return resData;
};
/**
* 处理一般筛选数据
* @param origin 要处理的筛选数据 filter
* @param params 当前 URL 中已有的参数,处理选中状态使用
* @returns {{}}
*/
exports.handleFilterData = (origin, params, total) => {
let dest = {
ageLevel: [],
brand: {
default: [],
brandsShow: [],
brandIndex: [{
index: 'all',
name: '全部'
}, {
index: '0-9',
name: '0~9'
}],
showMore: true,
showMulti: true
},
price: [],
gender: [],
color: [],
checkedConditions: {
conditions: []
}
};
// 品牌索引数据处理
for (let i = 65; i < 91; i++) {
dest.brand.brandIndex.push({
index: String.fromCharCode(i).toLowerCase(),
name: String.fromCharCode(i)
});
}
// 品牌数据处理
// 分拆品牌参数
let brands = _.split(params.brand, ',');
let intBrands = [];
_.forEach(brands, value => {
intBrands.push(parseInt(value, 10));
});
if (origin.brand) {
let count = 0;
_.forEach(origin.brand, function(value) {
let brand = {
checked: (typeof _.find(intBrands, o => {
return _.isEqual(o, value.id);
}) !== 'undefined'),
href: handleFilterUrl(params, {brand: value.id}),
name: value.brand_name,
key: value.brand_domain,
id: value.id
};
if (!_.isEmpty(value.brand_alif)) {
if (!_.isNaN(parseInt(value.brand_alif, 10))) {
brand.index = '0-9';
} else {
brand.index = value.brand_alif.toLowerCase();
}
}
if (count < 10) {
dest.brand.default.push(brand);
}
dest.brand.brandsShow.push(brand);
count++;
});
// 没有品牌的情况下将 brand 设置为 false,前端不显示 品牌
if (_.size(dest.brand.brandsShow) <= 0) {
dest.brand = false;
} else {
// 品牌排序
dest.brand.brandsShow = _.sortBy(dest.brand.brandsShow, o => {
return o.index;
});
}
}
// 处理价格筛选数据
let priceRangechecked = false;
if (!_.isEmpty(origin.priceRange)) {
let customPrice = _.split(params.price, ',');
_.forEach(origin.priceRange, (value, key) => {
if (params.price === key) {
priceRangechecked = true;
}
let price = {
checked: params.price === key,
href: handleFilterUrl(params, {price: key}),
name: value
};
dest.price.push(price);
});
dest.customPrice = {
min: customPrice[0],
max: customPrice[1]
};
}
// 处理用户自主填写的价格区间
if (!priceRangechecked && params.price) {
let customPrice = _.split(params.price, ',');
if (customPrice[1] !== '99999') {
dest.customPrice = {
min: customPrice[0],
max: customPrice[1]
};
dest.checkedConditions.conditions.push({
name: '¥ ' + customPrice[0] + '-' + customPrice[1],
href: handleFilterUrl(params, {price: ''})
});
}
}
// 处理性别数据
dest.gender = [{
name: 'BOYS',
href: handleFilterUrl(params, {gender: '1,3'}),
checked: params.gender === '1,3'
}, {
name: 'GIRLS',
href: handleFilterUrl(params, {gender: '2,3'}),
checked: params.gender === '2,3'
}];
// 尺码处理
if (!_.isEmpty(origin.size) && (params.msort || params.misort || params.sort)) {
dest.size = [];
if (_.isArray(origin.size)) {
_.forEach(origin.size, value => {
dest.size.push({
checked: parseInt(params.size, 10) === parseInt(value.size_id, 10),
href: handleFilterUrl(params, {
size: value.size_id
}),
name: value.size_name
});
});
} else {
dest.size.push({
checked: parseInt(params.size, 10) === parseInt(origin.size.size_id, 10),
href: handleFilterUrl(params, {
size: origin.size.size_id
}),
name: origin.size.size_name
});
}
dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
handleCheckedData(params, dest.size, 'size'));
}
// 处理颜色筛选数据
if (!_.isEmpty(origin.color)) {
_.forEach(origin.color, (value) => {
let color = {
checked: parseInt(params.color, 10) === parseInt(value.color_id, 10),
href: handleFilterUrl(params, {color: value.color_id}),
name: value.color_name,
rgb: value.color_value ? 'url(' + value.color_value + ')' : '#' + value.color_code
};
// 处理颜色选中数据
if (color.checked) {
color.href = handleFilterUrl(params, null, {color: value.color_id});
dest.checkedConditions.conditions.push(color);
}
dest.color.push(color);
});
}
// 处理年龄段
if (!_.isEmpty(origin.ageLevel)) {
// 只有一个默认选中
let isChecked = origin.ageLevel.length === 1;
_.forEach(origin.ageLevel, (value) => {
let ageLevel = {
checked: params.ageLevel === value.id || isChecked,
href: handleFilterUrl(params, {ageLevel: value.id}),
name: value.name
};
// 处理颜色年龄段数据
if (ageLevel.checked || isChecked) {
ageLevel.href = handleFilterUrl(params, null, {ageLevel: value.id});
dest.checkedConditions.conditions.push(ageLevel);
}
dest.ageLevel.push(ageLevel);
});
}
// 清除所有选中数据
let remainParams = {};
if (params.id) {
remainParams.id = params.id;
}
dest.checkedConditions.clearUrl = '?' + queryString.stringify(remainParams);
// 处理频道筛选数据
dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
handleCheckedData(params, dest.gender, 'gender'));
// 处理品牌筛选数据
if (dest.brand.brandsShow) {
dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
handleBrandCheckedData(params, dest.brand.brandsShow));
}
// 处理价格筛选数据
dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
handleCheckedData(params, dest.price, 'price'));
// 商品记录小于10,不显示价格区间
if (total && total < 10) {
dest.price = null;
}
return dest;
};
/**
* 处理高级选项数据
* @param origin 要处理的筛选数据 filter
* @param params 当前 URL 中已有的参数,处理选中状态使用
* @returns {{}}
*/
exports.handleSeniorFilterData = (data, params) => {
let resData = {};
if (data) {
let chose = [];
let conditions = [];
if (!_.isEmpty(data.style)) {
let sub = [],
conName = [];
let styles = _.split(_.get(params, 'style', ''), ',');
_.forEach(data.style, value => {
let ched = styles.indexOf(`${value.style_id}`) >= 0;
if (ched && conName.length < 2) {
conName.push(value.style_name);
}
sub.push({
id: value.style_id,
checked: ched,
href: handleFilterUrl(params, {style: value.style_id}),
name: value.style_name
});
});
if (conName.length) {
conditions.push({
name: _.join(conName, '、'),
href: handleFilterUrl(params, {}, {style: true})
});
}
chose.push({
name: '风格',
showMulti: true,
sub: sub,
attr: 'style'
});
}
if (!_.isEmpty(data.standard)) {
let qStandardStr = (params && params.standard) || '';
let qStandard = {};
_.forEach(qStandardStr.split(','), value => {
let val = value.split('_');
if (val && val.length >= 2) {
qStandard[parseInt(val[0], 10)] = parseInt(val[1], 10);
}
});
_.forEach(data.standard, value => {
let sub = [];
// let parKey = `parameter_${value.standard_id}`;
_.forEach(value.sub, subValue => {
let ched = qStandard[value.standard_id] === parseInt(subValue.standard_id, 10);
if (ched) {
conditions.push({
name: subValue.standard_name,
href: handleFilterUrl(params, {
standard: getStandard(qStandard, null, {
k: value.standard_id,
v: subValue.standard_id
})
})
});
}
sub.push({
id: value.standard_id,
checked: ched,
href: handleFilterUrl(params, {
standard: getStandard(qStandard, {
k: value.standard_id,
v: subValue.standard_id
})
}),
name: subValue.standard_name
});
});
chose.push({
name: value.standard_name,
sub: sub
});
});
}
if (!_.isEmpty(chose)) {
resData.seniorChose = chose;
}
if (!_.isEmpty(conditions)) {
_.set(resData, 'checkedConditions.conditions', conditions);
}
}
return resData;
};
exports.handleFilterDataAll = (data, params) => {
let baseFilter = this.handleFilterData(data.filter, params, data.total);
let seniorFilter = this.handleSeniorFilterData({
style: _.get(data, 'filter.style', []),
standard: _.get(data, 'standard', [])
}, params);
let conditions = _.union(_.get(baseFilter, 'checkedConditions.conditions'),
_.get(seniorFilter, 'checkedConditions.conditions'));
let destFilter = Object.assign({}, seniorFilter, baseFilter);
if (!destFilter.checkedConditions) {
destFilter.checkedConditions = {};
}
destFilter.checkedConditions.conditions = conditions;
return destFilter;
};
/**
* 根据页面设置面包屑导航
* @type {[type]}
*/
exports.handlePathNavData = (data, params, page) => {
let pathNav = [{
href: '/',
name: '首页', // TODO 从根据cookie获取频道
pathTitle: '首页'
}];
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('brand')
},
{
name: data.brandNameEn + data.brandNameCn,
pathTitle: data.brandNameEn + data.brandNameCn
}
);
break;
case 'shop':
// 店铺
pathNav.push(
{
name: data.brandName,
pathTitle: data.brandName
}
);
break;
default :
// 分类
_.forEach(data, (sort) => {
if (sort.sort_id === params.msort) {
pathNav.push({
name: sort.sort_name,
href: helpers.urlFormat('/product/outlets/list', {msort: sort.sort_id}),
pathTitle: sort.sort_name
});
}
if (!_.isEmpty(sort.sub)) {
_.forEach(sort.sub, misort => {
if (misort.sort_id === params.misort) {
pathNav.push({
name: misort.sort_name,
pathTitle: misort.sort_name
});
}
});
}
});
break;
}
return {
pathNav: pathNav
};
};
/**
* 分页
* @param {[type]} total 总页数
* @param {[type]} params 筛选条件
* @return {[type]} [description]
*/
exports.handlePagerData = (total, params) => {
let dest = {
prePage: {
url: ''
},
nextPage: {
url: ''
},
pages: []
};
let currentPage = parseInt((_.isEmpty(params.page) ? 1 : params.page), 10); // 当前页
let perPageCount = parseInt((_.isEmpty(params.limit) ? 60 : params.limit), 10); // 每页商品数
let totalPage = parseInt(total / perPageCount, 10) + 1; // 总页数
if (currentPage === 1) {
// 当前页为 1,一定没有上一页
delete dest.prePage;
} else {
dest.prePage.url = handleFilterUrl(params, {page: currentPage - 1});
}
if (currentPage === totalPage) {
// 当前页为最后一页,一定没有下一页
delete dest.nextPage;
} else {
dest.nextPage.url = handleFilterUrl(params, {page: currentPage + 1});
}
// 页码临时数据
let pages = [];
if (currentPage > 2 && currentPage <= totalPage - 2) {
for (let i = currentPage - 2; i <= ((currentPage + 2) > totalPage ? totalPage : (currentPage + 2)); i++) {
pages.push({
url: handleFilterUrl(params, {page: i}),
num: i,
cur: currentPage === i
});
}
// 处理页码小于等于 2 的情况
} else if (currentPage <= 2) {
for (let i = 1; i <= (totalPage < 5 ? totalPage : 5); i++) {
pages.push({
url: handleFilterUrl(params, {page: i}),
num: i,
cur: currentPage === i
});
}
} else if (currentPage > totalPage - 2) {
for (let i = totalPage; i >= totalPage - 4; i--) {
pages.push({
url: handleFilterUrl(params, {page: i}),
num: i,
cur: currentPage === i
});
}
pages = _.sortBy(pages, ['num']);
}
let prevPages = [];
let nextPages = [];
if (_.size(pages) === 5) {
if (currentPage > 4) {
prevPages.push({
url: handleFilterUrl(params, {page: 1}),
num: 1
});
prevPages.push({
num: '...'
});
}
if (currentPage < totalPage - 2) {
nextPages.push({
num: '...'
});
nextPages.push({
url: handleFilterUrl(params, {page: totalPage}),
num: totalPage
});
}
}
// 商品开始结束数
dest.tip = {
total: total,
start: (currentPage ? currentPage - 1 : 0) * perPageCount + 1
};
let endPageNum = (totalPage === currentPage) ?
total : parseInt(dest.tip.start, 10) + perPageCount - 1;
dest.tip.end = (total < perPageCount) ? total : endPageNum;
dest.pages = _.concat(prevPages, pages, nextPages);
return dest;
};
/**
* 处理分类介绍
* @type {[type]}
*/
exports.handleSortIntro = (data, params) => {
let sortIntro = {
name: data.title,
enName: data.subtitle,
description: data.intro,
img: data.logo,
keyEntry: []
};
_.forEach(data.keyword, function(value) {
sortIntro.keyEntry.push({
name: value.word,
url: helpers.urlFormat(value.url, params)
});
});
return sortIntro;
};
/**
* 处理分类页左侧广告
* @type {[type]}
*/
exports.handleSortAds = (data) => {
let sortAds = {
picLink: {
picTitle: '',
list: []
}
};
_.forEach(data, function(value) {
sortAds.picLink.list.push({
href: value.ads_url,
src: value.ads_image
});
});
return sortAds;
};
/**
* 处理一周新品上架
* @type {[type]}
*/
exports.handleWeekNew = (data, params) => {
let dest = {
newSales: {
name: '',
list: []
}
};
_.forEach(data.recent, function(value, key) {
dest.newSales.list.push({
name: key,
href: handleFilterUrl(params, {shelveTime: key}),
active: key === params.shelveTime
});
});
return dest;
};
/**
* 处理搜索提示
* @type {[type]}
*/
exports.handleSuggest = (data) => {
let suggest = [];
_.forEach(data, function(value) {
suggest.push({
href: helpers.urlFormat('', {query: value.keyword}, 'search'),
keyword: value.keyword,
count: value.count
});
});
return suggest;
};
/**
* 处理品牌页banner
* @type {[type]}
*/
exports.handleBrandBanner = (data) => {
return {
bannerHeight: 150,
banner: helpers.getForceSourceUrl(data.brandBanner),
brandHome: data.url,
brandIntro: helpers.urlFormat('/about', '', data.brandDomain),
dataId: data.brandId
};
};
/**
* 处理品牌系列
* @type {[type]}
*/
exports.handleFolderData = (data) => {
let dest = [];
_.forEach(data, (value) => {
dest.push({
href: '?folder_id=' + value.id,
src: value.brand_sort_ico
});
});
return dest;
};
/**
* 筛选类链接处理的对外接口
* @type {[type]}
*/
exports.handleFilterUrl = handleFilterUrl;
/**
* 处理选中数据的对外接口
* @type {[type]}
*/
exports.handleCheckedData = handleCheckedData;
exports.handleNextPage = (params, total) => {
let href;
let currentPage = parseInt((_.isEmpty(params.page) ? 1 : params.page), 10); // 当前页
let perPageCount = parseInt((_.isEmpty(params.limit) ? 60 : params.limit), 10); // 每页商品数
let totalPage = parseInt(total / perPageCount, 10) + 1; // 总页数
if (currentPage === totalPage) {
return null;
} else {
href = handleFilterUrl(params, {page: currentPage + 1});
}
return {
href: href,
src: '//img10.static.yhbimg.com/product/2014/01/15/11/01fa01614784f6239760f1b749663016f1.jpg?' +
'imageMogr2/thumbnail/235x314/extent/235x314/background/d2hpdGU=/position/center/quality/90'
};
};