list.js
5.96 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
'use strict';
const _ = require('lodash');
const Search = require('../models/list');
const camelCase = global.yoho.camelCase;
function brandLetters() {
let letters = [];
letters.push({
letter: '0-9',
selected: false
});
for (let i = 'A'.charCodeAt(0); i <= 'Z'.charCodeAt(0); i++) {
letters.push({
letter: String.fromCharCode(i),
selected: false
});
}
return letters;
}
function peopleChoose() {
return [
{
name: '男士',
value: '1,3'
}, {
name: '女士',
value: '2,3'
}];
}
function colorConver(colors) {
return colors.map(c => {
return {
id: c.colorId,
title: c.colorName,
rgb: c.colorValue || '#' + c.colorCode
};
});
}
function newFilter(key, value, name) {
return {
key: key,
value: value,
name: name
};
}
const list = {
index: (req, res, next) => {
let page = req.query.page || 1;
let brand = req.query.brand || 0;
let sort = req.query.sort || '';
let gender = req.query.gender || '';
let price = req.query.price || '';
let size = req.query.size || '';
let color = req.query.color || '';
Search.queryProduct({
page: page,
brand: brand,
sort: sort,
gender: gender,
size: size,
price: price,
color: color
}).then(result => {
if (result && result.code === 200 && result.data) {
let data = camelCase(result.data);
let ret = {};
if (data.filter) {
let priceRange = data.filter.priceRange;
let sizeInfo = data.filter.size;
let genders = peopleChoose(gender);
let brands = data.filter.brand;
let colors = colorConver(data.filter.color);
let sorts = data.filter.groupSort;
let filters = [];
genders.forEach(g => {
if (g.value === gender) {
g.checked = true;
filters.push(newFilter('gender', gender, g.name));
}
});
priceRange = Object.keys(priceRange).map((k) => {
let prices = k.split(',');
if (k === price) {
filters.push(newFilter('price', price, `¥${prices[0]}-¥${prices[1]}`));
}
return {
lower: prices[0],
higher: prices[1]
};
}).sort((a, b) => {
return a.lower - b.lower;
});
if (!_.isArray(sizeInfo)) {
sizeInfo.checked = true;
sizeInfo = [sizeInfo];
}
if (size) {
sizeInfo.forEach(s => {
if (s.sizeId === parseInt(size)) {
filters.push(newFilter('size', size, s.sizeName));
}
});
}
if (brand) {
let brandNames = brands.filter(b => {
return (',' + brand + ',').indexOf(',' + b.id + ',') >= 0;
}).map(b => {
return b.brandName;
}).join('、');
filters.push(newFilter('brand', brand, brandNames));
}
if (color) {
colors.forEach(c => {
if (c.id === parseInt(color)) {
c.cur = true;
let fi = newFilter('color', color, c.title);
fi.color = c;
filters.push(fi);
}
});
}
ret = {
people: genders,
sortData: sorts,
brandData: brands,
colors: colors,
size: sizeInfo,
priceRange: priceRange,
filters: filters,
showFilters: filters.length > 0
};
}
res.display('list', _.assign(ret, {
letters: brandLetters(),
total: data.total,
pageTotal: data.pageTotal,
pageNo: data.page,
products: data.productList,
navPath: {
nav: true,
pathNav: [
{
link: true,
href: '',
pathTitle: '',
name: 'MEN首页'
},
{
link: true,
href: '',
pathTitle: '',
name: '咨询'
},
{
link: false,
href: '',
pathTitle: '',
name: '咨询'
}
]
},
module: 'product',
page: 'list',
title: '列表'
}));
} else {
throw 'query product error';
}
}).catch(next);
}
};
module.exports = list;