You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.4 KiB
JavaScript

var cartUtils = {};
var app = {};
var init = function (app,cartUtils){
this.app = app;
this.cartUtils = cartUtils;
}
/**
* 加入购物车
*/
var addToCart = function (goods, num) {
var suiltData = {};
if (goods.suitFlag == 1){
suiltData = processSuitData(this.app,goods, num);
}
var options = {
boxPrice: goods.boxPrice,
uniqueId: goods.uniqueId,
productId: goods.productId,
productNo: goods.productNo,
unitId: goods.unitId,
categoryId: goods.categoryId,
goodsId: goods.goodsId,
goodsName: goods.name,
specId: goods.specId,
specName: goods.specName,
makeCount: undefined == goods.makeCount ? 0 : goods.makeCount,
price: goods.price,
memberPrice: goods.memberPrice,
makeId: undefined == goods.makeId ? "" : goods.makeId,
makeName: undefined == goods.makeName ? "" : goods.makeName,
makePrice: undefined == goods.makePrice ? "" : goods.makePrice,
makeAmount: undefined == goods.makeAmount ? 0 : goods.makeAmount,
num: num,
isSuit: goods.suitFlag == 1 ?true:false,
addAmount: undefined == suiltData.addAmount ? 0 : suiltData.addAmount,
suitDetails: undefined == suiltData.suitDetails ? [] : suiltData.suitDetails
}
// 商品压入购物车
this.cartUtils.putCartMap(options);
};
/**
* 处理套餐商品加入购物车
*/
function processSuitData(app,goods,num){
var suiltData = {};
var suitDetails = [];
goods.suitList.forEach(function (group, ginx) {
// 获取已选中套餐明细
group.detail.forEach(function (item, inx) {
if (item.select) {
var suitItem = {
goodsId: item.id,
name: item.productName,
productId: item.productId,
productNo: item.productNo,
categoryId: item.categoryId,
specId: item.specId,
specName: item.specName,
unitId: item.unitId,
num: item.num,
quantity:item.quantity,
price: item.price,
addPrice: item.addPrice,
memberPrice: item.memberPrice,
makeId: item.makeId,
makeName: item.makeName,
makePrice: item.makePrice,
makeAmount: item.makeAmount,
}
suitDetails.push(suitItem);
}
});
});
suiltData.addAmount = goods.addAmount;
suiltData.suitDetails = suitDetails;
return suiltData;
};
/**
* 从购物车中减去商品数量
*/
var addNumberByCart = function (uniqueId) {
var specMap = this.cartUtils.getSpecMapByCart();
if (undefined != specMap[uniqueId]) {
var specGoods = specMap[uniqueId];
var num = this.app.utils.numAdd(specGoods.num, 1);
this.addToCart(specGoods, num);
}
};
/**
* 从购物车中减去商品数量
*/
var decNumberByCart = function (uniqueId) {
var specMap = this.cartUtils.getSpecMapByCart();
if (undefined != specMap[uniqueId]) {
var specGoods = specMap[uniqueId];
var num = 0;
if (specGoods.num > 0) {
num = this.app.utils.numSub(specGoods.num, 1);
}
this.addToCart(specGoods, num);
}
};
module.exports = {
init: init,
addToCart: addToCart,
addNumberByCart: addNumberByCart,
decNumberByCart: decNumberByCart
}