import { defineStore } from 'pinia'; import { computed, watchEffect } from 'vue'; import { ref } from 'vue'; export const useCartStore = defineStore('cart', () => { // const dinersNum = uni.cache.get('dinersNum') // const isVip = uni.cache.get('orderVIP').isVip //此用户是否是会员 // const isMemberPrice = uni.cache.get('ordershopUserInfo').isMemberPrice //此店是否可以用会员 // const isTableFee = uni.cache.get('ordershopUserInfo').isTableFee //此店是否免桌位费 // const tableFee = uni.cache.get('ordershopUserInfo').tableFee //一个餐位费多钱 // 计算单个商品的打包费用(向下取整并保留两位小数) 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; } // console.log(uni.cache.get('orderVIP').isVip, uni.cache.get('ordershopUserInfo').isMemberPrice, // 111) // 购物车总数价格 let cart = matchedProducts.reduce((total, item) => { // 是否启用会员价 0否1是 if (uni.cache.get('orderVIP').isVip == 1 && uni.cache.get('ordershopUserInfo') .isMemberPrice == 1) { // memberPrice会员价 return total + parseFloat(item.memberPrice) * parseFloat(item.num); } else { // salePrice销售价 return total + parseFloat(item.price) * parseFloat(item.num); } }, 0); // 向上取整并保留两位小数 return cart = Math.ceil(cart * 100) / 100; }); // 计算商品卷所选择的总价格 const getTotalProductroll = (matchedProducts) => computed(() => { if (!matchedProducts || !Array.isArray(matchedProducts)) { return 0; } // 购物车总数价格 let cart = matchedProducts.reduce((total, item) => { // 是否启用会员价 0否1是 if (uni.cache.get('orderVIP').isVip == 1 && uni.cache.get('ordershopUserInfo') .isMemberPrice == 1) { // memberPrice会员价 return total + parseFloat(item.memberPrice) } else { // salePrice销售价 return total + parseFloat(item.price) } }, 0); // 向上取整并保留两位小数 return cart = Math.ceil(cart * 100) / 100; }); // 桌位置 const getTotalSeatcharge = (seatNum) => computed(() => { // 是否免除桌位费 0 否 1 是 let tableFeeTotals = 0 if (uni.cache.get('ordershopUserInfo').isTableFee == 0 && (seatNum || uni.cache.get('dinersNum'))) { tableFeeTotals = Math.ceil(parseFloat((seatNum || uni.cache.get('dinersNum'))) * parseFloat( uni.cache.get('ordershopUserInfo').tableFee) * 100) / 100; } console.log(uni.cache.get('ordershopUserInfo').isTableFee,seatNum,22222) return Math.floor(tableFeeTotals * 100) / 100; }); // 计算购物车总打包费用(向下取整并保留两位小数) 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, getTotalProductroll }; });