plustar.js
11.3 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
363
364
365
366
367
368
369
370
'use strict';
const productNameProcess = require(`${utils}/product-name-process`);
const api = global.yoho.API;
const serviceAPI = global.yoho.ServiceAPI;
const _ = require('lodash');
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;
const htmlProcess = require(`${global.utils}/html-process`);
const crypto = global.yoho.crypto;
const imageProcess = require(`${global.utils}/image`);
const privateKeyList = {
android: 'fd4ad5fcfa0de589ef238c0e7331b585',
iphone: 'a85bb0674e08986c6b115d5e3a4884fa',
ipad: 'ad9fcda2e679cf9229e37feae2cdcf80',
web: '0ed29744ed318fd28d2c07985d3ba633',
h5: 'fd4ad5fcfa0de589ef238c0e7331b585'
};
const formaData = (data, gender) => {
let build = [];
_.forEach(data, function(val) {
// 多张图
if (val.data[1]) {
let imgs = [];
_.forEach(val.data, function(list) {
let obj = {};
obj = _.assign(obj, {
url: '/guang/plustar/brandinfo?id=' + val.id + '&gender=' + gender,
img: list.src,
deps: val.brand_title
});
imgs.push(obj);
});
build.push({
imgs: imgs
});
}
// 单张图
if (val.data[0] && !val.data[1]) {
_.forEach(val.data, function(list) {
build.push({
url: '/guang/plustar/brandinfo?id=' + val.id + '&gender=' + gender,
img: list.src,
deps: val.brand_title
});
});
}
});
return build;
};
const getContentData = (gender, type, channel, isRecommend) => {
let param = {
gender: gender,
brand_type: type,
yh_channel: channel
};
if (isRecommend) {
param.is_recommend = isRecommend;
}
return serviceAPI.get('guang/api/v3/plustar/getlist', param, {cache: true}).then((result) => {
if (result && result.code === 200) {
return formaData(result.data.data.list[0].data, gender);
} else {
logger.error('list data return code is not 200');
return {};
}
});
};
const getListData = (gender, recom, all) => {
return Promise.all([getContentData(gender, recom), getContentData(gender, all)]).then((result) => {
let ps = [];
if (result[0]) {
ps.push({
focus: true,
name: '设计师',
list: result[0]
});
}
if (result[1]) {
ps.push({
name: '经典潮牌',
list: result[1]
});
}
return ps;
});
};
const getBrandsData = (gender, starBrand, originalBrand, channel, isRecommend) => {
return Promise.all([getContentData(gender, starBrand, isRecommend, channel), getContentData(gender, originalBrand, isRecommend, channel)]).then((result) => {
let ps = [];
if (result[1]) {
ps.push({
focus: true,
name: '原创潮牌',
list: result[1]
});
}
if (result[0]) {
ps.push({
name: '明星潮品',
list: result[0]
});
}
return ps;
});
};
// 新品到着
const getNewProduct = (brandId, gender, url, isApp) => {
return api.get('', {
method: 'app.search.li',
brand: brandId,
gender: gender,
limit: 6,
order: 's_t_desc',
page: 1
}, {cache: true}).then((result) => {
if (result && result.code === 200) {
let newArrival = {
naList: []
};
if (result.data.product_list) {
_.forEach(result.data.product_list, function(list, index) {
let obj = {};
let price;
if (index <= 5) {
let tag = [];
if (!list.product_skn) {
return false;
}
if (list.market_price === list.sales_price) {
list.market_price = false;
}
tag.push({
is_new: list.is_new === 'Y',
is_discount: list.is_discount === 'Y',
is_limited: list.is_limited === 'Y',
is_yohood: list.is_yohood === 'Y',
is_advance: list.is_advance === 'Y'
});
_.forEach(tag, function(data) {
if (data.is_discount === true && list.is_soon_soldOut === true) {
data.is_new = true;
data.is_discount = false;
} else if (data.is_discount === true && (data.is_new === true || data.is_limited === true || data.is_yohood === true || data.is_advance === true)) {
data.is_discount = false;
} else if (data.is_yohood === true && data.is_new === true) {
data.is_new = false;
}
});
if (list.market_price && list.market_price !== false) {
price = list.market_price + '.00';
}
newArrival.moreUrl = url;
if (list.cn_alphabet) {
list.cn_alphabet = productNameProcess(list.cn_alphabet);
}
let productUrl = '//m.yohobuy.com/product/pro_' + list.product_id + '_' +
list.goods_list[0].goods_id + '/' + list.cn_alphabet + '.html';
if (isApp) {
productUrl += `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${list.product_skn}"}}`;
}
obj = _.assign(obj, {
id: list.product_skn,
product_id: list.product_id,
thumb: helpers.image(list.default_images, 235, 314),
name: list.product_name,
salePrice: list.sales_price + '.00',
studentPrice: list.sales_price * 0.9,
is_soon_sold_out: list.is_soon_sold_out === 'Y',
isShowSaleTagDis: list.sales_price * 2 < list.market_price,
url: productUrl,
tags: tag
});
if (price) {
obj = _.assign(obj, {
price: price
});
}
newArrival.naList.push(obj);
}
});
}
return newArrival;
} else {
logger.error('new data return code is not 200');
return {};
}
});
};
// 相关资讯
const getRelatedEditorial = (brandId, uid, udid, clientType, isApp) => {
return serviceAPI.get('guang/service/v1/article/getArticleByBrand', {
brand_id: brandId,
uid: uid,
udid: udid,
client_type: clientType,
limit: 3,
private_key: privateKeyList[clientType]
}).then((result) => {
if (result && result.code === 200) {
let list = [];
_.forEach(result.data, function(data) {
if (isApp) {
data.url = data.url + '';
}
list.push({
id: data.id,
url: isApp ? `${helpers.https(data.url)}&openby:yohobuy={"action":"go.h5","params":{"param":{"id":"${data.id}"},"shareparam":{"id":"${data.id}"},"share":"/guang/api/v1/share/guang","id":${data.id},"type":1,"url":"http:${helpers.urlFormat('/info/index', null, 'guang')}","islogin":"N"}}` : data.url,
title: data.title,
text: data.intro,
img: helpers.image(data.src, 640, 640),
publishTime: data.publish_time,
like: data.like,
pageView: data.views_num
});
});
return list;
} else {
logger.error('editorial data return code is not 200');
return {};
}
});
};
// 是否收藏
const isCollection = (brandId, clientType, uid) => {
let param = {
brandId: brandId,
client_type: clientType,
private_key: privateKeyList[clientType]
};
if (uid) {
param.uid = uid;
}
return serviceAPI.get('shops/service/v1/favorite/getUidBrandFav', param).then((result) => {
if (result && result.code === 200) {
let isLike = true;
return isLike;
} else {
let isLike = false;
return isLike;
}
});
};
// 品牌详情
const getDetailData = (id, uid, udid, gender, isApp, clientType) => {
return serviceAPI.get('guang/api/v1/plustar/getbrandinfo', {
id: id,
client_type: clientType
}, {cache: true}).then((result) => {
if (result && result.code === 200) {
let list = result.data || [];
let jumpToApp;
let url;
let brandId = list.brand_id;
let brandDomain = list.brand_domain;
let isDifferent = list.is_different;
if (isApp) {
url = '?openby:yohobuy={"action":"go.brand","params":{"brand_id":"' + brandId + '","is_different":"' + isDifferent + '"}}';
uid = parseInt(crypto.decrypt(null, uid));
} else {
url = '//' + brandDomain + '.m.yohobuy.com';
}
if (isApp && !uid) {
jumpToApp = 1;
}
list.brand_ico = imageProcess.getSourceUrl(list.brand_ico, 'brandLogo');
return Promise.all([getRelatedEditorial(brandId, uid, udid, clientType, isApp), getNewProduct(brandId, gender, url, isApp)]).then((result) => {
list = _.assign(list, {
isLike: false,
infos: result[0],
newArrival: result[1],
jumpToApp: jumpToApp,
shareLink: '//guang.m.yohobuy.com/plustar/brandinfo?id=' + id,
shareTitle: list.brand_name,
shareImg: list.brand_ico,
shareDesc: htmlProcess.removeHtml(list.brand_intro),
clientType: clientType
});
return list;
});
} else {
logger.error('detail data return code is not 200');
return {};
}
});
};
/**
* [品牌详情异步数据]
*/
const getDetailDataAsync = (brandId, uid, udid, clientType, isApp) => {
return Promise.all([isCollection(brandId, clientType, uid), getRelatedEditorial(brandId, uid, udid, clientType, isApp)]).then((result) => {
return {
isLike: result[0],
infos: result[1]
};
});
};
module.exports = {
getListData,
getContentData,
getDetailData,
getDetailDataAsync,
getBrandsData
};