优化订单小票打印

This commit is contained in:
gyq
2026-04-08 15:06:22 +08:00
parent 8655757dd6
commit 1985713f28
19 changed files with 782 additions and 395 deletions

View File

@@ -204,14 +204,55 @@ const printHandle = _.throttle(async function () {
await printOrderLable(true)
}, 1500, { leading: true, trailing: false })
// 在 usePrint 的 actions 中添加(比如放在 pushReceiptData 方法下方)
function calcAllCartsTotalSum(cartObj) {
// 初始化总和为0
let totalSum = 0;
// 遍历原始对象的所有键0、1、2...
Object.keys(cartObj).forEach(key => {
const carts = cartObj[key] || [];
// 累加当前分类下的菜品总价num × unitPrice
carts.forEach(item => {
const num = Number(item.num) - Number(item.returnNum) || 0; // 防错非数字转0
const unitPrice = Number(item.unitPrice) || 0;
totalSum += num * unitPrice;
});
});
return totalSum; // 返回所有菜品的总价总和
}
// 打印订单标签
async function printOrderLable(isBefore = false) {
try {
let orderId = goodsStore.orderListInfo.id
const data = await getOrderByIdAjax(orderId);
let data = await getOrderByIdAjax(orderId);
console.log(`打印订单标签数据${isBefore}===`, data);
if (isBefore) {
data.originAmount = calcAllCartsTotalSum(data.detailMap)
}
if (data.seatAmount > 0 && data.dineMode == 'dine-in' && isBefore) {
data.originAmount += data.seatAmount
}
let packFee = 0
data.cartList.forEach(item => {
packFee += item.num - item.returnNum * item.packAmount
})
if (packFee > 0 && isBefore) {
data.originAmount += packFee
data.packFee = packFee
}
let printList = useStorage.get("printList") || [];
console.log('printStore.deviceNoteList.length:', printStore.deviceNoteList.length);
// 防止重复打印
if (!printList.some((el) => el == orderId)) {
if (!isBefore) {
@@ -225,7 +266,7 @@ async function printOrderLable(isBefore = false) {
printStore.labelPrint(commOrderPrintData(data))
}
}
console.log('printStore.deviceNoteList', printStore.deviceNoteList);
if (printStore.deviceNoteList.length) {
// 使用本地打印机打印
printStore.pushReceiptData(commOrderPrintData({ ...data, isBefore: isBefore }));
@@ -248,7 +289,7 @@ async function printOrderLable(isBefore = false) {
// 订单已支付
function paySuccess() {
// if (isPrint.value) printOrderLable()
if (isPrint.value) printOrderLable()
emits('success')
dialogVisible.value = false;
ElMessage.success('支付成功')