brands-model.js
10.4 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
361
362
/**
* 品牌一览 model
* @author: ghw<hongwei.gao@yoho.cn>
* @date: 2016/9/29
*/
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const helpers = global.yoho.helpers;
const brandApi = require('./brands-api');
const serviceApi = global.yoho.ServiceAPI;
// 品牌一览资源位CODE码
const channelCode = {
boys_brand: '8b16b7baf9a66fbe553a6caa97d2ce2a',
girls_brand: 'c95ae9e40f0add10549b819f821ad626',
kids_brand: 'c575c6bfdfa4125fae7d24bbec7119c8',
lifestyle_brand: '84b7926282fdef92f1039bdcf77c18ba',
brand_list: 'f0f72b1e8f30e6ad086dfc4401f3a856', // 品牌列表资源位CODE码
brand_plusstar_banner_boys: 'd0149783b8dd2adaf083fd10556c39a9',
brand_plusstar_banner_girls: 'd0149783b8dd2adaf083fd10556c39a9',
brand_plusstarindex_boys: 'a833aed63d28457156310e97faa7fa37', // plusstarindex男首资源位
brand_plusstarindex_girls: '6e4f162be3b3ba44f3bfcf1c38bdb745' // plusstarindex女首资源位
};
const BOYS = 'boys';
const GIRLS = 'girls';
const KIDS = 'kids';
const LIFESTYLE = 'lifestyle';
/**
* 获取品牌一览资源位&channelType
*
* @param string $channelStr
* @return array
*/
const switchBrandParams = channel => {
let req = {};
switch (channel) {
case BOYS:
req = {
channelType: 1,
brandCode: channelCode.brand_plusstar_banner_boys
};
break;
case GIRLS:
req = {
channelType: 2,
brandCode: channelCode.brand_plusstar_banner_girls
};
break;
case KIDS:
req = {
channelType: 3,
brandCode: channelCode.kids_brand
};
break;
case LIFESTYLE:
req = {
channelType: 4,
brandCode: channelCode.lifestyle_brand
};
break;
default:
req = {
channelType: 1,
brandCode: channelCode.boys_brand
};
break;
}
return req;
};
/**
* 获取品牌一览页面,品牌top
* @param string $channel 频道名称
*/
const getBrandViewTop = channel => {
return co(function*() {
let switchParams = switchBrandParams(channel);
let res = yield brandApi.getBrandTopData(switchParams.brandCode);
let result = {},
brandAds = [],
brandLogos = [];
// 头部10个品牌小图块 url
if (res.data[1].data && res.data[1].data.list) {
_.forEach(res.data[1].data.list, subValue => {
brandAds.push({
name: subValue.name,
src: helpers.image(subValue.src, 80, 50, 3),
url: helpers.urlFormat(subValue.url)
});
});
}
// 头部品牌图块,广告位
if (res.data[0].data) {
_.forEach(res.data[0].data, subValue => {
let srcUrl;
// kids lifestyle 第一张图尺寸不同
if (switchParams.channelType === 1 || switchParams.channelType === 2) {
srcUrl = helpers.image(subValue.src, 222, 180, 3);
} else {
srcUrl = (subValue === 0) ? helpers.image(subValue.src, 570, 280, 3) :
helpers.image(subValue.src, 280, 280, 3);
}
brandLogos.push({
name: subValue.title,
src: srcUrl,
url: helpers.urlFormat(subValue.url)
});
});
}
// 整合brandTop数据结构,boys、girls
if (switchParams.channelType === 1 || switchParams.channelType === 2) {
result.isTab = true;
}
result.tabHeader = brandLogos;
result.logos = brandAds;
return result;
})();
};
/**
* 获取品牌一览list
* @param string $channel 频道名称
* @param int start 开始位置 1 开始
* @param int length 取数长度 0 取到最后
*/
const getBrandViewList = (channel, start, length) => {
return co(function*() {
let switchParams = switchBrandParams(channel);
let res = yield brandApi.getBrandListData(switchParams.brandCode);
let result = [],
navigation = [];
// 品牌list A-Z 0-9
if (res.data.brands) {
_.forEach(res.data.brands, (subValue, key) => {
let listTmp = [];
_.forEach(subValue, ssubValue => {
// 为品牌名称
let href;
if (switchParams.channelType === 1) {
href = helpers.urlFormat(ssubValue.brand_domain) + '?gender=1,3';
} else if (switchParams.channelType === 2) {
href = helpers.urlFormat(ssubValue.brand_domain) + '?gender=2,3';
} else {
href = helpers.urlFormat(ssubValue.brand_domain);
}
let brandItem = {
name: ssubValue.brand_name,
key: ssubValue.id,
href: href
};
if (ssubValue.is_hot === 'Y') {
brandItem.hot = 'hot';
}
listTmp.push(brandItem);
});
navigation.push(key);
result.push({
key: key,
val: listTmp
});
});
}
// 只取部分数据
let begin;
begin = (start - 1) ? (start - 1) : 0;
begin = (begin > 0) ? begin : 0;
result = length ? result.slice(begin, length + begin) : result.slice(begin);
result.navigation = navigation;
return result;
})();
};
/**
* 获取单个广告浮窗内容
*
* @param int $brandId
* @param int $uid
* @return array
*/
const getBrandInfo = (brandId, uid) => {
return co(function*() {
let data = {},
imgs = [];
// 获取品牌简介
let res = yield brandApi.getBrandIntro(brandId, uid);
if (res.data) {
// 获取品牌下的产品信息
let proInfo = yield brandApi.getProductByBrand(brandId, 3);
let proInfoTmp = proInfo.data.product_list;
if (!_.isEmpty(proInfoTmp)) {
_.forEach(proInfoTmp, subValue => {
imgs.push({
src: helpers.image(subValue.default_images, 80, 100)
});
});
}
// 整合
data = {
key: res.data.brand_id,
icon: helpers.image(res.data.brand_ico, 80, 50),
title: res.data.brand_name,
content: res.data.brand_intro,
subtitle: 'FEATURED ITEMS',
imgs: imgs
};
}
return data;
})();
};
/**
* 多个品牌ID获取品牌信息
*
* @param array $brandIds
* @return array
*/
const getBrandInfoByIds = (brandIds) => {
return co(function*() {
let data = yield brandApi.getBrandInfoByIds(brandIds);
let brandsInfo = {};
if (data.data && data.code === 200) {
_.forEach(data.data, (subValue, k) => {
subValue.desc = _.trim(subValue.brand_intro.replace('/[\r\n\t ]/g',''));
subValue.url = helpers.urlFormat(subValue.brand_domain);
delete subValue.brand_intro;
brandsInfo[k] = subValue;
})
}
return brandsInfo;
})();
}
/**
* 获取plusstar品牌列表项目
*
* @param string $channel
* @return array
*/
const getPlusstarBrandListItem = (channel) => {
return co(function*() {
let code = channel === 'girls' ? channelCode.brand_plusstar_banner_girls :
channelCode.brand_plusstar_banner_boys;
// 资源位数据
let resource = yield serviceApi.get('operations/api/v5/resource/get', {content_code: code});
let items = [];
if (resource.data && resource.code === 200) {
items[0] = {'name': '所有品牌', 'src': '', 'url': helpers.urlFormat('/brands'), 'brandType': ''},
items[1] = {'name': '设计新潮', 'src': '', 'url': '', 'brandType': 4},
items[2] = {'name': '潮流经典', 'src': '', 'url': '', 'brandType': 1},
items[3] = {'name': '明星潮牌', 'src': '', 'url': '', 'brandType': 2},
items[4] = {'name': '原创潮牌', 'src': '', 'url': '', 'brandType': 3}
let resourceData = resource.data,
pos = 0;
_.forEach(items, (subValue, k) => {
if (_.isEmpty(subValue.url)) {
subValue.url = helpers.urlFormat('/brands/plusstar?id=' + k);
}
if (pos in resourceData[0].data) {
subValue.url = helpers.urlFormat(resourceData[0].data[pos].url);
subValue.src = helpers.image(resourceData[0].data[pos].src, 222, 180, 1);
subValue.name = resourceData[0].data[pos].title;
}
pos++;
})
}
return items;
})();
}
/**
* 获取Plustar列表
*
* @param string $brandType
* @param string $gender
* @return array
*/
const getPlustarList = (brandType, gender) => {
return co(function*() {
let list = yield brandApi.getPlusstarList(brandType, gender);
let brandList = {},
data = {},
brandsIds = [];
if (list.data.data.list) {
brandList = list.data.data.list[0];
}
if(brandList.data) {
_.forEach(brandList.data, brand => {
let src = '';
if(brand.data[0]) {
src = helpers.image(brand.data[0].src, 320, 160, 1);
}
data[brand.brand_id] = {
brand_id: brand.brand_id,
name: brand.brand_name,
sort_id: brand.sort_id,
src: src,
desc: '',
url: ''
};
brandsIds.push(brand.brand_id);
})
}
return {brandsIds: brandsIds, data: data};
})();
}
module.exports = {
getBrandViewTop,
getBrandViewList,
getBrandInfo,
getBrandInfoByIds,
getPlusstarBrandListItem,
getPlustarList
};