item-handler.js
15.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/**
* 商品详情页model
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2016/7/11
*/
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
/**
* 使sizeBoList id以 sizeAttributeBos id顺序一样
* @param sizeInfoBo
*/
const _sizeInfoBoSort = sizeInfoBo => {
if (!sizeInfoBo.sizeBoList || !sizeInfoBo.sizeAttributeBos) {
return {};
}
// TODO: 这里的排序代码很乱
_.forEach(sizeInfoBo.sizeBoList, (sizeBoList, sizek)=> {
let sortAttr = {};
_.forEach(sizeBoList.sortAttributes, sortAttributes => {
sortAttr[sortAttributes.id] = sortAttributes;
});
sizeInfoBo.sizeBoList[sizek].sortAttributes = sortAttr;
});
_.forEach(sizeInfoBo.sizeBoList, (sizeBoList, sizek)=> {
let sortAttr = [];
_.forEach(sizeInfoBo.sizeAttributeBos, val => {
sortAttr.push(sizeBoList.sortAttributes[val.id]);
});
sizeInfoBo.sizeBoList[sizek].sortAttributes = sortAttr;
});
return sizeInfoBo;
};
const setPathNav = (data, name) => {
let resData = {};
if (!_.isEmpty(data)) {
let navs = [];
let sort = data.sort[0];
navs.push({
link: helpers.urlFormat('/list', {
msort: sort.sort_id
// gender: '' // 待处理
}),
name: sort.sort_name,
pathTitle: sort.sort_name
});
// 二级分类
if (!_.isEmpty(sort.sub)) {
navs.push({
link: helpers.urlFormat('/list', {
msort: sort.sort_id,
misort: sort.sub.sort_id
// gender: '' // 待处理
}),
name: sort.sub[0].sort_name,
pathTitle: sort.sub[0].sort_name
});
}
navs.push({
name: name,
pathTitle: name
});
resData.nav = navs;
}
return resData;
};
/**
* 设置品牌banner数据
* @param brand 品牌相关数据
* @return Object
*/
const setBrandBanner = brand => {
let data = {
bgColor: '#000',
brandLogo: {
link: `#${brand.brandDomain}`, // 品牌跳转链接 -- 待处理
img: brand.brandIco
},
brandHome: {
link: `#${brand.brandDomain}` // 品牌跳转链接 -- 待处理
}
};
return {
brandBanner: data
};
};
/**
* 设置商品数据
* @param base 商品基础数据
* @return Object
*/
const setProductData = base => {
let data = {
name: base.productName,
brandName: _.has(base, 'brand.brandName') ? base.brand.brandName : '',
intro: base.salesPhrase,
sellPrice: base.productPriceBo.salesPrice,
marketPrice: base.productPriceBo.marketPrice,
total: 0
};
let chooseSkuFlag;
if (_.toNumber(data.sellPrice) >= _.toNumber(data.marketPrice)) {
// 售价与吊牌价相同,只显示售价
_.unset(data, 'marketPrice');
}
// 遍历颜色尺寸
if (base.goodsList) {
let goods = [];
// 处理商品数据
_.forEach(base.goodsList, function(value) {
let group = {},
thumbs = [],
sizes = [];
// 如果status为0,即skc下架时就跳过该商品
if (!value.status) {
return;
}
if (value.goodsImagesList) {
group = {
name: value.colorName,
title: `${data.name} ${value.colorName}`,
color: value.colorName,
total: 0
};
// 目前没有RGB值先以背景图方式实现
group.rgb = `url('${helpers.image(value.colorImage, 30, 30)}')`;
// 商品颜色列表
_.forEach(value.goodsImagesList, function(subValue) {
thumbs.push(subValue.imageUrl);
});
group.thumbs = thumbs;
// 缩略图空,不显示
if (!thumbs.length) {
return;
}
// 默认第一张图片
if (!_.has(data, 'img')) {
data.img = value.colorImage;
}
// 商品尺码列表
_.forEach(value.goodsSizeBoList, function(subValue) {
let size = {
name: subValue.sizeName,
title: subValue.sizeName,
sku: subValue.goodsSizeSkuId,
num: _.toInteger(subValue.goodsSizeStorageNum),
goodsId: subValue.goodsId
};
// 虚拟商品,增加为一件
if (subValue.attribute === 3) {
size.num = size.num > 1 ? 1 : 0;
}
// 如果status为0,即skc下架时,则库存设为0
if (subValue.status === 0) {
size.num = 0;
}
// 单个sku商品的总数累加
group.total += size.num;
// 默认选中该sku商品
if (group.total > 0 && !chooseSkuFlag) {
data.colorName = group.name;
group.cur = true; // 选中sku商品
chooseSkuFlag = true;
}
// 商品的总数累加
data.total += size.num;
sizes.push(size);
});
group.sizes = sizes;
}
goods.push(group);
});
if (goods.length && !chooseSkuFlag) {
goods[0].focus = true;
}
data.colors = goods;
}
return data;
};
/**
* 设置品牌介绍
* @param brand 品牌相关数据
* @return Object
*/
const setBrandIntro = brand => {
let barndIntro = {};
if (brand) {
barndIntro = {
brand: {
titleEn: 'BRAND',
titleCn: '品牌介绍',
logo: brand.brandIco,
intro: brand.brandIntro
}
};
}
return barndIntro;
};
/**
* 设置商品描述
* @param sizeInfo
* @param comfortInfo
* @return Object
*/
const setDescriptionData = (sizeInfo, comfortInfo) => {
let resData = {};
if (_.has(sizeInfo, 'productDescBo.erpProductId')) {
let description = {
titleEn: 'DESCRIPTION',
titleCn: '商品信息'
};
let sex;
switch (sizeInfo.productDescBo.gender) {
case 1:
sex = '男款';
break;
case 2:
sex = '女款';
break;
default:
sex = '通用';
break;
}
let basic = [
{ key: '编号', value: sizeInfo.productDescBo.erpProductId },
{ key: '颜色', value: sizeInfo.productDescBo.colorName },
{ key: '性别', value: sex }
];
if (_.isArray(sizeInfo.productDescBo.standardBos)) {
_.forEach(sizeInfo.productDescBo.standardBos, value => {
basic.push({
key: value.standardName,
value: value.standardVal
});
});
}
description.basic = basic;
if (comfortInfo) {
let comfort = [];
_.forEach(comfortInfo, value => {
let i = 1;
let blocks = [];
let flag = false;
do {
if (i === +value.wearSense.value) {
flag = true;
blocks.push({
cur: true
});
} else {
blocks.push({});
}
i++;
} while (i <= 5);
if (flag) {
comfort.push({
name: value.caption.caption,
minDes: value.caption.low,
blocks: blocks,
maxDes: value.caption.high
});
}
});
description.comfort = comfort;
}
resData = {
description: description
};
}
return resData;
};
/**
* 设置洗涤材质信息
* @param sizeInfo
* @return Object
*/
const setMaterialData = sizeInfo => {
let resData = {};
let material = {};
// 洗涤提示
if (sizeInfo.washTipsBoList) {
let wash = [];
_.forEach(sizeInfo.washTipsBoList, value => {
wash.push({
name: value.caption,
img: value.img
});
});
if (!_.isEmpty(wash)) {
material.wash = wash;
}
}
// 商品材质[洗涤说明]
if (sizeInfo.productMaterialList) {
let detail = [];
_.forEach(sizeInfo.productMaterialList, value => {
detail.push({
img: value.imageUrl,
name: value.caption,
enName: value.encaption,
text: value.remark
});
});
if (!_.isEmpty(detail)) {
material.detail = detail;
}
}
if (!_.isEmpty(material)) {
Object.assign(material, {
titleEn: 'MATERIALS',
titleCn: '材料洗涤'
});
resData.material = material;
}
return resData;
};
/**
* 设置尺寸信息
* @param sizeInfo
* @return Object
*/
const setSizeData = (sizeInfo) => {
let resData = {};
if (sizeInfo.sizeInfoBo) {
let referenceName,
boyReference,
girlReference,
gender;
let size = {
titleEn: 'SIZEINFO',
titleCn: '尺码信息'
};
sizeInfo.sizeInfoBo = _sizeInfoBoSort(sizeInfo.sizeInfoBo);
// 参考尺码
if (_.has(sizeInfo, 'productExtra')) {
boyReference = sizeInfo.productExtra.boyReference;
girlReference = sizeInfo.productExtra.girlReference;
}
gender = _.has(sizeInfo, 'productDescBo.gender') ? sizeInfo.productDescBo.gender : 3;
if (gender === 3 && boyReference) {
referenceName = '参考尺码(男)';
} else if (gender === 3 && girlReference) {
referenceName = '参考尺码(女)';
} else {
referenceName = '参考尺码';
}
// 判断是否显示参考尺码
let showReference = (boyReference && _.get(sizeInfo.sizeInfoBo, 'sizeBoList[0].boyReferSize', false)) ||
(girlReference && _.get(sizeInfo.sizeInfoBo, 'sizeBoList[0].girlReferSize', false));
if (sizeInfo.sizeInfoBo.sizeAttributeBos) {
let sizeTable = {};
// 尺码信息头部
sizeTable.thead = [{name: '吊牌尺码', id: '', width: 126}];
// 显示参考尺码
if (showReference) {
sizeTable.thead.push({name: referenceName, id: '', width: 126});
}
_.forEach(sizeInfo.sizeInfoBo.sizeAttributeBos, value => {
sizeTable.thead.push({
name: value.attributeName || ' ',
id: value.id,
width: 126
});
});
sizeTable.tbody = [];
_.forEach(sizeInfo.sizeInfoBo.sizeBoList, value => {
let sizes = [];
// 吊牌尺码
sizes.push(value.sizeName);
// 判断是否显示参考尺码
if (boyReference && (gender === 1 || gender === 3) && showReference) {
sizes.push(value.boyReferSize.referenceName || ' ');
} else if (girlReference && (gender === 2 || gender === 3) && showReference) {
sizes.push(value.girlReferSize.referenceName || ' ');
} else {
if (sizeTable.thead[1] && showReference) {
sizeTable.thead[1] = {};
}
}
// 其他尺码信息
value.sortAttributes.forEach(attr => {
sizes.push((attr.sizeValue || ' '));
});
// 尺码信息
sizeTable.tbody.push(sizes);
});
// 参考尺码为空
if (_.isEmpty(sizeTable.thead[1]) && showReference) {
// 移除这个值
sizeTable.thead.splice(1, 1);
}
size.param = sizeTable;
}
if (!_.isEmpty(sizeInfo.modelBos)) {
let tasteTable = {};
tasteTable.thead = [
{name: '模特', width: 170},
{name: '身高', width: 126},
{name: '体重', width: 126},
{name: '三围', width: 126},
{name: '吊牌尺码', width: 126},
{name: '试穿描述', width: 126}
];
tasteTable.tbody = [];
_.forEach(sizeInfo.modelBos, value => {
let modelInfo = [
value.avatar, value.modelName, value.height,
value.weight, value.vitalStatistics, value.fitModelBo.fit_size,
value.fitModelBo.feel
];
// 是否有备注
if (_.get(value, 'fitModelBo.fit_remark', false)) {
modelInfo.push(value.fitModelBo.fit_remark);
tasteTable.thead[7] = {name: '备注', width: 170};
}
tasteTable.tbody.push(modelInfo);
});
size.taste = tasteTable;
}
resData.sizeInfo = size;
}
return resData;
};
/**
* 设置商品详情页介绍
*
* @param array sizeInfo
* @return string
*/
const setDetailData = sizeInfo => {
let resData = {};
// 详情配图
if (_.has(sizeInfo, 'productIntroBo.productIntro')) {
let details = {
titleEn: 'DETAILS',
titleCn: '商品详情'
};
if (sizeInfo.productDescBo && sizeInfo.productDescBo.phrase) {
details.content = `${sizeInfo.productDescBo.phrase}<br/>`;
}
// 图片换规则
const replacePairs = [
{
reg: /<img src=/g,
str: '<img class="lazy" src="data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP///93d3f' +
'///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw==" data-original='
},
{
reg: /<img border="0" src=/g,
str: '<img border="0" class="lazy" src="data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP' +
'///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw==" data-original='
}
];
let content = sizeInfo.productIntroBo.productIntro;
// 图片换成懒加载方式
_.forEach(replacePairs, value => {
content = _.replace(content, value.reg, value.str);
});
details.content += content;
resData.details = details;
}
return resData;
};
module.exports = {
setBrandBanner,
setPathNav,
setProductData,
setBrandIntro,
setDescriptionData,
setMaterialData,
setSizeData,
setDetailData
};