brand-list.js
8.28 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
'use strict';
const api = global.yoho.API;
const serviceAPI = global.yoho.ServiceAPI;
const _ = require('lodash');
const helpers = global.yoho.helpers;
const yhChannel = {
1: {
channel: '1'
},
2: {
channel: '2'
},
4: {
channel: '3'
}
};
const genderMap = {
MEN男士: '1,3',
WOMEN女士: '2,3'
};
const channelNav = {
boys: 'MEN男士',
girls: 'WOMEN女士'
};
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
handleBrandList(origin) {
let dest = {
ListData: [],
indexList: []
};
let keyList = [];
// 标记是否有数字,有数字先暂存
let hasNum = false;
let numTemp = {};
_.forEach(origin, (value, key) => {
if (_.size(value) <= 0) {
return;
}
_.forEach(value, function(subValue) {
subValue.brandUrl = helpers.urlFormat('/', {
query: subValue.brand_domain,
app_type: 1
}, 'search');
subValue.name = subValue.brand_name_en || subValue.brand_name_cn || subValue.brand_name;
subValue.domain = subValue.brand_domain;
});
if (key === '0~9') {
hasNum = true;
numTemp = origin[key];
} else {
keyList.push(key);
}
});
keyList.sort();
_.forEach(keyList, function(key) {
let keyName = key;
dest.ListData.push({
index: keyName,
brands: origin[key]
});
dest.indexList.push({
index: keyName,
name: key === '0~9' ? '0' : key
});
});
// 如果有数字,单独处理
if (hasNum) {
dest.ListData.push({
index: '0~9',
brands: numTemp
});
dest.indexList.push({
index: '0_9',
name: '0'
});
}
return dest;
}
_getChannelData(params) {
let gender = params - 1 || 0;
return api.get('', {
method: 'app.blk.getAllChannels'
}, {
cache: true,
code: 200
}).then(result => {
let channel = [];
if (gender === 3 && result.data.length === 3) {
// 1:男,2:女,3:童装,4:创意生活
// 如果为3,说明童装没有配置,只配置了创意生活。所以要减一
gender = 2;
}
_.forEach(result.data, (res, index) => {
channel.push({
id: res.channel_id,
mame: res.channel_name,
code: res.content_code,
focus: index === gender ? true : false
});
});
return channel;
});
}
_getResourcesData(contentCode) {
if (!contentCode) {
return [];
}
return serviceAPI.get('operations/api/v5/resource/get', {
content_code: contentCode
}, {
cache: true,
code: 200
}).then(result => {
return result && result.data ? result.data : [];
});
}
_getBrandListOriginData(channel) {
let finalResult = {};
return api.get('', {
method: 'app.brand.newBrandList',
yh_channel: yhChannel[channel].channel,
app_type: 1
}, {
code: 200,
cache: true
}).then(result => {
if (result.data && result.data.all_list) {
return Object.assign(finalResult, this.handleBrandList(result.data.all_list));
} else {
return {};
}
});
}
// 全部分类数据
_processCateData(list, channel, appType) {
let nav = ['MEN男士', 'WOMEN女士'];
nav = _.map(nav, function(item) {
return {
name: item,
channel: item,
focus: item === channelNav[channel]
};
});
_.forEach(list, function(item, key) {
item.focus = key === channelNav[channel];
_.forEach(item, function(firstItem) {
// 如果有二级菜单,二级菜单跳转,否则一级菜单跳转
if (firstItem.sub && firstItem.sub.length) {
_.forEach(firstItem.sub, function(secondItem) {
secondItem.url = helpers.urlFormat('/', {
sort: _.get(secondItem, 'relation_parameter.sort'),
sort_name: secondItem.category_name,
gender: genderMap[key] || '',
app_type: appType
}, 'list');
});
firstItem.sub.unshift({
category_name: `全部${firstItem.category_name}`,
url: helpers.urlFormat('/', {
sort: _.get(firstItem, 'relation_parameter.sort'),
sort_name: firstItem.category_name,
gender: genderMap[key] || '',
app_type: appType
}, 'list')
});
} else {
firstItem.url = helpers.urlFormat('/', {
sort: _.get(firstItem, 'relation_parameter.sort'),
sort_name: firstItem.category_name,
gender: genderMap[key] || '',
app_type: appType
}, 'list');
}
});
});
return {
nav,
list
};
}
_getCateData(channel, appType) {
return api.get('', {
method: 'app.sort.get',
app_type: 1
}, {
cache: true
}).then((result) => {
if (!result.code || result.code !== 200 || !result.data) {
return [];
}
return result.data;
}).then((list) => {
return this._processCateData(list, channel, appType);
});
}
indexData(gender, brandCode, channel, appType) {
return Promise.all([
this._getChannelData(gender),
this._getResourcesData(brandCode),
this._getBrandListOriginData(gender),
this._getCateData(channel, appType)
]).then(result => {
let brandList = {};
let list = {};
let categoryData = {};
if (result[0]) {
categoryData.channel = result[0];
list.channel = result[0];
}
if (result[1]) {
_.forEach(result[1], function(data) {
if (data.focus_type === '1') {
brandList.bannerTop = data;
} else if (data.focus_type === '2') {
brandList.focusData = data.data;
}
});
}
if (result[2]) {
brandList.listData = result[2].ListData;
brandList.indexList = result[2].indexList;
}
if (result[3]) {
list.categoryData = result[3];
}
list.brandList = brandList;
return list;
});
}
brandListData(code, gender) {
return Promise.all([this._getResourcesData(code), this._getBrandListOriginData(gender)]).then(result => {
let brandList = {};
let list = {};
if (result[0]) {
_.forEach(result[0], function(data) {
if (data.focus_type === '1') {
brandList.bannerTop = data;
} else if (data.focus_type === '2') {
brandList.focusData = data.data;
}
});
}
if (result[1]) {
brandList.listData = result[1].ListData;
brandList.indexList = result[1].indexList;
}
list.brandList = brandList;
return list;
});
}
};