global.js
4.36 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
'use strict';
const utils = '../../../utils';
const productProcess = require(`${utils}/product-process`);
const globalapi = global.yoho.GlobalAPI;
const $ = require('cheerio');
const _ = require('lodash');
exports.list = (param) => {
return globalapi.get('product/api/v2/detail/getlist', param).then((result) => {
if (!result || !result.data) {
return {};
}
return {
filter: productProcess.processFilter(result.data.filter || []),
list: result.data.product_list.map((data) => {
return {
product_url: data.is_global === 'Y' ? `//m.yohobuy.com/product/global/${data.product_skn}.html` : `//m.yohobuy.com/product/${data.product_skn}.html`, // eslint-disable-line
product_img: data.default_images,
price: data.formart_final_price,
product_name: data.product_name,
country_name: data.country_name,
is_stock: data.is_stock,
is_limited: data.is_limited,
is_plane: data.is_plane
};
})
};
});
};
exports.getBrand = (param) => {
return globalapi.get('editor/api/v1/brand/get', param).then((data) => {
return data.data || {};
});
};
exports.detail = (param) => {
return globalapi.get('product/api/v2/detail/get', param).then((result) => {
if (!result || !result.data) {
return {};
}
result = result.data;
let goods = result.goods_list || [];
if (goods.length === 1) {
result.bannerTop = {
img: (goods[0].images_list[0] || {}).image_url,
imgAlt: _.get(result, 'brand_info.brand_name', '') + '|' + _.get(result, 'product_name', '')
};
} else {
result.bannerTop = {
list: goods.map((g) => {
return {
img: (g.images_list[0] || {}).image_url,
imgAlt: _.get(result, 'brand_info', 'brand_name', '') + '|' + _.get(result, 'product_name', '')
};
})
};
}
result.show_final_price = result.formart_final_price;
if (result.formart_final_price !== result.formart_orign_price) {
result.show_orign_price = result.formart_orign_price;
}
result.show_sales_price = result.format_sales_price;
if (result.format_market_price !== result.format_sales_price) {
result.show_market_price = result.format_market_price;
}
if (result.brand_info && result.brand_info.brand_id) {
result.brand_info.brand_url = `//m.yohobuy.com/product/global/list?brand=${result.brand_info.brand_id}`;
}
if (result.illustrate_contents && result.illustrate_contents.length) {
result.illustrate = {
title1: (result.illustrate_contents[0] || {}).title,
content1: (result.illustrate_contents[0] || {}).content,
title2: (result.illustrate_contents[1] || {}).title,
content2: (result.illustrate_contents[1] || {}).content
};
}
return result;
});
};
exports.gethtml = (param) => {
return globalapi.get('product/api/v1/detail/gethtml', param, {
cache: true
}).then((result) => {
result = result || '';
result = $.load(result);
result = result('.good-detail-page');
// 修改为你优选url
result.find('.recommend-for-you a.swiper-slide').each((index, el)=> {
el = $(el);
let href = el.attr('href');
if (href) {
let o = (href.split('?')[1] || '').replace('openby:yohobuy=', '');
o = JSON.parse(o);
el.attr('href', `//m.yohobuy.com/product/global/${o.params.skn}.html`);
}
});
return (result.html() || '').replace(/<img src=/g, '<img class="lazy" src="data:image/gif;' +
'base64,R0lGODlhAQABAJEAAAAAAP///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw=="' +
' data-original=').replace(/<img border="0" src=/g, '<img border="0" class="lazy" ' +
'src="data:image/gif;base64,' +
'R0lGODlhAQABAJEAAAAAAP///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw=="' +
' data-original=').replace(/http:\/\//g, '//');
});
};