70 lines
2.0 KiB
JavaScript
70 lines
2.0 KiB
JavaScript
import {
|
||
defineStore
|
||
} from 'pinia';
|
||
import {
|
||
computed,
|
||
watchEffect
|
||
} from 'vue';
|
||
import {
|
||
ref
|
||
} from 'vue';
|
||
|
||
export const useCartStore = defineStore('cart', () => {
|
||
const shopInfo = uni.cache.get('shopInfo')
|
||
const dinersNum = uni.cache.get('dinersNum')
|
||
|
||
// 计算单个商品的打包费用(向下取整并保留两位小数)
|
||
const itemSinglePackFee = (item) => {
|
||
const fee = item.packFee * item.cartNumber;
|
||
// 先将结果乘以 100,向下取整,再除以 100 以保留两位小数
|
||
return Math.floor(fee * 100) / 100
|
||
};
|
||
|
||
// 计算购物车商品总价格
|
||
const getTotalTotalPrices = (matchedProducts) => computed(() => {
|
||
if (!matchedProducts || !Array.isArray(matchedProducts)) {
|
||
return 0;
|
||
}
|
||
// 购物车总数价格
|
||
let cart = matchedProducts.reduce((total, item) => {
|
||
// 是否启用会员价 0否1是
|
||
if (shopInfo.isVip == 1 && shopInfo.isMemberPrice == 1) {
|
||
// memberPrice会员价
|
||
return total + parseFloat(item.memberPrice) * parseFloat(item.cartNumber);
|
||
} else {
|
||
// salePrice销售价
|
||
return total + parseFloat(item.salePrice) * parseFloat(item.cartNumber);
|
||
}
|
||
}, 0);
|
||
// 向上取整并保留两位小数
|
||
return cart = Math.ceil(cart * 100) / 100;
|
||
});
|
||
|
||
// 桌位置
|
||
const getTotalSeatcharge = () => computed(() => {
|
||
// 是否免除桌位费 0 否 1 是
|
||
let tableFeeTotals = 0
|
||
if (shopInfo.isTableFee == 0 && dinersNum) {
|
||
const tableFeeTotals = Math.ceil(parseFloat(dinersNum) * parseFloat(shopInfo
|
||
.tableFee) * 100) / 100;
|
||
}
|
||
return Math.floor(tableFeeTotals * 100) / 100 ? Math.floor(tableFeeTotals * 100) / 100 : 0;
|
||
});
|
||
|
||
// 计算购物车总打包费用(向下取整并保留两位小数)
|
||
const getTotalPackFee = (cartList) => computed(() => {
|
||
let total = 0;
|
||
for (const item of cartList) {
|
||
total += itemSinglePackFee(item);
|
||
}
|
||
return Math.floor(total * 100) / 100 ? Math.floor(total * 100) / 100 : 0;
|
||
// 同样对总费用进行向下取整并保留两位小数处理
|
||
});
|
||
|
||
return {
|
||
itemSinglePackFee,
|
||
getTotalPackFee,
|
||
getTotalSeatcharge,
|
||
getTotalTotalPrices
|
||
};
|
||
}); |