...
|
...
|
@@ -5,4 +5,196 @@ |
|
|
*/
|
|
|
|
|
|
'use strict';
|
|
|
const helpers = global.yoho.helpers;
|
|
|
const api = global.yoho.API;
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
const indexData = (uid, shoppingKey, saleChannel, cartType) => {
|
|
|
return api.get('', {
|
|
|
method: 'app.Shopping.queryCart',
|
|
|
uid: uid,
|
|
|
shopping_key: shoppingKey,
|
|
|
sale_channel: saleChannel
|
|
|
}).then((data) => {
|
|
|
let cart = data.data;
|
|
|
let result = {};
|
|
|
let ordinaryCount = _.get(cart, 'ordinary_cart_data.shopping_cart_data.goods_count', 0);
|
|
|
let advanceCount = _.get(cart, 'advance_cart_data.shopping_cart_data.goods_count', 0);
|
|
|
let ordinarySoldOut = _.get(cart, 'ordinary_cart_data.sold_out_goods_list', []);
|
|
|
let advanceSoldOut = _.get(cart, 'advance_cart_data.sold_out_goods_list', []);
|
|
|
|
|
|
// 普通购物车和预售购物车都为空
|
|
|
if (ordinaryCount === 0 && advanceCount === 0 && !ordinarySoldOut.length && !advanceSoldOut.length) {
|
|
|
result.isEmptyCart = true;
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
// 普通购物车空,则显示预售购物车
|
|
|
if (ordinaryCount === 0 && !ordinarySoldOut.length) {
|
|
|
result.cartNav = false;
|
|
|
result.cartType = 'advance';
|
|
|
} // 预售购物车空,则显示普通购物车
|
|
|
else if (advanceCount === 0 && !advanceSoldOut.length) {
|
|
|
result.cartNav = false;
|
|
|
result.cartType = 'ordinary';
|
|
|
} // 以上两个购物车中都有数据, 默认显示普通购物车
|
|
|
else {
|
|
|
result.cartNav = true;
|
|
|
result.cartType = cartType || 'ordinary';
|
|
|
}
|
|
|
|
|
|
/* 普通购物车 */
|
|
|
result.commonGoodsCount = ordinaryCount;
|
|
|
result.commonCart = processData(cart.ordinary_cart_data, false);
|
|
|
/* 预售购物车 */
|
|
|
result.presellGoodsCount = advanceCount;
|
|
|
result.preSellCart = processData(cart.advance_cart_data);
|
|
|
console.log(result)
|
|
|
return result;
|
|
|
});
|
|
|
};
|
|
|
|
|
|
const processData = (data, isAdvanceCart) => {
|
|
|
let result = {};
|
|
|
|
|
|
// 购买的可用商品列表
|
|
|
result.goods = _.get(data, 'goods_list', []).map(good => { return formatCartGoods(good, isAdvanceCart); });
|
|
|
result.promotionPoolList = _.get(data, 'promotion_pool_list', []).map(promotion => {
|
|
|
return {
|
|
|
goods: _.get(promotion, 'goods_list', []).map(good => { return formatCartGoods(good, isAdvanceCart); }),
|
|
|
promotions: _.get(promotion, 'promotion_list', []).map(promo => {
|
|
|
return {
|
|
|
status: promo.status,
|
|
|
conditionUnit: promo.condition_unit,
|
|
|
conditionValue: promo.condition_value,
|
|
|
giftGoodsList: _.get(promo, 'gift_goods_List', []).map(gift => { return formatAdvanceGoods(gift); }),
|
|
|
giftPrice: promo.gift_price,
|
|
|
promotionId: promo.promotion_id,
|
|
|
promotionTitle: promo.promotion_title,
|
|
|
promotionType: promo.promotion_type,
|
|
|
alreadyMatch: promo.alreadyMatch
|
|
|
};
|
|
|
})
|
|
|
};
|
|
|
});
|
|
|
|
|
|
// 失效商品列表
|
|
|
result.notValidGoods = _.get(data, 'sold_out_goods_list', []).map(good => { return formatCartGoods(good, isAdvanceCart, false); });
|
|
|
|
|
|
// 下架的商品列表
|
|
|
result.offShelveGoods = _.get(data, 'off_shelves_goods_list', []).map(good => { return formatCartGoods(good, isAdvanceCart, false); });
|
|
|
|
|
|
// 赠品和加价购商品
|
|
|
if (data.gift_list.length || data.price_gift.length) {
|
|
|
result.freebieOrAdvanceBuy = true;
|
|
|
|
|
|
// 赠品
|
|
|
result.freebie = data.gift_list.map(good => { return formatAdvanceGoods(good); });
|
|
|
result.giftCount = result.freebie.length;
|
|
|
|
|
|
// 加价购
|
|
|
result.advanceBuy = data.price_gift.map(good => { return formatAdvanceGoods(good); });
|
|
|
result.advanceBuyCount = result.advanceBuy.length;
|
|
|
}
|
|
|
|
|
|
// 已参加的活动
|
|
|
if (data.promotion_info && data.promotion_info.length > 0) {
|
|
|
result.promotionInfo = data.promotion_info.map(promotion => {
|
|
|
return {id: promotion.promotion_id, name: promotion.promotion_title};
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 结算数据
|
|
|
result.formulaPrice = data.shopping_cart_data.promotion_formula;
|
|
|
result.count = data.shopping_cart_data.selected_goods_count;
|
|
|
result.isAllSelected = (data.shopping_cart_data.goods_count === data.shopping_cart_data.selected_goods_count) && (data.shopping_cart_data.selected_goods_count > 0);
|
|
|
result.sumPrice = transPrice(data.shopping_cart_data.last_order_amount);
|
|
|
|
|
|
|
|
|
return result;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 格式化加价购和赠品商品
|
|
|
*
|
|
|
* @param array $advanceGoods 加价购商品列表
|
|
|
* @param int $count 计商品件数
|
|
|
* @return array $arr 处理之后的加价购商品数据
|
|
|
*/
|
|
|
const formatAdvanceGoods = (advanceGood, isGift) => {
|
|
|
let result = {};
|
|
|
|
|
|
result.id = advanceGood.product_skn;
|
|
|
result.name = advanceGood.product_name;
|
|
|
result.thumb = advanceGood.goods_images ? helpers.image(advanceGood.goods_images, 120, 160) : '';
|
|
|
result.price = transPrice(advanceGood.last_price);
|
|
|
result.marketPrice = isGift ? '0.00' : transPrice(advanceGood.market_price);
|
|
|
result.count = advanceGood.storage_number;
|
|
|
|
|
|
return result;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 格式化购物车商品
|
|
|
*
|
|
|
* @param array $cartGoods 购物车商品列表
|
|
|
* @param boolean $isValid 是否是可用商品(非失效商品),默认是
|
|
|
* @param bool $isAdvanceCart 是否是预售购物车(和上市期有关)
|
|
|
* @return array 处理之后的购物车商品数据
|
|
|
*/
|
|
|
const formatCartGoods = (goodData, isAdvanceCart, isValid) => {
|
|
|
let result = {};
|
|
|
result.id = goodData.product_sku;
|
|
|
result.skn = goodData.product_skn;
|
|
|
result.name = goodData.product_name;
|
|
|
result.thumb = goodData.goods_images ? helpers.image(goodData.goods_images, 120, 160) : '';
|
|
|
result.color = goodData.color_name;
|
|
|
result.size = goodData.size_name;
|
|
|
result.checked = goodData.selected === 'Y';
|
|
|
result.price = transPrice(goodData.last_vip_price);
|
|
|
result.isVipPrice = goodData.sales_price !== goodData.last_vip_price && goodData.discount_tag === 'V';
|
|
|
result.isStudents = goodData.sales_price !== goodData.last_vip_price && goodData.discount_tag === 'S';
|
|
|
result.count = goodData.buy_number;
|
|
|
result.promotion_id = goodData.promotion_id;
|
|
|
if (isValid) {
|
|
|
// 库存不足
|
|
|
result.lowStocks = (goodData.buy_number > goodData.storage_number);
|
|
|
} else { // 失效商品
|
|
|
result.inValid = true;
|
|
|
}
|
|
|
|
|
|
// gift=>是否赠品,advanceBuy=>是否加价购,soldOut=>失效商品;
|
|
|
if (!goodData.goods_type) {
|
|
|
result.inValid = true;
|
|
|
}
|
|
|
else if (goodData.goods_type === 'gift' && !goodData.isAdvanceBuy) {
|
|
|
result.isGift = true;
|
|
|
result.salesPrice = transPrice(goodData.sales_price);
|
|
|
result.price = transPrice(goodData.last_price);
|
|
|
}
|
|
|
else if (goodData.goods_type === 'price_gift') {
|
|
|
result.showCheckbox = true;
|
|
|
result.isAdvanceBuy = true;
|
|
|
result.salesPrice = transPrice(goodData.sales_price);
|
|
|
result.price = transPrice(goodData.last_price);
|
|
|
}
|
|
|
else {
|
|
|
result.showCheckbox = true;
|
|
|
}
|
|
|
|
|
|
// 上市期
|
|
|
if (isAdvanceCart && goodData.expect_arrival_time) {
|
|
|
result.appearDate = goodData.expect_arrival_time;
|
|
|
}
|
|
|
|
|
|
// 商品链接
|
|
|
result.link = helpers.urlFormat(`/product/show_${goodData.product_skn}.html`);
|
|
|
};
|
|
|
|
|
|
const transPrice = (price, isSepcialZero) => {
|
|
|
return (price || isSepcialZero) ? price.toFixed(2) : 0;
|
|
|
};
|
|
|
|
|
|
module.exports = {
|
|
|
indexData
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|