brand-list.js
5.6 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
'use strict';
const api = global.yoho.API;
const serviceAPI = global.yoho.ServiceAPI;
const _ = require('lodash');
const yhChannel = {
1: {
channel: '301'
},
2: {
channel: '302'
},
4: {
channel: '303'
}
};
const handleBrandList = origin => {
let dest = {
ListData: [],
indexList: []
};
// 标记是否有数字,有数字先暂存
let hasNum = false;
let numTemp = {};
_.forEach(origin, (value, key) => {
let brands = [];
if (_.size(value) <= 0) {
return;
}
if (key === '0~9') {
hasNum = true;
numTemp = origin[key];
} else {
_.forEach(value, (subValue) => {
brands.push({
name: subValue.brand_name_en || subValue.brand_name_cn || subValue.brand_name,
logo: subValue.brand_ico,
domain: subValue.brand_domain
});
});
dest.ListData.push({
index: key,
brands: brands
});
dest.indexList.push({
index: key,
name: key === '0~9' ? '0' : key
});
}
});
// 商品列表排序一次
_.sortBy(dest.ListData, o => {
return o.index.charCodeAt();
});
// 字母列表排序一次
_.sortBy(dest.indexList, o => {
return o.index.charCodeAt();
});
// 如果有数字,单独处理
if (hasNum) {
let brands = [];
_.forEach(numTemp, (subValue) => {
brands.push({
name: subValue.brand_name_en || subValue.brand_name_cn || subValue.brand_name,
logo: subValue.brand_ico,
domain: subValue.brand_domain
});
});
dest.ListData.push({
index: '0~9',
brands: brands
});
dest.indexList.push({
index: '0_9',
name: '0'
});
}
return dest;
};
const _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;
});
};
const _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 : [];
});
};
const _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 => {
return Object.assign(finalResult, handleBrandList(result.data.all_list));
});
};
const indexData = (isbrand, gender, code) => {
if (isbrand) {
return Promise.all([_getChannelData(gender), _getResourcesData(code), _getBrandListOriginData(gender)]).then(result => {
let brandList = {};
let list = {};
if (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;
}
list.brandList = brandList;
return list;
});
// return favfavBrand(uid, page, limit).then(result=> {
// if (result.total === 0) {
// result = {nobrandData: 1};
// }
// return result;
// });
} else {
// return favProduct(uid, page, limit).then(result=> {
// if (result.total === 0) {
// result = {noproductData: 1};
// }
// return result;
// });
}
};
const brandListData = (code, gender) => {
return Promise.all([_getResourcesData(code), _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;
})
}
module.exports = {
indexData,
brandListData
};