item.js
5.49 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
/**
* 商品详情页model
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2016/7/11
*/
'use strict';
const _ = require('lodash');
const itemApi = require('./item-api');
const brandApi = require('./brand-api');
const shopApi = require('./shop-api');
const itemFun = require('./item-handler');
const search = require('./search-api');
/**
* 根据商品基本信息获取商品品牌、材质、尺码、描述、详情等
* @function _getMultiResourceByBaseInfo
* @param { Object } 商品的基本信息
* @return { Object } 返回单个商品品牌、材质、尺码、描述、详情等信息
*/
const _getMultiResourceByBaseInfo = (base) => {
const productId = base.product_id;
const skn = base.product_skn;
const brandId = base.brand_info.brand_id || 0;
const brandDomain = _.get(base, 'brand_info.brand_name', false);
const shopId = base.shop_id || 0;
let apiIndex = {};
// 获取相关数据
let promiseData = [
itemApi.getsizeInfoAsync(skn),
itemApi.getComfortAsync(productId),
itemApi.getModelTryAsync(skn),
search.getSortAsync({sort: base.smallSortId}),
itemApi.getBrandBannerAsync(brandId)
];
if (base.uid) {
apiIndex.productFav = promiseData.length;
promiseData.push(itemApi.getUserIsFav(base.uid, productId));
apiIndex.brandFav = promiseData.length;
promiseData.push(brandApi.getBrandInfo(brandId, base.uid));
}
// 根据品牌名获取店铺信息
if (brandDomain) {
apiIndex.domainInfo = promiseData.length;
promiseData.push(brandApi.getDomainInfo(brandDomain));
}
// 根据店铺ID获取店铺装修信息
if (shopId) {
apiIndex.shopDecorator = promiseData.length;
promiseData.push(shopApi.getShopDecorator(shopId));
}
return Promise.all(promiseData).then(result => {
return {
sizeInfo: result[0],
comfort: result[1].data,
modelTry: result[2].data,
sort: result[3].data,
brandBanner: result[4].data,
productFav: (result[apiIndex.productFav] && result[apiIndex.productFav].data),
brandFav: (_.get(result[apiIndex.brandFav], 'data.is_favorite', 'N') === 'Y'),
shopInfo: Object.assign(
{},
{type: _.get(result[apiIndex.domainInfo], 'data.type', 0)},
{list: _.get(result[apiIndex.shopDecorator], 'data.list', [])}
)
};
});
};
/**
* 获取商品基本信息、品牌、材质、尺码、描述、详情等
* @function getProductItemData
* @param { Object } 商品的参数
* @param { String } url 用户ID
* @param { String } uid 用户ID
* @return { Object } 返回单个商品的基本信息及品牌、材质、尺码、描述、详情等
*/
const getProductItemData = (params, url, uid) => {
let pid = params[0];
// let gid = params[1];
return itemApi.getProductBaseAsync(pid).then(result => {
let resData = {};
let data = {};
result = result.data;
if (!result) {
return;
}
// 如果status为0,即商品下架时则返回空对象
// if (!result.status) {
// return resData;
// }
if (!result.product_name &&
!result.product_skn &&
!result.sales_price &&
!result.market_price
) {
return resData;
}
result.uid = uid;
result.mainPath = `/product${url}`;
// 商品基本信息
data.goodInfo = itemFun.setProductData(result);
// BRAND品牌简介
if (result.brand_info) {
Object.assign(data, itemFun.setBrandIntro(result.brand_info));
}
return _getMultiResourceByBaseInfo(result).then(mulRes => {
Object.assign(data,
itemFun.setBrandBanner(result, mulRes.brandBanner, mulRes.shopInfo), // banner
itemFun.setPathNav(mulRes.sort, result.product_name, params.channel), // 面包屑导航
itemFun.setDescriptionData(mulRes.sizeInfo, mulRes.comfort), // DESCRIPTION商品描述
itemFun.setMaterialData(mulRes.sizeInfo), // MATERIALS材料洗涤
itemFun.setSizeData(mulRes.sizeInfo, mulRes.modelTry), // SIZEINFO尺码信息
itemFun.setDetailData(mulRes.sizeInfo) // DETAILS商品详情
);
// 收藏状态
_.set(data, 'goodInfo.productFav', mulRes.productFav);
if (data.brandBanner) {
_.set(data, 'brandBanner.brandFav', mulRes.brandFav);
}
resData.content = data;
// 商品详情SEO
Object.assign(resData, itemFun.setSeoInfo(data.goodInfo, data.nav));
return resData;
});
});
};
/**
* 获取商品尺寸,颜色,和缩略图
* @function getProductInfo
* @param { Number } productId 商品ID
* @param { Number } skn 商品skn
* @param { String } uid 用户ID
* @return { Object } 接口返回单个商品的基本信息
*/
const getProductInfo = (productId, skn, uid) => {
return itemApi.getProductBaseAsync(productId, uid, skn).then(result => {
return result.data && itemFun.setProductData(result.data) || {};
});
};
/**
* 是否支持退换货,true 支持,false 不支持
*/
const fetchReturn = (skn) => {
return itemApi.isSupportReturnedSale(skn).then(result => _.get(result, `data.${skn}`, 'N') === 'N' ? 'Y' : 'N');
};
module.exports = {
getProductItemData,
getProductInfo,
fetchReturn
};