brands-service.js
9.76 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
/**
* 品牌一览 controller
* @author: ghw<hongwei.gao@yoho.cn>
* @date: 2016/9/29
*/
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const api = global.yoho.API;
const headerModel = require('../../../doraemon/models/header');
const brandsModel = require('./brands-model');
const _ = require('lodash');
const helpers = global.yoho.helpers;
const pager = require(`${global.utils}/pager`).setPager;
const BOYS = 'boys';
const GIRLS = 'girls';
const KIDS = 'kids';
const LIFESTYLE = 'lifestyle';
const queryString = require('querystring');
/**
* 获取品牌一览资源位&channelType
*
* @param string $channelStr
* @return array
*/
const getGenderByChannel = channel => {
let gender = '';
switch (channel) {
case BOYS:
gender = '1,3';
break;
case GIRLS:
gender = '2,3';
break;
default:
gender = '1,2,3';
break;
}
return gender;
};
const getHomeurlByChannel = channel => {
let home;
switch (channel) {
case GIRLS:
home = helpers.urlFormat('/woman', {}, 'new');
break;
case LIFESTYLE:
home = helpers.urlFormat('/lifestyle', {}, 'new');
break;
case KIDS:
home = helpers.urlFormat('/kids', {}, 'new');
break;
default:
home = helpers.urlFormat('');
break;
}
return home;
};
// 添加网站的SEO
const seoMap = {
boys: {
title: '品牌一览|男装品牌排行榜,男装品牌大全|YOHO!BUY 有货 100%正品保证',
keywords: '品牌一览,男装品牌,男装品牌排行榜,男装品牌大全,YOHO!BUY 有货',
description: 'YOHO!BUY 有货男装品牌一览汇集国内国际各大男装品牌大全,为广大爱好时尚的男士青年提供品牌男装、' +
'休闲男装、商务男装.YOHO!BUY 有货,100%正品保证'
},
girls: {
title: '品牌一览|女装品牌排行榜,女装品牌大全|YOHO!BUY 有货 100%正品保证',
keywords: '品牌一览,女装品牌,女装品牌排行榜,女装品牌大全,YOHO!BUY 有货',
description: 'YOHO!BUY 有货女装品牌一览汇集国内国际各大女装品牌,为广大爱美女生提供品牌女装、休闲女装、' +
'商务女装.买品牌女装就上YOHO!BUY 有货,100%正品保证'
},
kids: {
title: '品牌一览|童装童鞋品牌,儿童鞋包配饰排行榜,潮童品牌大全|YOHO!BUY 有货 100%正品保证',
keywords: '童装品牌,童装童鞋排行榜,儿童鞋包配饰排行榜,潮童品牌大全,品牌一览,YOHO!BUY 有货',
description: 'YOHO!BUY 有货童装品牌一览汇集国内国际各大童装品牌大全,为广大爱好潮流的儿童提供品牌童装、童鞋,' +
'儿童鞋包配饰.YOHO!BUY 有货,100%正品保证'
},
lifestyle: {
title: '品牌一览|数码3c,居家,玩具娱乐,文具,美妆品牌|YOHO!BUY 有货 100%正品保证',
keywords: '数码3c品牌,居家品牌,玩具娱乐品牌,文具品牌,美妆品牌',
description: 'YOHO!BUY 有货女装品牌一览汇集国内国际各大数码3c品牌,居家品牌,玩具娱乐品牌,文具品牌,' +
'美妆品牌.买创意生活家居就上YOHO!BUY 有货,100%正品保证'
},
'boys-plusstar': {
title: '品牌plusstar,男装潮流品牌简介|YOHO!BUY有货',
keywords: '品牌plusstar,男装潮流品牌简介',
description: 'YOHO!BUY 有货男装品牌一览汇集国内国际各大男装品牌,为广大爱美男生提供品牌男装、' +
'休闲男装、商务男装.买品牌男装就上YOHO!BUY 有货,100%正品保证'
},
'girls-plusstar': {
title: '品牌plusstar,女装潮流品牌简介|YOHO!BUY有货',
keywords: '品牌plusstar,女装潮流品牌简介',
description: 'YOHO!BUY 有货女装品牌一览汇集国内国际各大女装品牌,为广大爱美女生提供品牌女装、' +
'休闲女装、商务女装.买品牌女装就上YOHO!BUY 有货,100%正品保证'
}
};
/**
* plusstar 分页链接伪静态
* @param string uri
* @param string originUrl 动态地址
*/
const staticUrl = (uri, originUrl) => {
originUrl = queryString.parse(_.split(originUrl, '?')[1]);
return `${uri}id${originUrl.id || 0}-p${originUrl.page || 1}/`;
};
/**
* 获取品牌一览list
* @param string $channel 频道名称
* @param int start 开始位置 1 开始
* @param int length 取数长度 0 取到最后
*/
exports.getBrandViewList = (channel) => {
let apiMethod = [
headerModel.requestHeaderData(channel),
brandsModel.getBrandViewTop(channel),
brandsModel.getBrandViewList(channel) // 分屏加载
];
return api.all(apiMethod).then(result => {
let responseData = {
module: 'brands',
page: 'brands',
noCashe: false
};
// 头部数据
Object.assign(responseData, result[0]);
// 品牌一览列表
responseData.brands = result[1];
responseData.brands.navigation = result[2].navigation;
responseData.brands.category = result[2];
// 导航pathNav
responseData.brands.pathNav = [
{
href: getHomeurlByChannel(channel),
name: `${_.toUpper(channel)}首页`,
pathTitle: 'YOHO!BUY 有货'
},
{
href: helpers.urlFormat('/brands'),
name: '品牌一览',
pathTitle: '品牌一览'
}
];
// SEO
Object.assign(responseData, seoMap[channel]);
// 数据出错不cashe
if (_.isEmpty(result[0].headerData) || _.isEmpty(result[1]) || _.isEmpty(result[2])) {
responseData.noCashe = true;
}
return responseData;
});
};
/**
* brandList-Ajax调用
*/
exports.getBrandList = (channel, start) => {
let apiMethod = [
brandsModel.getBrandViewList(channel, start)
];
return api.all(apiMethod).then(result => {
let responseData = {};
// 品牌一览列表
responseData.category = result[0];
return responseData;
});
};
/**
* 品牌接口数据
*
* @param string brandId 获取品牌ID
* @return json
*/
exports.brandInfo = (brandId, uid) => {
let apiMethod = [
brandsModel.getBrandInfo(brandId, uid)
];
return api.all(apiMethod).then(result => {
let responseData = {
code: _.isEmpty(result[0]) ? 400 : 200,
brand: _.isEmpty(result[0]) ? '' : result[0],
noCashe: false
};
// 数据出错不cashe
if (_.isEmpty(result[0])) {
responseData.noCashe = true;
}
return responseData;
});
};
/**
* 多个品牌ID获取品牌信息
*
* @param array $brandIds
* @return array
*/
exports.plusstarList = (channel, req) => {
return co(function*() {
let headerData = yield headerModel.requestHeaderData(channel);
let responseData = {
module: 'brands',
page: 'brands',
brandsHomePage: true,
noCashe: false
};
let id = req.query.id || '',
gender = req.query.gender || getGenderByChannel(channel),
limit = 20,
page = parseInt(req.query.page, 10) || 1;
let items = yield brandsModel.getPlusstarBrandListItem(channel);
let brandType = 1;
if (+id !== 0 && items[id]) {
brandType = items[id].brandType;
}
let plustarList = yield brandsModel.getPlustarList(brandType, gender);
let list = plustarList.data;
// 数据出错不cashe
if (_.isEmpty(headerData.headerData) || _.isEmpty(items) || _.isEmpty(list)) {
responseData.noCashe = true;
}
let brandIds = [],
brands = [],
pageList = {};
brandIds = plustarList.brandsIds.slice((page - 1) * limit, page * limit);
if (plustarList.brandsIds.length > limit) {
pageList = pager(Math.ceil(plustarList.brandsIds.length / limit), {
page: page,
id: id
});
// url 伪静态
_.forEach(pageList.pages, (val) => {
val.url = staticUrl(`/${channel}-brands/plusstar/`, val.url);
});
if (pageList.nextPage) {
pageList.nextPage.url = staticUrl(`/${channel}-brands/plusstar/`, pageList.nextPage.url);
}
if (pageList.prePage) {
pageList.prePage.url = staticUrl(`/${channel}-brands/plusstar/`, pageList.prePage.url);
}
}
if (brandIds.length > 0) {
// 获取品牌信息
let brandsInfo = yield brandsModel.getBrandInfoByIds(brandIds);
_.forEach(brandIds, brandId => {
if (brandsInfo[brandId]) {
list[brandId].desc = brandsInfo[brandId].desc;
list[brandId].url = helpers.urlFormat('', {gender: gender}, brandsInfo[brandId].url);
}
brands.push(list[brandId]);
});
// 数据出错不cashe
if (_.isEmpty(brandsInfo)) {
responseData.noCashe = true;
}
}
let data = {
brandsHomePage: true,
brands: {
items: brands,
tabs: items
}
};
// 头部数据
Object.assign(responseData, headerData);
// 产品信息
Object.assign(responseData, data);
// 页码
Object.assign(responseData, pageList);
// SEO
Object.assign(responseData, seoMap[`${channel}-plusstar`] || {});
return responseData;
})();
};