cashier_wx/stores/order.js

131 lines
4.2 KiB
JavaScript

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 roundUpToTwoDecimals = (num, i) => {
// 先将数字乘以 100 并转换为字符串保留足够的小数位
let temp = (num * 100).toFixed(10);
// 向上取整
let rounded = null
if (i == 'upward') {
rounded = Math.ceil(parseFloat(temp));
} else {
rounded = Math.floor(parseFloat(temp));
}
// 再除以 100 得到保留两位小数的结果
return rounded / 100;
};
// 精确计算函数
const preciseCalculation = (num1, num2) => {
// 将数字转换为整数,乘以 100 以保留两位小数
const int1 = BigInt(Math.round(num1 * 100));
const int2 = BigInt(Math.round(num2 * 100));
// 执行乘法运算
const result = int1 * int2;
// 再除以 10000 以还原为原来的小数
return Number(result) / 10000;
};
// 计算购物车商品总价格
const getTotalTotalPrices = (matchedProducts, changeFreeenable = true) => 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 && changeFreeenable) {
// memberPrice会员价
return total + (parseFloat(item.memberPrice || item.price) * parseFloat(item
.num - item
.returnNum));
} else {
// salePrice销售价
return total + (parseFloat(item.price) * parseFloat(item.num - item.returnNum));
}
}, 0);
cart = cart.toFixed(2)
console.log(parseFloat(cart))
// 向上取整并保留两位小数
// let result = roundUpToTwoDecimals(cart, 'upward')
return parseFloat(cart);
});
// 计算商品卷所选择的总价格
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 || item.price)
} else {
// salePrice销售价
return total + parseFloat(item.price)
}
}, 0);
// 向上取整并保留两位小数
let result = roundUpToTwoDecimals(cart, 'upward')
return result;
});
// 桌位置
const getTotalSeatcharge = (seatNum) => computed(() => {
// 是否免除桌位费 0 否 1 是
let cart = 0
if (uni.cache.get('ordershopUserInfo').isTableFee == 0 && seatNum) {
cart = parseFloat(seatNum) * parseFloat(uni.cache.get('ordershopUserInfo').tableFee)
// Math.ceil(parseFloat(seatNum) * parseFloat(
// uni.cache.get('ordershopUserInfo').tableFee) * 100) / 100;
}
// 向下取整并保留两位小数
let result = roundUpToTwoDecimals(cart, 'downward')
return result;
});
// 计算购物车总打包费用(向下取整并保留两位小数)
const getTotalPackFee = (cartList) => computed(() => {
const total = cartList.reduce((sum, item) => {
return sum + (parseFloat(item.packAmount) * (parseFloat(item.packNumber) || (
parseFloat(item.num) - parseFloat(item.returnNum))));
}, 0);
// 向下取整并保留两位小数
let result = roundUpToTwoDecimals(total, 'downward')
console.log(result)
return result;
// return Math.floor(total * 100) / 100;
});
return {
getTotalPackFee,
getTotalSeatcharge,
getTotalTotalPrices,
getTotalProductroll
};
});