优化小票 新增存酒管理

This commit is contained in:
gyq
2026-04-03 15:35:48 +08:00
parent c3a20ab2db
commit 78e88b8eb7
15 changed files with 1622 additions and 28138 deletions

View File

@@ -11,30 +11,28 @@ import { useSocket } from './socket.js';
export const usePrint = defineStore("print", {
state: () => ({
isPrintService: false, // 打印服务是否启动
printServiceTimer: false, // 打印服务定时器
printServiceTimerCount: 0, // 打印服务定时器计数
printServiceTimerMaxCount: 10, // 打印服务定时器最大计数
showPrintNotService: false, // 是否显示重启软件,
localDevices: [], // 本地打印机列表
deviceNoteList: [], // 添加的打印机
deviceLableList: [], // 添加的打印机
labelList: [], // 要打印的队列数据
isPrintService: false,
printServiceTimer: null,
printServiceTimerCount: 0,
printServiceTimerMaxCount: 10,
showPrintNotService: false,
localDevices: [],
deviceNoteList: [],
deviceLableList: [],
labelList: [],
printTimer: null,
receiptList: [], // 小票队列数据
receiptList: [],
receiptTimer: null,
}),
actions: {
// 获取本地打印机和已添加的可以用打印机列表
async init() {
// 获取本地打印机
ipcRenderer.send("getPrintList");
ipcRenderer.on("printList", (event, arg) => {
this.localDevices = arg;
});
try {
// 获取已添加的打印机
const res = await printerList();
this.deviceNoteList = res.records.filter(
(item) => item.status && item.subType == "cash"
@@ -42,18 +40,12 @@ export const usePrint = defineStore("print", {
this.deviceLableList = res.records.filter(
(item) => item.status && item.subType == "label"
);
console.log("打印队列初始化成功", {
deviceNoteList: this.deviceNoteList,
deviceLableList: this.deviceLableList,
});
} catch (error) {
console.error("获取已添加的打印机列表失败", error);
console.error("获取打印机列表失败", error);
}
// 检测打印服务是否启动
this.checkPrintService();
},
// 检测打印组件服务是否启动
checkPrintService() {
this.printServiceTimer = setInterval(() => {
if (
@@ -61,26 +53,154 @@ export const usePrint = defineStore("print", {
LODOP.webskt &&
LODOP.webskt.readyState == 1
) {
// 准备好
this.isPrintService = true;
clearInterval(this.printServiceTimer);
this.printServiceTimer = null;
} else {
this.printServiceTimerCount++;
console.log("打印服务未启动", this.printServiceTimerCount);
if (this.printServiceTimerCount >= this.printServiceTimerMaxCount) {
// 超过最大次数
this.isPrintService = false;
this.showPrintNotService = true;
clearInterval(this.printServiceTimer);
this.printServiceTimer = null;
}
}
console.log("打印服务是否启动:", this.isPrintService);
}, 1000);
},
// 检查本地打印机是否能正常使用
// 检查打印机是否可用
checkPrinterAvailable(printer) {
if (printer.connectionType === "局域网") {
const ipReg = /^(\d{1,3}\.){3}\d{1,3}$/;
return ipReg.test(printer.address);
}
if (printer.connectionType === "usb") {
return this.localDevices.some(item => item.name === printer.address);
}
return false;
},
// ==============================
// 最终完美版 → 空数组打印全部
// ==============================
pushReceiptData(props, isDevice = true) {
if (!isDevice) {
this.receiptList.push(props);
this.startReceiptPrint();
return;
}
const validPrinters = this.deviceNoteList.filter(p => {
return this.checkPrinterAvailable(p) && this.isPrintService;
});
if (validPrinters.length === 0) {
console.log("无可用小票打印机");
return;
}
const store = useUser();
validPrinters.forEach(printer => {
let filterCarts = [];
// ======================================
// ✅ 核心:分类为空数组 → 打印全部菜品
// ======================================
if (!printer.categoryList || printer.categoryList.length === 0) {
filterCarts = props.carts || [];
} else {
// 有分类 → 只打印对应分类
filterCarts = (props.carts || []).filter(item => {
return printer.categoryList.includes(item.categoryId);
});
}
if (filterCarts.length === 0) return;
const printData = {
...props,
carts: filterCarts,
deviceId: printer.id,
deviceName: printer.address,
printerName: printer.name,
connectionType: printer.connectionType,
shop_name: store.shopInfo.shopName,
loginAccount: store.userInfo.name,
createdAt: dayjs(props.createdAt).format("YYYY-MM-DD HH:mm:ss"),
printTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
};
if (!printData.orderInfo.masterId) {
printData.orderInfo.masterId = printData.orderInfo.tableName;
}
printData.orderInfo.outNumber = printData.outNumber;
if (!printData.discountAmount) {
printData.discountAmount = printData.amount;
}
this.receiptList.push(printData);
});
if (this.receiptList.length > 0) {
this.startReceiptPrint();
}
},
// ==============================
// 打印执行USB/局域网自动区分)
// ==============================
async startReceiptPrint() {
try {
const socketStore = useSocket();
const userStore = useUser();
if (this.receiptTimer !== null) return;
this.receiptTimer = setInterval(() => {
if (!this.receiptList.length) {
clearInterval(this.receiptTimer);
this.receiptTimer = null;
return;
}
const printData = this.receiptList[0];
try {
if (printData.connectionType === "局域网") {
ipcRenderer.send('networkPrint', JSON.stringify({
printerIp: printData.deviceName,
orderData: printData
}));
} else {
receiptPrint(printData);
}
const syncData = {
type: "cashier",
operate_type: "order_print_status",
table_code: printData.orderInfo.tableCode,
account: userStore.shopInfo.account,
print_status: "1",
order_id: printData.orderInfo.id,
print_id: printData.deviceId,
shop_id: printData.orderInfo.shopId,
};
socketStore.ws.send(JSON.stringify(syncData));
console.log("✅ 打印成功:", printData.deviceName, "菜品数:", printData.carts.length);
} catch (error) {
console.error("❌ 打印失败:", printData.deviceName, error);
} finally {
this.receiptList.splice(0, 1);
}
}, 2000);
} catch (error) {
console.log("打印定时器异常", error);
}
},
// ————————————————————————————————
// 以下原有代码完全不动除了printWork方法
// ————————————————————————————————
checkLocalPrint(address) {
let print = "";
for (let item of this.localDevices) {
@@ -88,17 +208,15 @@ export const usePrint = defineStore("print", {
print = item;
}
}
if (!print.name) {
return false;
} else {
return true;
}
},
// 打印标签小票
labelPrint(props) {
const store = useUser();
if (
this.deviceLableList.length &&
this.checkLocalPrint(this.deviceLableList[0].address)
@@ -106,7 +224,6 @@ export const usePrint = defineStore("print", {
let pids = this.deviceLableList[0].categoryList;
let count = 0;
let sum = 0;
props.carts.map((item) => {
if (pids.some((el) => el == item.categoryId)) {
for (let i = 0; i < item.number; i++) {
@@ -114,7 +231,6 @@ export const usePrint = defineStore("print", {
}
}
});
props.carts.map((item) => {
if (pids.some((el) => el == item.categoryId)) {
for (let i = 0; i < item.number; i++) {
@@ -133,17 +249,12 @@ export const usePrint = defineStore("print", {
}
}
});
console.log("this.labelList===", this.labelList);
// return;
// 执行打印操作
this.startLabelPrint();
} else {
console.log("标签打印:未在本机查询到打印机");
}
},
// 开始打印标签数据
startLabelPrint() {
if (this.printTimer != null) return;
this.printTimer = setInterval(() => {
@@ -161,88 +272,55 @@ export const usePrint = defineStore("print", {
}
}, 2000);
},
// 添加小票打印队列数据
pushReceiptData(props, isDevice = true) {
// console.log("pushReceiptData===", props);
if (!isDevice) {
// 测试打印,无需校验本地打印机
this.receiptList.push(props);
this.startReceiptPrint();
} else {
if (
this.deviceNoteList.length &&
this.checkLocalPrint(this.deviceNoteList[0].address) &&
this.isPrintService
) {
const store = useUser();
props.deviceId = this.deviceNoteList[0].id;
props.deviceName = this.deviceNoteList[0].address;
props.shop_name = store.shopInfo.shopName;
props.loginAccount = store.userInfo.name;
props.createdAt = dayjs(props.createdAt).format(
"YYYY-MM-DD HH:mm:ss"
);
props.printTime = dayjs().format("YYYY-MM-DD HH:mm:ss");
if (!props.orderInfo.masterId) {
props.orderInfo.masterId = props.orderInfo.tableName;
}
props.orderInfo.outNumber = props.outNumber;
if (!props.discountAmount) {
props.discountAmount = props.amount;
}
this.receiptList.push(props);
this.startReceiptPrint();
} else {
console.log("订单小票:未在本机查询到打印机");
}
}
},
// 开始打印小票
async startReceiptPrint() {
try {
const socketStore = useSocket();
const userStore = useUser();
if (this.receiptTimer !== null) return;
this.receiptTimer = setInterval(() => {
if (!this.receiptList.length) {
clearInterval(this.receiptTimer);
this.receiptTimer = null;
} else {
receiptPrint(this.receiptList[0]);
// 在这里触发已打印操作标记
const data = {
type: "cashier",
operate_type: "order_print_status",
table_code: this.receiptList[0].orderInfo.tableCode,
account: userStore.shopInfo.account,
print_status: "1",
order_id: this.receiptList[0].orderInfo.id,
print_id: this.receiptList[0].deviceId,
shop_id: this.receiptList[0].orderInfo.shopId,
}
socketStore.ws.send(JSON.stringify(data));
this.receiptList.splice(0, 1);
}
}, 2000);
} catch (error) {
console.log(error);
}
},
// 打印交班小票
// 打印交班小票优化1. 新增handoverSwitch=1才打印 2. 兼容局域网打印机)
printWork(data) {
if (
this.deviceNoteList.length &&
this.checkLocalPrint(this.deviceNoteList[0].address)
) {
data.deviceName = this.deviceNoteList[0].address;
lodopPrintWork(data);
} else {
console.log("交班小票:未在本机查询到打印机");
// 筛选条件:
// 1. 状态启用 + 小票类型 + handoverSwitch=1
// 2. 打印机可用
// 3. 打印服务正常
const validPrinters = this.deviceNoteList.filter(p => {
return p.status
&& p.subType === "cash"
&& p.handoverSwitch === 1 // 新增只有handoverSwitch为1的打印机才打印交班小票
&& this.checkPrinterAvailable(p)
&& this.isPrintService;
});
if (validPrinters.length === 0) {
console.log("交班小票无符合条件的可用打印机handoverSwitch≠1 或 打印机不可用)");
return;
}
// 遍历符合条件的打印机打印
validPrinters.forEach(printer => {
const printData = {
...data,
deviceId: printer.id,
deviceName: printer.address,
connectionType: printer.connectionType,
printTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
};
try {
// 区分局域网和USB打印机
if (printer.connectionType === "局域网") {
// 局域网打印机使用networkPrint指令
ipcRenderer.send('printHandoverReceipt', JSON.stringify({
printerIp: printer.address,
handoverData: printData
}));
} else {
// USB打印机使用原有LODOP方式
lodopPrintWork(printData);
}
console.log("✅ 交班小票打印成功:", printer.address);
} catch (error) {
console.error("❌ 交班小票打印失败:", printer.address, error);
}
});
},
// 打印订单发票
printInvoice(data) {
if (
this.deviceNoteList.length &&
@@ -254,17 +332,55 @@ export const usePrint = defineStore("print", {
console.log("订单发票:未在本机查询到打印机");
}
},
// 打印退单小票
// 退菜/退款
printRefund(data) {
if (
this.deviceNoteList.length &&
this.checkLocalPrint(this.deviceNoteList[0].address)
) {
data.deviceName = this.deviceNoteList[0].address;
refundPrint(data);
} else {
console.log("退单小票:未在本机查询到打印机");
// 筛选条件:
// 1. 状态启用 + 小票类型
// 2. 打印机可用
// 3. 打印服务正常
const validPrinters = this.deviceNoteList.filter(p => {
return p.status
&& p.subType === "cash"
&& this.checkPrinterAvailable(p)
&& this.isPrintService;
});
if (validPrinters.length === 0) {
console.log("退单小票:无符合条件的可用打印机");
return;
}
},
const store = useUser();
// 遍历符合条件的打印机打印
validPrinters.forEach(printer => {
const printData = {
...data,
deviceId: printer.id,
deviceName: printer.address,
printerName: printer.name,
connectionType: printer.connectionType,
shop_name: store.shopInfo.shopName,
loginAccount: store.userInfo.name,
printTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
};
try {
// 区分局域网和USB打印机
if (printer.connectionType === "局域网") {
// 局域网打印机使用networkPrint指令
ipcRenderer.send('printRefund', JSON.stringify({
printerIp: printer.address,
orderData: printData
}));
} else {
// USB打印机使用原有LODOP方式
refundPrint(printData);
}
console.log("✅ 退单小票打印成功:", printer.address);
} catch (error) {
console.error("❌ 退单小票打印失败:", printer.address, error);
}
});
}
},
});
});