header.js
7.16 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
/**
* header model
* @author: 赵彪<bill.zhao@yoho.cn>
* @date: 2016/05/03
*/
'use strict';
const _ = require('lodash');
const api = global.yoho.API;
const serviceApi = global.yoho.ServiceAPI;
const helpers = global.yoho.helpers;
const Handlebars = require('handlebars');
const path = require('path');
const headerHtml = require('fs').readFileSync(path.resolve(__dirname, '../views/partial/headerNew.hbs')).toString();
const template = Handlebars.compile(headerHtml);
const logger = global.yoho.logger;
/**
* 获取菜单
* @param undefined
* @return {array} 菜单数组
*/
const getMenuData = () => (
[{
link: 'http://www.yoho.cn',
cn: '集团官网',
en: 'YOHO!'
}, {
link: 'http://www.yohoboys.com',
cn: '男生潮流',
en: 'YOHO!BOYS'
}, {
link: 'http://www.yohogirls.com',
cn: '女生潮流',
en: 'YOHO!GIRLS'
}, {
link: 'http://www.yohoshow.com',
cn: '物趣分享',
en: 'YOHO!SHOW'
}, {
link: 'http://www.yohood.cn',
cn: '潮流嘉年华',
en: 'YO\'HOOD'
}]
);
/**
* 获取导航
* @param {Object} data 要处理的数据
* @param {String} type 频道类型
* @return {array} 导航数组
*/
const getNavBar = (data, type) => {
let navBars = [];
_.forEach(data, item => {
let obj = {};
let lowEn = _.camelCase(item.sort_name_en).toLowerCase();
Object.assign(obj, {
type: lowEn,
link: item.sort_url,
cn: item.sort_name,
en: item.sort_name_en,
isNewPage: item.is_new_page === 'Y'
});
if (type === lowEn) {
obj.active = true;
}
// 奥莱频道显示图片,特殊处理
if (lowEn === 'outlets') {
obj.ico = item.sort_ico;
}
navBars.push(obj);
});
return navBars;
};
/**
* 获取品牌名字
* @param {Object} data 要处理数据
* @return {array} 品牌数组
*/
const getBrandItems = (data) => {
let brandItems = [];
_.forEach(data, item => {
let obj = {
link: '',
hot: false,
brandName: ''
};
obj.link = item.sort_url;
obj.hot = item.is_hot === 'Y' ? true : false;
obj.brandName = item.sort_name;
brandItems.push(obj);
});
return brandItems;
};
/**
* 获取三级菜单
* @param {Object} data 要处理数据
* @return {array} 三级菜单数组
*/
const getThirdNav = (data) => {
let thirdNav = [];
_.forEach(data, item => {
let obj = {
link: '',
title: '',
brandItems: false
};
obj.link = item.sort_url;
obj.title = item.sort_name;
if (item.sub) {
obj.brandItems = getBrandItems(item.sub);
}
thirdNav.push(obj);
});
return thirdNav;
};
/**
* 获取子菜单
* @param {Object} data 要处理数据
* @param {String} type 频道类型
* @return {array} 子菜单数组
*/
const getSubNavGroup = (data, type) => {
let subNavGroup = [];
_.forEach(data, it => {
let subNav = [];
_.forEach(it.sub, item => {
let obj = {};
obj.link = item.sort_url;
obj.name = item.sort_name;
obj.isHot = item.is_hot === 'Y' ? true : false;
obj.isNew = item.is_new === 'Y' ? true : false;
if (item.sub) {
obj.thirdNav = getThirdNav(item.sub);
obj.imgCode = item.content_code;
}
subNav.push(obj);
});
let lowEn = _.camelCase(it.sort_name_en).toLowerCase();
subNavGroup.push({
subType: lowEn,
subNav: subNav,
active: lowEn === type
});
});
return subNavGroup;
};
/**
* 处理接口返回的数据
* @param {object} 接口返回的对象
* @param {String} 指定页面类型为boys,girls,kids,lifestyle
* @return {object} 头部数据
*/
const setHeaderData = (resData, type) => (
{
header: true,
headType: type,
yohoGroup: getMenuData(),
navbars: resData ? getNavBar(resData, type) : [],
subNavGroup: resData ? getSubNavGroup(resData, type) : []
}
);
const getHotSearchAsync = (channel) => {
return api.get('', {method: 'app.search.getTerms', yh_channel: channel}, {
cache: 600,
code: 200
});
};
const getHeaderNavAsync = () => {
return serviceApi.get('operations/api/v6/category/getCategory', {}, {
cache: true,
code: 200
});
};
/**
* 请求头部数据
* @param {String} 频道类型
* @return {promise}
*/
const cacheHeaderHtml = {
boys: [],
girls: [],
kids: [],
lifestyle: []
};
let cacheNavData;
const THIRTY_MINUTES = 1000 * 60 * 10;
async function requestHeaderData(type) {
let resData = {};
let _html;
if (!cacheHeaderHtml[type]) {
type = 'boys';
}
let cacheData = cacheHeaderHtml[type];
if (_.isEmpty(cacheData[0]) || ((new Date() - cacheData[1]) > THIRTY_MINUTES)) {
let channelNum = (function() {
switch (type) {
case 'boys':
return 1;
case 'girls':
return 2;
case 'kids':
return 3;
case 'lifestyle':
return 4;
default:
return 1;
}
}());
let res = await Promise.all([
getHeaderNavAsync(),
getHotSearchAsync(channelNum)
]);
resData.headerData = {};
if (res[0] && res[0].data) {
Object.assign(resData.headerData, setHeaderData(res[0].data, type));
cacheNavData = _.get(resData, 'headerData.subNavGroup', '');
} else {
logger.error('header api data empty');
}
if (res[1] && res[1].data) {
resData.headerData.defaultSearch = _.get(res[1], 'data.defaultTerms[0].content', '');
resData.headerData.hotTerms = _.map(_.get(res[1], 'data.hotTerms', []), (value) => {
return {
href: helpers.urlFormat('', {query: value.content}, 'search'),
content: value.content,
sort: value.sort,
status: value.status,
type: value.type
};
});
} else {
logger.error('search terms api data empty');
}
if (_.isEmpty(_.get(resData, 'headerData.navbars')) && cacheData[0]) {
_html = cacheData[0];
} else {
_html = template(resData);
cacheHeaderHtml[type] = [_html, new Date()];
}
} else {
_html = cacheData[0];
}
return Promise.resolve({headerData: _html});
}
async function getHeaderSubNav(type) {
if (_.isEmpty(cacheNavData)) {
let res = await getHeaderNavAsync();
cacheNavData = _.get(setHeaderData(res.data, type), 'subNavGroup', []);
}
return Promise.resolve(_.takeWhile(cacheNavData, o => {
return o.subType === type;
}));
}
module.exports = {
requestHeaderData,
getHeaderSubNav
};