item.js
5.22 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
/**
* 商品详情页model
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2016/7/11
*/
'use strict';
const _ = require('lodash');
const itemAPI = require('./item-api');
const helpers = global.yoho.helpers;
// const getForceSourceUrl = (img) => {
// if (img) {
// img = img.split('\?imageView')[0];
// img = img.split('\?imageMogr2')[0];
// }
// return img;
// };
/**
* 设置品牌banner数据
* @param brand 品牌相关数据
* @return Object
*/
const _setBrandBanner = (brand) => {
let data = {
bgColor: '#93897d',
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}`,
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) {
group.focus = 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;
};
const _setBrandIntro = (brand) => {
let barndIntro = {};
if (brand) {
barndIntro = {
brand: {
titleEn: 'BRAND',
titleCn: '品牌介绍',
logo: brand.brandIco,
intro: brand.brandIntro
}
}
}
return barndIntro;
}
let getProductItemData = (params) => {
let pid = params[0];
// let gid = params[1];
return Promise.all([
itemAPI.getProductBaseAsync(pid)
]).then(result => {
let data = {};
if (!result[0].productName &&
!result[0].erpProductId &&
!result[0].productPriceBo) {
return data;
}
if (result[0].brand) {
Object.assign(data, _setBrandBanner(result[0].brand));
Object.assign(data, _setBrandIntro(result[0].brand));
}
Object.assign(data, {
goodInfo: _setProductData(result[0])
});
console.log(data);
return data;
});
};
module.exports = {
getProductItemData
};