guang-process.js
16.2 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/**
* 逛处理类
*/
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const productNameProcess = require('./product-name-process');
const productPrcs = require('./product-process');
/**
* 将商品转化成以 product_skn 为键名的对象
* @param {*} goodsArray
*/
const _goodsArrayToObj = (goodsArray) => {
return _.keyBy(goodsArray, value => {
return value.product_skn;
});
};
/**
* [将首字符为//的url转换为http://]
* @param {[string]} url [需要转换的url]
* @return {[string]} [description]
*/
const transHttpsUrl = (url) => {
return url.replace(/^\/\//g, 'http://');
};
/**
* [过滤app查询字符串]
* @param {[string]} url [url地址]
* @return {[strng]}
*/
const getFilterUrl = (url) => {
url = url.replace('.m.yohobuy.com', global.yoho.config.subDomains.host)
.replace('www.yohobuy.com', global.yoho.config.siteUrl);
const whiteDomains = ['m.yohobuy.com', 'cdn.yoho.cn/myohobuy'];
const blackDomains = ['sale.m.yohobuy.com',
'cuxiao.m.yohobuy.com',
'activity.m.yohobuy.com',
'huodong.m.yohobuy.com',
'/home/orders/pay'];
if (whiteDomains.every(item => url.includes(item)) &&
blackDomains.every(item => !url.includes(item))) {
url = url.replace('http://', '//');
}
if (url.includes('feature.yoho.cn')) {
url = transHttpsUrl(url);
}
if (url.includes('openby:yohobuy=')) {
let filters = ['openby:yohobuy=', '&', '?'];
filters.forEach(item => {
url = url.subString(0, url.lastIndexOf(item));
});
return url.replace(/(^\s*)|(\s*$)/g, '');
} else {
return url;
}
};
/**
* [格式化资讯文章]
* @param {[array]} articleData [需要格式化的资讯数据]]
* @param {[Boolean]} showTag [是否显示左上角标签]]
* @param {Boolean} isApp [是否显示分享,在APP客户端里嵌入需要传url链接]
* @param {[Boolean]} showAuthor [控制是否显示作者信息]
* @param {[int]} uid [当前登录的用户ID]
* @return {[array | false]}
*/
const formatArticle = (articleData, showTag, isApp, showAuthor, uid) => {
// 资讯ID不存在,则不显示
if (!articleData.id) {
return false;
}
let result = {
id: articleData.id,
showTags: showTag,
img: articleData.src ? helpers.image(articleData.src, 640, 640) : '',
url: isApp ? `${helpers.https(articleData.url)}&openby:yohobuy={"action":"go.h5","params":{"param":{"id":"${articleData.id}"},"shareparam":{"id":"${articleData.id}"},"share":"/guang/api/v1/share/guang","id":${articleData.id},"type":1,"url":"http:${helpers.urlFormat('/info/index', null, 'guang')}","islogin":"N"}}` : articleData.url, //eslint-disable-line
title: articleData.title,
text: articleData.intro,
publishTime: articleData.publish_time,
publishTimeLong: articleData.publish_time_long,
pageView: articleData.views_num
};
if (result.url.includes('feature.yoho.cn') ||
result.url.includes('cdn.yoho.cn')) {
result.url = transHttpsUrl(result.url);
}
// 收藏
if (isApp) {
result.collect = {};
result.collect.isCollected = articleData.isFavor === true;
let colparam = articleData.colparam || {
urlpath: '',
param: ''
};
let originUrl = helpers.urlFormat(colparam.urlpath || '/author/index', null, 'guang'); // 跳转回的链接
// 根据用户是否登录做处理的链接
let collectUrl = 'javascript:;'; // eslint-disable-line
if (!uid) {
let playUrlEncode = `${originUrl}${colparam.param}`.replace(/\//g, '\\\/');
collectUrl = `${originUrl}?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"${playUrlEncode}","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}`; //eslint-disable-line
}
result.collect.url = collectUrl;
} else { // 点赞
result.like = {};
result.like.count = articleData.praise_num;
result.like.isLiked = articleData.isPraise === 'Y';
}
if (isApp && articleData.share.url) {
// 分享链接
result.share = `${articleData.share.url}?openby:yohobuy={"action":"go.share","params":{"title":"${articleData.title}","content":"${articleData.intro}","url":"${articleData.share.url}","pic":"https:${result.img}"}}`; //eslint-disable-line
}
// 判断是否显示作者信息
if (showAuthor && articleData.author) {
if (!isApp) {
articleData.author.url = getFilterUrl(articleData.author.url);
} else {
// 编辑人员 app跳转url处理 20160601
let isLogin = uid ? true : false;
articleData.author.url = `${helpers.https(articleData.author.url)}&openby:yohobuy={"action":"go.h5","params":{"param":{},"share":"","id":${articleData.author.author_id},"type":0,"islogin":"${isLogin}","url":"${articleData.author.url}"}}`; //eslint-disable-line
}
result.author = articleData.author;
if (result.author.avatar) {
result.author.avatar = result.author.avatar.replace('http://', '//');
}
}
// 模板中需要的标签标识
if (showTag && articleData.category_id) {
switch (articleData.category_id) {
case '1': // 话题
result.isTopic = true;
break;
case '2': // 搭配
result.isCollocation = true;
break;
case '3': // 潮人
result.isFashionMan = true;
break;
case '4': // 潮品
result.isFashionGood = true;
break;
case '5': // 小贴士
result.isTip = true;
break;
case '19': // 专题
result.isSpecialTopic = true;
break;
default:
break;
}
}
// 170503 新添加商品列表模块
if (articleData && articleData.product_list) {
result.productList = [];
_.forEach(articleData.product_list, value => {
result.productList.push(_.assign(value, {
price: parseFloat(value.sales_price).toFixed(2),
href: `//m.yohobuy.com/product/${value.product_skn}.html`
}));
});
}
return result;
};
/**
* [获取商品的ICON]
* @param {[int]} type [类型]
* @return {[type]}
*/
const getProductIcon = (type) => {
const icons = {
1: 'cloth',
3: 'pants',
4: 'dress',
6: 'shoe',
7: 'bag',
10: 'lamp',
241: 'headset',
8: 'watch',
360: 'swim-suit',
308: 'under'
};
return icons[type] || '';
};
/**
* [根据性别获取]
* @param {[type]} gender [description]
* @return {[type]} [description]
*/
const _getGenderByCookie = (gender) => {
switch (gender) {
case 1: // 男
return '1,3';
case 2: // 女
return '2,3';
default: // 其它
return '1,2,3';
}
};
/**
* [根据性别来决定 默认图片获取字段 如果是 2、3]
*
* 则优先从cover2 --》 cover1 -- 》 images_url
* 否则优先从cover1 --》 cover2 -- 》 images_url
*
* @param {[array]} images [images]
* @param {[Boolean]} gender [性别]
* @return {[type]}
*/
const _procProductImg = (images, gender) => {
let imgUrl = images.images_url || '';
let cover1 = images.cover_1 || '';
let cover2 = images.cover_2 || '';
gender = _getGenderByCookie(gender);
if (gender === '2,3') {
return cover2 || (cover1 || imgUrl);
} else {
return cover1 || (cover2 || imgUrl);
}
};
/**
* 格式化商品信息
*
* @param array productData 需要格式化的商品数据
* @param bool showTags 控制是否显示标签
* @param bool showNew 控制是否显示NEW图标
* @param bool showSale 控制是否显示SALE图标
* @param int width 图片的宽度
* @param int height 图片的高度
* @param bool isApp 判断是不是APP访问
* @param bool showPoint 商品价格是否显示小数位,默认显示
* @param int gender 性别
* @return array | false
*/
const formatProduct = (productData, showTags, showNew, showSale, width, height, isApp, showPoint, gender) => {
// 商品信息有问题,则不显示
if (!productData.product_skn || !productData.goods_list[0]) {
return false;
}
productData.sales_price = productData.sales_price || '';
productData.market_price = productData.market_price || '';
// 市场价和售价一样,则不显示市场价
if (parseInt(productData.market_price, 10) === parseInt(productData.sales_price, 10)) {
productData.market_price = false;
}
// 判别默认的商品是否将默认的图片URL赋值到skn
let flag = false;
// 如果设置了默认图片,就取默认的图片
for (let oneGoods of productData.goods_list) {
// 此skc是默认的,则将图片赋值给skn
if (oneGoods.is_default === 'Y') {
productData.default_images = _procProductImg(oneGoods, gender);
flag = true;
break;
}
}
// 如果还未赋值,则取第一个skc产品的默认图片
if (!flag) {
productData.default_images = _procProductImg(productData.goods_list[0], gender);
}
let result = {
id: productData.product_skn,
product_id: productData.product_id,
thumb: helpers.image(productData.default_images, width, height),
name: productData.product_name,
price: productData.market_price || false,
salePrice: productData.sales_price
};
if (showPoint) {
if (result.price) {
result.price += '.00';
}
if (result.salePrice) {
result.salePrice += '.00';
}
}
// TODO student price
result.studentPrice = parseInt(productData.sales_price * 100 * 0.9, 10) / 100;
result.is_soon_sold_out = (productData.is_soon_sold_out === 'Y');
if (productData.cn_alphabet) {
productData.cn_alphabet = productNameProcess(productData.cn_alphabet);
}
let url = encodeURI(helpers.urlFormat(`/product/${productData.product_skn}.html`));// 商品url改版
result.url = url.replace('http://', '//');
// APP访问需要加附加的参数
// 备注:如果以后APP的接口太多,可以把这边参数提取出来,变成一个公共的方法来生成,便于以后管理维护
if (isApp) {
result.url += `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":${productData.product_skn}}}`; //eslint-disable-line
}
if (showTags) {
result.tags = {
is_new: showNew && productData.is_new === 'Y', // 新品
is_discount: showSale && productData.is_discount === 'Y', // 在售
is_limited: productData.is_limited && productData.is_limited === 'Y', // 限量
is_yohood: productData.is_yohood && productData.is_yohood === 'Y', // YOHOOD
midYear: productData.midear && productData.midear === 'Y', // 年中
yearEnd: productData.yearnd && productData.yearnd === 'Y', // 年末
is_advance: productData.is_advance && productData.is_advance === 'Y' // 再到着
};
// 打折与即将售完组合显示打折
if (result.is_soon_sold_out && result.tags.is_discount) {
result.tags.is_new = false;
} else if (result.tags.is_discount &&
(result.tags.is_new ||
result.tags.is_limited ||
result.tags.is_yohood ||
result.tags.is_advance) // 打折与其它组合则隐藏打折
) {
result.tags.is_discount = false;
} else if (result.tags.is_yohood && result.tags.is_new) { // YOHOOD和新品组合显示YOHOOD
result.tags.is_new = false;
}
}
return result;
};
const processArticleDetail = (articleContent, isApp, gender, isWeixin, isqq, isWeibo) => {
let finalDetail = [];
let allgoods = '';
_.forEach(articleContent, (value, index) => {
// 文字
if (_.get(value, 'text.data.text', false)) {
finalDetail.push({
text: value.text.data.text
});
}
// 一张图片
if (_.get(value, 'singleImage.data.length', false)) {
finalDetail.push({
bigImage: helpers.image(_.get(value, 'singleImage.data[0].src', ''), 640, 640),
noLazy: index <= 3
});
}
// 两张小图
if (_.get(value, 'smallPic.data.length', 0) > 1) {
finalDetail.push({
smallImage: [
{
src: helpers.image(_.get(value, 'smallPic.data[0].src', ''), 315, 420)
},
{
src: helpers.image(_.get(value, 'smallPic.data[1].src', ''), 315, 420)
}
],
});
}
// 微信公众号?
if (_.get(value, 'weixinPublic', false)) {
finalDetail.push({
weixinPublic: _.get(value, 'weixinPublic', '')
});
}
// 分享?
if (_.get(value, 'shareCode', false)) {
let shareCode = false;
if (isWeixin) {
shareCode = _.get(value, 'shareCode.wechatShareImgUrl', false);
}
if (isqq) {
shareCode = _.get(value, 'shareCode.qqShareImgUrl', false);
}
if (isWeibo) {
shareCode = _.get(value, 'shareCode.showShareImgUrl', false);
}
if (shareCode) {
finalDetail.push({
shareCode: [{
codeShare: shareCode
}]
});
}
}
// 相关推荐
if (_.get(value, 'goods.data', false)) {
let relatedReco = [];
let moreThanOne = _.get(value, 'goods.data', []).length > 1;
_.forEach(_.get(value, 'goods.data', []), item => {
allgoods += item.id + ',';
});
if (moreThanOne) {
relatedReco = _.get(value, 'goods.data', []);
} else {
relatedReco = _.get(value, 'goods.data[0]', {});
}
finalDetail.push({
relatedReco: relatedReco
});
}
// 悬停浮动商品
if (_.get(value, 'goodsGroup.data', false)) {
let collocation = [];
_.forEach(_.get(value, 'goodsGroup.data', []), item => {
let cover = _.get(item, 'cover', '');
_.forEach(_.get(item, 'list', []), item2 => {
allgoods += item2.id + ',';
});
collocation.push({
thumb: cover ?
helpers.image(cover.cover, 235, 314) : '',
type: cover ? getProductIcon(cover.maxSortId) : '',
goods: _.get(item, 'list', [])
});
});
finalDetail.push({
collocation: collocation
});
}
// 更多商品链接
if (_.get(value, 'link', false)) {
finalDetail.push({
moreLink: _.get(value, 'link.data[0].url', '')
});
}
});
return {
finalDetail: finalDetail,
allgoods: allgoods
};
};
const pushGoodsInfo = (finalDetail, goodsList) => {
let goodsObj = _goodsArrayToObj(productPrcs.processProductList(goodsList));
// console.log(goodsList);
_.forEach(finalDetail, (value, key) => {
// if (value.relatedReco) {
// console.log(value.relatedReco);
// }
if (value.collocation) {
_.forEach(value.collocation, (item, subKey) => {
_.forEach(item.goods, (thItem, thKey) => {
finalDetail[key].collocation[subKey].goods[thKey] = goodsObj[thItem.id];
});
});
}
});
// console.log(JSON.stringify(finalDetail, 2, 2));
return finalDetail;
};
module.exports = {
formatArticle,
transHttpsUrl,
getFilterUrl,
formatProduct,
getProductIcon,
processArticleDetail,
pushGoodsInfo
};