Authored by htoooth

refactor async

... ... @@ -8,10 +8,12 @@ const log = require(`${library}/logger`);
const api = new API();
module.exports.getProductBannerAsync = function(pid) {
module.exports.getProductBannerAsync = function(pid, clientType) {
clientType = clientType || 'web';
return api.get('', sign.apiSign({
method: 'web.productBanner.data',
product_id: pid
product_id: pid,
client_type: clientType
})).catch(log.error);
};
... ...
... ... @@ -30,8 +30,9 @@ const HeaderModel = require('../../../doraemon/models/header');
const BLANK_STR = ' ';
const multiResourcesUrl = {};
const setMultiResourceByProductBaseInfo = (data) => {
const cachedRequestData = {};
const getProductAdditionInfoByProductBaseInfoAsync = (data) => {
return co(function * () {
let productId = data.id;
let uid = data.uid ? data.uid : 0;
... ... @@ -42,37 +43,38 @@ const setMultiResourceByProductBaseInfo = (data) => {
// 获取相关url
let result = yield Promise.all([
productAPI.getProductBannerAsync(productId, 'web', isOnlyUrl),
productAPI.sizeInfoAsync(skn, isOnlyUrl),
productAPI.getProductComfortAsync(productId, isOnlyUrl),
productAPI.getProductModelCardAsync(productId, isOnlyUrl),
productAPI.getProductModelTryAsync(skn, isOnlyUrl),
brandService.getBannerInfoAsync(brandId, isOnlyUrl)
productAPI.getProductBannerAsync(productId, 'web'),
productAPI.sizeInfoAsync(skn),
productAPI.getProductComfortAsync(productId),
productAPI.getProductModelCardAsync(productId),
productAPI.getProductModelTryAsync(skn),
brandService.getBannerInfoAsync(brandId)
]);
multiResourcesUrl['ItemData::getProductBanner'] = result[0];
multiResourcesUrl['ItemData::sizeInfo'] = result[1];
multiResourcesUrl['ItemData::getProductComfort'] = result[2];
multiResourcesUrl['ItemData::getProductModelCard'] = result[3];
multiResourcesUrl['ItemData::getProductModelTry'] = result[4];
multiResourcesUrl['BrandData::getBannerInfo'] = result[5];
cachedRequestData['ItemData::getProductBanner'] = result[0];
cachedRequestData['ItemData::sizeInfo'] = result[1];
cachedRequestData['ItemData::getProductComfort'] = result[2];
cachedRequestData['ItemData::getProductModelCard'] = result[3];
cachedRequestData['ItemData::getProductModelTry'] = result[4];
cachedRequestData['BrandData::getBannerInfo'] = result[5];
if (uid) {
multiResourcesUrl['FavoriteData::getUidProductFav'] =
cachedRequestData['FavoriteData::getUidProductFav'] =
yield favoriteProductService.isFavoriteAsync(uid, productId, isOnlyUrl);
}
return null;
})();
};
function getMultiDataByResourceName(resourceName) {
return multiResourcesUrl[resourceName] || false;
function getCacheDataByName(resourceName) {
return cachedRequestData[resourceName] || false;
}
/**
* 获取商品的喜欢
*/
const getProductFavoriteData = (uid, pid, bid) => {
const getProductFavoriteDataAsync = (uid, pid, bid) => {
return co(function*() {
let result = {
... ... @@ -82,7 +84,7 @@ const getProductFavoriteData = (uid, pid, bid) => {
if (uid) {
if (pid) {
let productData = getMultiDataByResourceName('FavoriteData::getUidProductFav');
let productData = getCacheDataByName('FavoriteData::getUidProductFav');
if (!productData) {
productData = yield favoriteProductService.isFavoriteAsync(uid, pid);
... ... @@ -177,16 +179,14 @@ const getVipDataByProductBaseInfo = (data, vipLevel, uid) => {
return vipData;
};
function getProductActivityBannerAsync(pid) {
return co(function*() {
function getProductActivityBanner() {
let result = {};
let data = getMultiDataByResourceName('ItemData::getProductBanner');
let data = getCacheDataByName('ItemData::getProductBanner');
if (!data) {
data = yield productAPI.getProductBannerAsync(pid);
return result;
}
if (data.code && data.code === 200 && data.data) {
... ... @@ -195,15 +195,12 @@ function getProductActivityBannerAsync(pid) {
}
return result;
})();
}
const getActivityDataByProductBaseInfo = (data) => {
return co(function *() {
let result = [];
let activityBanner = yield getProductActivityBannerAsync(data.id);
let activityBanner = getProductActivityBanner();
if (activityBanner) {
result.push(activityBanner);
... ... @@ -219,7 +216,6 @@ const getActivityDataByProductBaseInfo = (data) => {
}
return result;
})();
};
/**
... ... @@ -232,7 +228,7 @@ const getConsultCommentDataByProductInfo = (data) => {
let consultComment = {};
consultComment.consultNum = 0;
consultComment.captchaUrl = helpers.urlFormat('/passport/images', {
consultComment.captchaUrl = helpers.urlFormat('passport/images', {
t: moment().valueOf()
});
... ... @@ -244,7 +240,7 @@ const getConsultCommentDataByProductInfo = (data) => {
consultComment.commentNum = 0;
if (data.commentBoWrapper) {
consultComment.comments = [];
consultComment.commentUrl = helpers.urlFormat('/home/commnet');
consultComment.commentUrl = helpers.urlFormat('home/commnet');
}
return consultComment;
... ... @@ -255,9 +251,6 @@ const getConsultCommentDataByProductInfo = (data) => {
* 获取品牌数据
*/
const getBrandDataByProductBaseInfo = (data) => {
return co(function *() {
if (!data.brand) {
return {};
}
... ... @@ -270,10 +263,10 @@ const getBrandDataByProductBaseInfo = (data) => {
let bannerInfo = null;
let result = getMultiDataByResourceName('BrandData::getBannerInfo');
let result = getCacheDataByName('BrandData::getBannerInfo');
if (!result) {
result = yield brandService.getBannerInfoAsync(brandId);
return {};
}
if (data.brand.brandIco) {
... ... @@ -310,7 +303,6 @@ const getBrandDataByProductBaseInfo = (data) => {
homeUrl: homeUrl,
isCollect: false
};
})();
};
/**
... ... @@ -506,7 +498,6 @@ const getFashionTopGoodsStatus = (uid, showStatus, isBeginSale) => {
/**
* 获取分类导航列表
*/
// controller item.php 32;
function getSortNavAsync(smallSortId, gender) {
return co(function*() {
... ... @@ -562,10 +553,7 @@ const detailDataPkg = (origin, uid, vipLevel) => {
result.maxSortId = origin.maxSortId;
result.smallSortId = origin.smallSortId;
result.promotionId = origin.isPromotion;
result.goCartUrl = helpers.urlFormat('/shopping/cart');
// 接口处理数据,设置并发请求数据
yield setMultiResourceByProductBaseInfo(origin);
result.goCartUrl = helpers.urlFormat('shopping/cart');
let brandId = 0;
... ... @@ -573,8 +561,12 @@ const detailDataPkg = (origin, uid, vipLevel) => {
brandId = origin.brand.id;
}
// 处理收藏喜欢数据
let favoriteData = yield getProductFavoriteData(uid, result.productId, brandId);
let requestData = yield Promise.all([
getProductAdditionInfoByProductBaseInfoAsync(origin), // 接口处理数据,设置并发请求数据
getProductFavoriteDataAsync(uid, result.productId, brandId) //处理收藏喜欢数据
]);
let favoriteData = requestData[1];
// 商品标签
result.tags = getTagsDataByProductInfo(origin);
... ... @@ -599,7 +591,7 @@ const detailDataPkg = (origin, uid, vipLevel) => {
result.vipPrice = getVipDataByProductBaseInfo(origin, vipLevel, uid);
// 促销活动banner
result.activity = yield getActivityDataByProductBaseInfo(origin);
result.activity = getActivityDataByProductBaseInfo(origin);
const C_VALUE = {
type: '返YOHO币',
... ... @@ -632,7 +624,7 @@ const detailDataPkg = (origin, uid, vipLevel) => {
result.brandImg = helpers.image(origin.brand.brandIco, 47, 47);
result.brandName = origin.brand.brandName;
result.brandUrl = helpers.urlFormat('', {}, origin.brand.brandDomain);
banner = yield getBrandDataByProductBaseInfo(origin);
banner = getBrandDataByProductBaseInfo(origin);
if (banner.isCollect && favoriteData.brand) {
banner.isCollect = favoriteData.brand;
}
... ... @@ -661,7 +653,6 @@ const detailDataPkg = (origin, uid, vipLevel) => {
let fashTopGoods = getFashionTopGoodsStatus(uid, showStatus, isBeginSale);
result.fashTopGoods = {
getLimitedCode: fashTopGoods.getLimitedCode, // 限购码状态
hasLimitedCode: fashTopGoods.hasLimitedCode, // 是否已经获取限购码
... ... @@ -702,7 +693,7 @@ const detailDataPkg = (origin, uid, vipLevel) => {
} else if (virtualGoods) {
// 虚拟商品
result.buyNow = true; // 是否立即购买
result.buyNowBase = helpers.urlFormat('/ticket', {}, 'shopping');
result.buyNowBase = helpers.urlFormat('ticket', {}, 'shopping');
result.virtualGoods = virtualGoods;
if (result.salePrice) {
result.advancePrice = result.salePrice; // 先行价格
... ... @@ -723,7 +714,6 @@ const detailDataPkg = (origin, uid, vipLevel) => {
result.shareImg = result.img;
result.shareDesc = result.phrase;
// 统计需要的商品信息
let statGoodsInfo = {};
... ... @@ -780,15 +770,14 @@ const detailDataPkg = (origin, uid, vipLevel) => {
/**
* 获取商品的舒适度
*/
function getProductComfortAsync(productId) {
return co(function*() {
function getProductComfort() {
let result = [];
let comfort = getMultiDataByResourceName('ItemData::getProductComfort');
let comfort = getCacheDataByName('ItemData::getProductComfort');
if (!comfort) {
comfort = yield favoriteBrandService.getProductComfortAsync(productId);
return result;
}
if (comfort.data) {
... ... @@ -822,13 +811,10 @@ function getProductComfortAsync(productId) {
}
return result;
})();
}
function getDescriptionDataBySizeInfoAsync(sizeInfo) {
return co(function *() {
function getDescriptionDataBySizeInfo(sizeInfo) {
let description = {};
if (sizeInfo.productDescBo.erpProductId) {
... ... @@ -871,11 +857,10 @@ function getDescriptionDataBySizeInfoAsync(sizeInfo) {
});
}
description.confort = yield getProductComfortAsync(sizeInfo.productBo.id);
description.confort = getProductComfort();
}
return description;
})();
}
/**
... ... @@ -1012,15 +997,14 @@ function getSizeDataBySizeInfo(sizeInfo) {
* 获取商品模特卡
* @param productId
*/
function getProductModelCardAsync(productId) {
return co(function*() {
function getProductModelCard() {
let result = [];
let data = getMultiDataByResourceName('ItemData::getProductModelCard');
let data = getCacheDataByName('ItemData::getProductModelCard');
if (!data) {
data = yield productAPI.getProductModelCardAsync(productId);
return result;
}
if (data.code && data.code === 200) {
... ... @@ -1034,7 +1018,6 @@ function getProductModelCardAsync(productId) {
}
return result;
})();
}
/**
... ... @@ -1155,14 +1138,13 @@ function getDetailDataBySizeInfo(sizeInfo) {
* 获取模特试穿
* @param productSkn
*/
function getProductModelTryAsync(productSkn) {
return co(function*() {
function getProductModelTry() {
let modelTry = {};
let result = getMultiDataByResourceName('ItemData::getProductModelTry');
let result = getCacheDataByName('ItemData::getProductModelTry');
if (!result) {
result = yield productAPI.getProductModelTryAsync(productSkn);
return modelTry;
}
if (result.code && result.code === 200 && result.data) {
... ... @@ -1195,7 +1177,6 @@ function getProductModelTryAsync(productSkn) {
}
return modelTry;
})();
}
/**
... ... @@ -1270,35 +1251,34 @@ function getSizeAttrByMaxSortId(maxSortId, sizeList) {
* @param int $maxSortId
* @return array
*/
const getSizeInfoAsync = (productSkn, maxSortId)=> {
return co(function*() {
const getSizeInfo = (productSkn, maxSortId)=> {
let result = {};
if (productSkn) {
// 并发资源中是否存在数据
let sizeInfo = getMultiDataByResourceName('ItemData::sizeInfo');
if (!sizeInfo) {
// 获取尺码
sizeInfo = yield productAPI.sizeInfoAsync(productSkn);
}
let sizeInfo = getCacheDataByName('ItemData::sizeInfo');
if (!sizeInfo) {
return result;
}
let productId = sizeInfo.productBo.id;
// 描述数据
result.description = yield getDescriptionDataBySizeInfoAsync(sizeInfo);
result.description = getDescriptionDataBySizeInfo(sizeInfo);
// 模特卡
result.modelCards = getProductModelCard();
// 试穿模特
let fittingReport = getProductModelTry();
if (!_.isEmpty(fittingReport)) {
result.fittingReport = fittingReport;
}
// 尺寸数据
result.size = getSizeDataBySizeInfo(sizeInfo);
// 模特卡
result.modelCards = yield getProductModelCardAsync(productId);
// 模特数据
result.reference = getReferenceDataBySizeInfo(sizeInfo);
... ... @@ -1308,20 +1288,12 @@ const getSizeInfoAsync = (productSkn, maxSortId)=> {
// 商品详情页介绍
result.details = getDetailDataBySizeInfo(sizeInfo);
// 试穿模特
let fittingReport = yield getProductModelTryAsync(productSkn);
if (!_.isEmpty(fittingReport)) {
result.fittingReport = fittingReport;
}
// 获取尺寸说明
result.sizeTitleJson =
sizeInfo.sizeInfoBo ? JSON.stringify(getSizeAttrByMaxSortId(maxSortId, sizeInfo.sizeInfoBo)) : '';
}
return result;
})();
};
/**
... ... @@ -1346,21 +1318,32 @@ module.exports.showMainAsync = (data) => {
return co(function * () {
// 获取商品信息
let productInfo = yield productAPI.getProductInfo(data.id);
let result = {};
productInfo = yield detailDataPkg(productInfo, data.uid, data.vipLevel);
let currentUserProductInfo = _.curry(detailDataPkg)(_, data.uid, data.vipLevel);
// 获取商品信息
let productInfo = yield productAPI.getProductInfo(data.id).then(currentUserProductInfo);
// 获取商品尺寸相关
let sizeInfo = yield getSizeInfoAsync(productInfo.goodsInfo, productInfo.goodsInfo.maxSortId);
let requestData = yield Promise.all([
getSortNavAsync(productInfo.goodsInfo.smallSortId, data.gender),
HeaderModel.requestHeaderData()
]);
let navs = yield getSortNavAsync(productInfo.goodsInfo.smallSortId, data.gender);
let sizeInfo = getSizeInfo(productInfo.goodsInfo, productInfo.goodsInfo.maxSortId);
let result = {
productDetailPage: true,
detail: Object.assign(productInfo, sizeInfo),
statGoodsInfo: Object.assign({fullSortName: navs.map(x => x.name).join('-')}, productInfo.statGoodsInfo)
};
let navs = requestData[0];
result.headerData = requestData[1];
result.productDetailPage = true;
result.detail = Object.assign(productInfo, sizeInfo);
result.statGoodsInfo = Object.assign({
fullSortName: navs.map(x => x.name).join('-')
},
productInfo.statGoodsInfo
);
// 导航
result.detail.pathNav = _.concat(
... ... @@ -1370,8 +1353,6 @@ module.exports.showMainAsync = (data) => {
);
result.detail.lastWalk = 5;
result.headerData = yield HeaderModel.requestHeaderData();
return result;
})();
};
... ...