group-filter-process.js
5.51 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
const _ = require('lodash');
const toArray = (obj) => {
if (_.isArray(obj)) {
return obj;
}
let arr = [];
_.forEach(obj, (v, k) => {
if (_.isObject(v)) {
v._key = k;
} else {
v = {
_key: k,
_value: v
};
}
arr.push(v);
});
return arr;
};
/**
* 按照数组中指定字段排序二维数组
*
* @param array list 需要排序的数组
* @param string key 字段名称
* @param boolean 有 desc 时候降序排列,默认为false
*/
const _sortListByField = (list, key, desc) => {
let array = toArray(list);
return array.sort(function(a, b) {
a = a._key.split(',')[0] * 1;
b = b._key.split(',')[0] * 1;
return desc ? a < b : a > b;
});
};
/**
* 处理筛选数据
* @param list
* @param string | options
* @return array 处理之后的筛选数据
*/
exports.processFilter = (list, options) => {
const filters = {
classify: []
};
const filtersType = {
brand: {
name: '所有品牌',
title: '品牌',
dataId: 'id',
subsName: 'brand_name',
firstSub: 0,
dataType: 'brand',
sortNum: '1'
},
color: {
name: '所有颜色',
title: '颜色',
dataId: 'color_id',
subsName: 'color_name',
firstSub: 0,
dataType: 'color',
sortNum: '4'
},
discount: {
name: '所有商品',
title: '折扣',
dataId: 'key',
subsName: 'name',
firstSub: '0,1',
dataType: 'p_d',
sortNum: '7'
},
gender: {
name: '所有性别',
title: '性别',
dataId: 'key',
subsName: 'flag',
firstSub: '1,2,3',
dataType: 'gender',
sortNum: '0'
},
group_sort: {
name: '所有品类',
title: '品类',
dataId: 'relation_parameter',
subsName: 'category_name',
firstSub: 0,
dataType: 'sort',
sortNum: '3'
},
priceRange: {
name: '所有价格',
title: '价格',
dataId: 'key',
subsName: 'flag',
firstSub: 0,
dataType: 'price',
sortNum: '6'
},
size: {
name: '所有尺码',
title: '尺码',
dataId: 'size_id',
subsName: 'size_name',
firstSub: 0,
dataType: 'size',
sortNum: '5'
},
ageLevel: {
name: '所有人群',
title: '人群',
dataId: 'id',
subsName: 'name',
firstSub: 0,
dataType: 'ageLevel',
sortNum: '2'
}
};
options = Object.assign({
gender: '1,2,3', // 默认选择的性别,默认1,2,3表示所有
exclude: null // 需要排除的字段
}, options);
_.forEach(list, (item) => {
let classify = {
subs: []
};
let key = item.filterId;
if (!filtersType[key]) {
return;
}
if ((options.hideSize && key === 'size') || (options.hideSort && key === 'group_sort')) {
return;
}
classify.dataType = filtersType[key].dataType;
classify.name = filtersType[key].name;
classify.title = filtersType[key].title;
classify.subs.push({
chosed: true,
dataId: filtersType[key].firstSub,
name: filtersType[key].name
});
// 折扣,价格区间,需要排序
if (key === 'discount' || key === 'priceRange') {
item = _sortListByField(item, 'name');
}
// 测试数据
// let example = [];
// for (let i = 0; i < 12; i++) {
// example.push({
// itemId: '1,3',
// itemName: 'MEN'
// });
// }
// item.itemList
_.forEach(item.itemList, (sub) => {
let subs = {};
subs.dataId = sub.itemId;
subs.name = sub.itemName;
// if (key === 'discount') {
// subs.dataId = sub._key;
// } else if (key === 'priceRange') {
// subs.dataId = sub._key;
// } else if (filtersType[key].dataId === 'key') {
// subs.dataId = index;
// } else if (filtersType[key].dataId === 'relation_parameter') {
// subs.dataId = sub.relation_parameter['sort']; // eslint-disable-line
// } else {
// subs.dataId = sub[filtersType[key].dataId];
// }
// if (key === 'priceRange') {
// subs.name = sub._value.replace('¥', '¥');
// } else if (filtersType[key].subsName === 'flag') {
// subs.name = sub;
// } else {
// subs.name = sub[filtersType[key].subsName];
// if (key === 'discount') {
// subs.name = subs.name + '折商品';
// }
// }
if (options[classify.dataType] + '' === subs.dataId + '') {
subs.chosed = true;
classify.subs[0].chosed = false;
}
classify.subs.push(subs);
});
filters.classify[filtersType[key].sortNum] = classify;
});
return filters;
};