Utils.js
8.1 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
'use strict';
import ReactNative from 'react-native';
import * as _ from 'lodash';
function isContains(str, substr) {
return new RegExp(substr).test(str);
}
function randomString(len) {
len = len || 32;
var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
var maxPos = $chars.length;
var pwd = '';
for (let i = 0; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
function parsecategoryFilter(json) {
let categoryFilterList = json.categoryFilterList;
let filterCategoryDetailFilterList = json.filterCategoryDetailFilterList;
for (var i = 0; i < categoryFilterList.length; i++) {
let key = categoryFilterList[i].key;
let type = 0;
if (key == 'sort') {
type = 0;
}else if (key == 'brand'){
type = 1;
}else if (key == 'color'){
type = 2;
}else if (key == 'sizeKey'){
type = 3;
}else if (key == 'price'){
type = 4;
}else if (key == 'p_d'){
type = 5;
}else if (key == 'gender'){
type = 7;
}else if (key == 'style'){
type = 10;
}
_.forEach(filterCategoryDetailFilterList, (v, k) => {
if (k == key) {
let list = v;
for (var j = 0; j < list.length; j++) {
if (list[j].isSelect) {
categoryFilterList[i].select = list[j];
categoryFilterList[i].subFilter = {list,key,type};
}
}
}
});
}
return{
categoryFilterList,
}
}
function parseProductList(json) {
let currentPage = json && json.page ? json.page : 1;
let pageCount = json && json.page_total ? json.page_total : 0;
let total = json && json.total ? json.total : 0;
let filter = json && json.filter ? json.filter : {};
let list = json && json.product_list ? json.product_list : [];
let filterCategoryDetailFilterList = {};
let categoryFilterList = [];
if (filter['gender']) {
categoryFilterList.push({
key: 'gender',
name: '性别',
isSelect: true,
});
let genderList = filter['gender'];
let newGenderList =[];
newGenderList.push({
key:'1,2,3',
name: '全部',
isSelect: true,
});
for (let gender in genderList) {
if (genderList.hasOwnProperty(gender)) {
newGenderList.push({
key: gender,
name: genderList[gender],
isSelect: false,
});
}
}
filterCategoryDetailFilterList.gender;
filterCategoryDetailFilterList.gender = newGenderList;
}
if (filter['group_sort']) {
categoryFilterList.push({
key: 'sort',
name: '品类',
isSelect: false,
});
let sortList = filter['group_sort'];
let newSortList = [];
newSortList.push({
key: '',
name: '所有品类',
isSelect: true,
});
sortList.map((item, i) => {
let subSortList = [];
subSortList.push({
key: item.relation_parameter.sort ? item.relation_parameter.sort : '',
name: '所有'+item.category_name,
isSelect: true,
});
let sub = item['sub'];
sub.map((items, j) => {
subSortList.push({
key: items.relation_parameter.sort ? items.relation_parameter.sort : '',
name: items.category_name,
isSelect: false,
})
});
newSortList.push({
key: item.relation_parameter.sort ? item.relation_parameter.sort : '',
name: item.category_name,
isSelect: false,
sub: subSortList,
})
});
filterCategoryDetailFilterList.sort;
filterCategoryDetailFilterList.sort = newSortList;
}
if (filter['brand']) {
categoryFilterList.push({
key: 'brand',
name: '品牌',
isSelect: false,
});
let brandList = filter['brand'];
let newBrandList = [];
newBrandList.push({
key: '',
name: '所有品牌',
isSelect: true,
});
brandList.map((item, i) => {
newBrandList.push({
key: item.id,
name: item.brand_name,
isSelect: false,
brand_alif : item.brand_alif,
})
});
filterCategoryDetailFilterList.brand;
filterCategoryDetailFilterList.brand = newBrandList;
}
if (filter['color']) {
categoryFilterList.push({
key: 'color',
name: '颜色',
isSelect: false,
});
let colorList = filter['color'];
let newColorList = [];
newColorList.push({
key: '',
name: '所有颜色',
isSelect: true,
});
colorList.map((item, i) => {
newColorList.push({
key: item.color_id,
name: item.color_name,
isSelect: false,
});
});
filterCategoryDetailFilterList.color;
filterCategoryDetailFilterList.color = newColorList;
}
if (filter['size']) {
categoryFilterList.push({
key: 'sizeKey',
name: '尺码',
isSelect: false,
});
let sizeList = filter['size'];
let newSizeList = [];
newSizeList.push({
key: '',
name: '所有尺码',
isSelect: true,
});
sizeList.map((item, i) => {
newSizeList.push({
key: item.size_id,
name: item.size_name,
isSelect: false,
});
});
filterCategoryDetailFilterList.sizeKey;
filterCategoryDetailFilterList.sizeKey = newSizeList;
}
if (filter['priceRange']) {
categoryFilterList.push({
key: 'price',
name: '价格',
isSelect: false,
});
let priceObject = filter['priceRange'];
priceObject = sortListByField(priceObject, 'name'); // 折扣,价格区间,需要排序
let newPriceList = [];
newPriceList.push({
key: '',
name: '所有价格',
isSelect: true,
});
_.forEach(priceObject, (v, k) => {
newPriceList.push({
key: v._key,
name: v._value,
isSelect: false,
});
});
filterCategoryDetailFilterList.price;
filterCategoryDetailFilterList.price = newPriceList;
}
if (filter['discount']) {
categoryFilterList.push({
key: 'p_d',
name: '折扣',
isSelect: false,
});
let p_dObject = filter['discount'];
p_dObject = sortListByField(p_dObject, 'name'); // 折扣,价格区间,需要排序
let newP_dList = [];
newP_dList.push({
key: '',
name: '所有折扣',
isSelect: true,
});
_.forEach(p_dObject, (v, k) => {
newP_dList.push({
key: v._key,
name: v.name,
isSelect: false,
});
});
filterCategoryDetailFilterList.p_d;
filterCategoryDetailFilterList.p_d = newP_dList;
}
let recId = randomString(40);
let standard = json && json.standard ? json.standard : [];
for (var i = 0; i < standard.length; i++) {
let standardItem = standard[i]
standardItem.isSelect = false;
standardItem.selectFilter = {
standard_id: '',
standard_name: '',
}
}
let tabs = json && json.tabs ? json.tabs : {};
let keys = Object.keys(tabs);
keys = keys.sort();
let latestTabs = [];
for (var i = 0; i < keys.length; i++) {
let tab = keys[i]
let item = {
type: tab,
name: tabs[tab]
};
latestTabs.push(item);
}
return {
list,
categoryFilterList,
filterCategoryDetailFilterList,
standard,
currentPage,
pageCount,
total,
recId,
latestTabs,
};
}
function 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
*/
function sortListByField(list, key, desc) {
let array = toArray(list);
array = array.sort((a, b) => {
let matchNumber = /([\d]+)/g;
// 有键,使用键的值排序
if (a[key] && b[key]) {
let numA = +(_.toArray(a[key].match(matchNumber))[0] || 0); // 取第一个出现的数字排序,如果不存在,取0
let numB = +(_.toArray(b[key].match(matchNumber))[0] || 0);
return (desc ? numA > numB : numA < numB) ? -1 : 1;
}
// 无键, 使用本身
let numA = +(_.toArray(a._value.match(matchNumber))[0] || 0);
let numB = +(_.toArray(b._value.match(matchNumber))[0] || 0);
return numA < numB ? -1 : 1;
});
return array;
}
module.exports = {
parseProductList,
parsecategoryFilter,
isContains,
};