redshop-process.js
9.31 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
/*
* @Author: Targaryen
* @Date: 2017-03-23 11:02:31
* @Last Modified by: Targaryen
* @Last Modified time: 2017-04-18 16:28:28
*/
/* 红人店铺数据处理 */
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const productPrcs = require('./product-process');
let countCarouselImage = 0; // 轮播图模块统计
/**
* 将商品转化成以 product_skn 为键名的对象
* @param {*} goodsArray
*/
const _goodsArrayToObj = (goodsArray) => {
return _.keyBy(goodsArray, value => {
return value.product_skn;
});
};
/**
* 链接处理
* @param {*} linkParent
*/
const _linkhandle = (linkParent) => {
if (linkParent.linkType) {
switch (parseInt(linkParent.linkType, 10)) {
case 0:
return helpers.urlFormat('', {
categoryId: linkParent.resource
}, 'list');
case 1:
return helpers.urlFormat('/product/' + linkParent.resource + '.html');
case 2:
return linkParent.resource;
default:
break;
}
} else {
return false;
}
};
/**
* 图片处理
* @param {*} moduleData
*/
const _picsHandle = (moduleData) => {
let pics = [];
_.forEach(_.get(moduleData, 'data', []), value => {
let showProductInfo = false;
if (_.has(value, 'showProductInfo')) {
showProductInfo = value.showProductInfo;
}
pics.push({
src: value.pic,
link: _linkhandle(value),
text: value.text,
showProductInfo: showProductInfo,
skn: _.parseInt(value.linkType) === 1 ? value.resource : ''
});
});
return pics;
};
/**
* 红人店铺楼层数据处理工具
*/
const _tools = {
/**
* 模块标题
* @param {*} moduleData
*/
title(moduleData) {
return {
module_type: 'Title',
text: _.get(moduleData, 'data[0].text', ''),
isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0),
link: _linkhandle(_.get(moduleData, 'data[0]', 0))
};
},
/**
* 视频模块
* @param {*} moduleData
*/
video(moduleData) {
return {
module_type: 'Video',
title: _.get(moduleData, 'data[0].text.title', ''),
content: _.get(moduleData, 'data[0].text.content', ''),
pic: _.get(moduleData, 'data[0].pic', ''),
isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0),
};
},
/**
* 轮播图
* @param {*} moduleData
*/
carouselImage(moduleData) {
return {
module_type: 'CarouselImage',
num: countCarouselImage,
pics: _picsHandle(moduleData),
isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0),
};
},
/**
* 一张图片
* @param {*} moduleData
*/
singleImage(moduleData) {
return {
module_type: 'SingleImage',
pics: _picsHandle(moduleData),
text: _.get(moduleData, 'data[0].text', ''),
width: _.get(moduleData, 'properties.width', ''),
height: _.get(moduleData, 'data[0].text', ''),
moduleHeight: _.get(moduleData, 'properties.moduleHeight', ''),
isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0),
};
},
/**
* 两张图片
* @param {*} moduleData
*/
doubleImage(moduleData) {
return {
module_type: 'DoubleImage',
pics: _picsHandle(moduleData),
isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0),
};
},
/**
* 三张图片
* @param {*} moduleData
*/
tripleImage(moduleData) {
let displayType = _.get(moduleData, 'properties.displayType', 0);
return {
module_type: 'TripleImage',
pics: _picsHandle(moduleData),
isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0),
displayType: parseInt(displayType, 10),
};
},
/**
* 四张图片
* @param {*} moduleData
*/
fourImage(moduleData) {
return {
module_type: 'FourImage',
pics: _picsHandle(moduleData),
isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0)
};
},
/**
* 商品列表
* @param {*} moduleData
*/
recommentProduct(moduleData) {
let displayType = _.parseInt(_.get(moduleData, 'properties.displayType', 0));
let result = {
module_type: 'RecommentProduct',
type: displayType
};
let skns = '';
switch (displayType) {
case 0:
_.assign(result, {
order: _.get(moduleData, 'data[0].order', 'p_d_asc')
});
break;
case 1:
_.forEach(_.get(moduleData, 'data[0].sknList', []), value => {
skns += value + ',';
});
_.assign(result, {
skns: skns
});
break;
default:
break;
}
return {
module_type: 'RecommentProduct',
skns: skns,
};
}
};
/**
* 处理红人店铺楼层
* @param {*} decoratorsData
*/
const floor = (decoratorsData) => {
let decorators = [];
let skns;
countCarouselImage = 0;
_.forEach(decoratorsData, value => {
try {
value.module_data = JSON.parse(value.module_data);
} catch (error) {
console.log(error);
}
switch (value.module_type) {
case 'Title':
decorators.push(_tools.title(value.module_data));
break;
// case 'Video':
// decorators.push(_tools.video(value.module_data));
// break;
case 'CarouselImage':
countCarouselImage++;
decorators.push(_tools.carouselImage(value.module_data));
break;
case 'SingleImage':
decorators.push(_tools.singleImage(value.module_data));
break;
case 'DoubleImage':
decorators.push(_tools.doubleImage(value.module_data));
break;
case 'TripleImage':
decorators.push(_tools.tripleImage(value.module_data));
break;
case 'FourImage':
decorators.push(_tools.fourImage(value.module_data));
break;
case 'SknList':
skns = _.get(value, 'module_data.data').join(',');
break;
case 'RecommentProduct':
decorators.push(_tools.recommentProduct(value.module_data));
break;
default:
break;
}
});
return {
decorators: decorators,
skns: skns
};
};
/**
* 店铺介绍处理
* @param {*} params
*/
const shopIntro = (params) => {
if (params) {
params.shop_intro_link = helpers.urlFormat('/product/index/intro', {shop_id: params.shops_id});
}
return params;
};
/**
* 店铺分类处理
* @param {*} params
*/
const category = (params, shopId) => {
let categoryData = {
list: [],
url: ''
};
if (!params) {
return categoryData;
}
if (params.length > 6) {
categoryData.url = helpers.urlFormat('/product/index/category', {
shop_id: shopId
});
} else {
_.forEach(params, value => {
categoryData.list.push({
url: helpers.urlFormat('', {
shop_id: shopId,
sort: (value.relation_parameter && value.relation_parameter.sort) || ''
}, 'search'),
categoryId: value.category_id,
name: value.category_name
});
});
}
return categoryData;
};
/**
* 加入商品价格等数据
* @param {*} decorators
* @param {*} goodsList
*/
const pushGoodsInfo = (decorators, goodsList) => {
let goodsObj = _goodsArrayToObj(productPrcs.processProductList(goodsList));
_.forEach(decorators, (value, key) => {
_.forEach(_.get(value, 'pics', []), (subValue, subKey) => {
if (subValue.skn) {
let salesPrice = _.get(goodsObj, `${subValue.skn}.sales_price`, '');
let marketPrice = _.get(goodsObj, `${subValue.skn}.market_price`, '');
let imageSrc = _.get(goodsObj, `${subValue.skn}.default_images`, '');
decorators[key].pics[subKey].name = _.get(goodsObj, `${subValue.skn}.product_name`, '');
decorators[key].pics[subKey].salesPrice = salesPrice ? '¥' + salesPrice : '';
decorators[key].pics[subKey].marketPrice = marketPrice ? '¥' + marketPrice : '';
decorators[key].pics[subKey].isGood = true;
if (value.module_type === 'TripleImage') {
decorators[key].pics[subKey].src = imageSrc;
} else {
decorators[key].pics[subKey].src = imageSrc.split('?')[0];
}
}
});
});
return decorators;
};
module.exports = {
floor,
shopIntro,
category,
pushGoodsInfo
};