product-detail.js
6.48 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
'use strict';
const $ = require('cheerio');
const _ = require('lodash');
const mipUtils = require('../mip-utils');
class ProductDetail extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
baseInfo(params) {
let options = {
data: {
method: 'app.product.data',
product_skn: params.productSkn
},
param: {
code: 200,
cache: true
}
};
return this.get(options).then(result => {
let brandId = _.get(result, 'data.brand_id');
let shopId = _.get(result, 'data.shop_id');
let optionsParams = {
data: {
method: 'app.product.queryShopsInfoById',
brand_id: brandId,
shop_id: shopId
},
param: {
cache: true
}
};
return this.get(optionsParams).then(data => {
if (result.data) {
result.data.enterStore = _.get(data, 'data');
}
return result;
});
});
}
intro(params) {
let options = {
data: {
method: 'app.product.intro',
product_skn: params.productSkn,
app_version: '5.6.0'
},
param: {
cache: true
}
};
return this.get(options).then(result => {
result = _.isString(result) ? result : '';
result = $.load(result);
result = result('#productDesc');
return (result.html() || '');
});
}
preference(params) {
let options = {
data: {
method: 'app.product.preference',
product_skn: params.productSkn
},
param: {
cache: true
}
};
return this.get(options).then(result => {
return result;
});
}
promotion(params) {
let options = {
data: {
method: 'app.product.promotion',
product_skn: params.productSkn
},
param: {
cache: true
}
};
return this.get(options).then(result => {
return result;
});
}
index(params) {
return Promise.all([
this.baseInfo(params),
this.intro(params),
this.preference(params),
this.promotion(params)
]).then(result => {
let resu = {
baseInfo: {},
intro: '',
preference: '',
promotion: [],
enterStore: [],
shoppingCount: '',
title: '',
mipCss: ''
};
let localCss = [];
if (_.get(result, '[0].data')) {
result[0].data.sales_price = result[0].data.sales_price.toFixed(2);
result[0].data.market_price = result[0].data.market_price.toFixed(2);
if (result[0].data.sales_price === result[0].data.market_price) {
result[0].data.market_price = 0;
}
resu.baseInfo = result[0].data;
let picBuild = [];
let build = [];
let tags = {};
_.forEach(result[0].data.tags, (value) => {
tags[value] = value;
});
resu.baseInfo.tags = tags;
_.forEach(result[0].data.goods_list, (value) => {
_.forEach(value.images_list, (item) => {
picBuild.push({
image_url: item.image_url
});
});
});
resu.baseInfo.images_list = picBuild;
_.forEach(result[0].data.enterStore, function(value) {
build.push({
storeName: value.brand_name,
url: `/shop/${value.brand_domain}-${value.shop_id}.html`,
img: value.brand_ico
});
});
resu.enterStore = build;
let brandName = '';
if (_.get(resu, 'baseInfo.brand_info.brand_name')) {
brandName = resu.baseInfo.brand_info.brand_name;
}
resu.title = `【${brandName}${resu.baseInfo.middle_sort_name}】${resu.baseInfo.product_name}`;
}
if (_.get(result, '[1]')) {
result[1] = result[1].replace(/http:/g, `${params.protocol}:`);
resu.intro = mipUtils.process(result[1]).mipHtml;
localCss.push(mipUtils.process(result[1]).css);
}
if (_.get(result, '[2]')) {
result[2] = result[2].replace(/http:/g, `${params.protocol}:`);
// 处理a标签
let option = {
mipLink: true
};
let resultHtml = _.isString(result[2]) ? result[2] : '';
let goodsContainer = $.load(resultHtml)('#shop-goods-container');
let goodThumb = goodsContainer.find('.good-thumb');
let nameA = goodsContainer.find('.name a');
_.forEach(goodThumb, function(value) {
let href = $(value).attr('href').split('?')[0];
let skn = $(value).closest('.good-info').data('id');
if (skn) {
href = `${params.protocol}://m.yohobuy.com/mip/product/${skn}.html`;
}
$(value).attr('href', href);
});
_.forEach(nameA, function(value) {
let href = $(value).attr('href').split('?')[0];
let skn = $(value).closest('.good-info').data('id');
if (skn) {
href = `${params.protocol}://m.yohobuy.com/mip/product/${skn}.html`;
}
$(value).attr('href', href);
});
resu.preference = mipUtils.process(goodsContainer.html(), option).mipHtml;
}
if (_.get(result, '[3].data')) {
resu.promotion = result[3].data;
}
resu.mipCss = localCss.join('');
return resu;
});
}
}
module.exports = ProductDetail;