channel.js
9.42 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
360
/**
* 频道页面 model
* @author: Bi Kai<kai.bi@yoho.cn>
* @date: 2016/05/09
*/
'use strict';
const utils = '../../../utils';
const contentCodeConfig = require('../../../config/content-code');
const _ = require('lodash');
const logger = global.yoho.logger;
const resourcesProcess = require(`${utils}/resources-process`);
/**
* 性别数据
* @type {Object}
*/
const genderData = {
boys: '1,3',
girls: '2,3'
};
/**
* 楼层资源的位置码
* @type {Object}
*/
const contentCode = contentCodeConfig.channel;
/**
* 频道底部 Banner 位置码
* @type {Object}
*/
const bottomBannerCode = contentCodeConfig.bottom;
/**
* 频道选择页 默认数据
* @type {Object}
*/
const channelList = [{
href: '/boys',
title: '男生',
entitle: 'BOYS'
}, {
href: '/girls',
title: '女生',
entitle: 'GIRLS'
}, {
href: '/kids',
title: '潮童',
entitle: 'KIDS'
}, {
href: '/lifestyle',
title: '创意生活',
entitle: 'LIFESTYLE'
}];
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 获取二级菜单顶部颜色
* @param {[string]} choosed
* @return {[string]}
*/
_getSidebarColor(choosed) {
let color = false;
if (choosed === 'girls') {
color = '#FF88AE';
} else if (choosed === 'kids') {
color = '#7ad9f9';
} else if (choosed === 'lifestyle') {
color = '#4f4138';
}
return color;
}
/**
* 处理侧边栏数据
* @param {[array]} list
* @return {[array]}
*/
_processSideBar(list, choosed) {
const formatData = [];
let offset = 0; // 分割数组用到的游标
list = list || [];
_.forEach(list, (item, i) => {
if (_.isArray(item.sub)) {
item.sub.unshift({
sort_name: item.sort_name,
sort_name_en: item.sort_name_en,
back: true,
isSelect: false,
bgColor: this._getSidebarColor(choosed)
});
}
// 如果有分隔符,分割数组
if (item.separative_sign === 'Y') {
formatData.push(list.slice(offset, i));
offset = i;
}
});
// 数组被分割剩余的部分
formatData.push(list.slice(offset));
return formatData;
}
/**
* 获取频道页面资源位
* @param {[object]} gender
* @return {[type]}
*/
getChannelResource(params) {
params.gender = params.gender || 'boys';
params.limit = params.limit || 100;
params = Object.assign(params, {
gender: genderData[params.gender] || '1,2,3',
content_code: contentCode[params.gender], // eslint-disable-line
page: 1
});
if (!params.uid) {
params.new_device = true; // eslint-disable-line
}
params.new_device = true;
return this.get({
url: 'operations/api/v5/resource/home',
data: params,
api: global.yoho.ServiceAPI,
param: {
cache: true
}
}).then(result => {
if (result && result.code === 200 && result.data && result.data.list) {
for (let item of result.data.list) {
item.template_name === 'single_image' &&
item.data.length === 1 &&
(item.singleOne = true);
}
return resourcesProcess(result.data.list);
} else {
logger.error('index resouce is not 200');
return result;
}
});
}
/**
* 获取左侧边栏数据
* @param {[string]} choosed
* @return {[object]}
*/
_getLeftNav(choosed) {
choosed = choosed || 'all';
return this.get({
url: 'operations/api/v6/category/getCategory',
api: global.yoho.ServiceAPI,
param: {
cache: true
}
}).then(result => {
// console.log(result)
if (result && result.code === 200) {
return this._processSideBar(result.data, choosed);
} else {
logger.error('sidebar code is not 200');
return result;
}
});
}
/**
* 获取热门搜索
**/
_getSearchIndex(params) {
let channels = {
boys: 1,
girl: 2,
kids: 3,
lifestyle: 4
};
if (params.gender && channels[params.gender]) {
params.yh_channel = channels[params.gender];
delete params.gender;
}
return this.get({
data: _.assign({
method: 'app.search.getTerms'
}, params),
param: {
cache: true
}
}).then((result) => {
if (result && result.code === 200) {
return result.data;
} else {
logger.error('Hot Search return code is not 200');
return {};
}
});
}
/**
* 获取频道选择页数据
* @return {[type]}
*/
_getChannelList() {
return this.get({
url: 'operations/api/v5/entrance/getEntrance',
api: global.yoho.ServiceAPI,
param: {
cache: true
}
}).then((result) => {
if (result && result.code === 200) {
const list = {};
list.channelList = [];
list.yohood = {};
result.data.list = result.data.list || [];
_.forEach(result.data.list, function(item) {
let id = _.toNumber(item.id);
const channel = channelList[id - 1];
if (channel) {
list.channelList.push(channel);
}
if (id === 5) {
list.yohood.showYohood = true;
list.yohood.yohoodHref = 'http://www.yohood.cn';
}
if (id === 6) {
list.double11 = {};
list.double11.href = item.url;
}
});
return Object.keys(list).length ? list : {
channelList: channelList
};
} else {
logger.error('channel select code is not 200');
return {
channelList: channelList
};
}
});
}
/**
* 获取频道选择页 背景
* @return {[type]}
*/
_getChannelBg() {
return this.get({
url: 'operations/api/v5/resource/get',
data: {
content_code: contentCode.index
},
api: global.yoho.ServiceAPI,
param: {
cache: true
}
}).then(result => {
if (result && result.code === 200) {
return result.data.length && result.data[0] && result.data[0].data && result.data[0].data.list[0];
} else {
logger.error('channel select background code is not 200');
return {
src: ''
};
}
});
}
/**
* 获取频道选择页面数据
* @param {[string]} gender
* @return {[type]}
*/
getChannelSwitchData() {
return Promise.all([this._getChannelList(), this._getChannelBg()]);
}
/**
* 获取频道页面数据
* @param {[object]} params
* @return {[object]}
*/
getChannelData(params) {
var channelData = {};
let navGender = _.cloneDeep(params.gender);
return Promise.all([this.getChannelResource(params), this._getLeftNav(navGender), this._getSearchIndex({
gender: navGender,
uid: params.uid,
})]).then((data) => {
channelData.content = data[0] || []; // 资源位数据
channelData.sideNav = data[1] || []; // 侧边栏数据
channelData.defaultTerms = (data[2] && data[2].defaultTerms && data[2].defaultTerms.length) ?
data[2].defaultTerms[0].content : '搜索商品、品牌';
return channelData;
});
}
/**
* 获取频道页底部 bannel 数据
* @param {[string]} gender
* @return {[type]}
*/
getBottomBannerData(gender) {
gender = gender || 'boys';
if (gender === 'boys' || gender === 'girls') {
return this.get({
url: 'operations/api/v5/resource/get',
data: {
content_code: bottomBannerCode[gender]
}, // eslint-disable-line
api: global.yoho.ServiceAPI,
param: {
cache: true
}
});
}
return Promise.resolve({
code: 400,
data: '',
message: '参数错误'
});
}
/**
* 获取店铺推荐收藏和人数
*/
shopRecom(params) {
return this.get({
data: {
method: 'app.shops.floorNewInfo',
shop_ids: params.shopIds,
uid: params.uid,
},
api: global.yoho.API
});
}
};