import { defineStore } from "pinia"; import { ipcRenderer } from "electron"; import { bySubType } from "@/api/device"; import { useUser } from "@/store/user.js"; import dayjs from "dayjs"; import receiptPrint from "@/components/lodop/receiptPrint.js"; import lodopPrintWork from "@/components/lodop/lodopPrintWork.js"; import invoicePrint from "@/components/lodop/invoicePrint.js"; import refundPrint from "@/components/lodop/refundPrint.js"; export const usePrint = defineStore({ id: "print", state: () => ({ localDevices: [], // 本地打印机列表 deviceNoteList: [], // 添加的打印机 deviceLableList: [], // 添加的打印机 labelList: [], // 要打印的队列数据 printTimer: null, receiptList: [], // 小票队列数据 receiptTimer: null, }), actions: { // 获取本地打印机和已添加的可以用打印机列表 async init() { const store = useUser(); // 获取本地打印机 ipcRenderer.send("getPrintList"); ipcRenderer.on("printList", (event, arg) => { // localPrintList.value = arg; // console.log(localPrintList.value); this.localDevices = arg; }); // 获取已添加的小票打印机 this.deviceNoteList = await bySubType({ shopId: store.userInfo.shopId, contentType: "local", subType: "cash", }); // 获取已添加的标签打印机 this.deviceLableList = await bySubType({ shopId: store.userInfo.shopId, contentType: "local", subType: "label", }); console.log("打印队列初始化成功"); }, // 检查本地打印机是否能正常使用 checkLocalPrint(deviceName) { let print = ""; for (let item of this.localDevices) { if (item.name == deviceName) { print = item; } } if (!print.name) { return false; } else { return true; } }, // 打印标签小票 labelPrint(props) { if ( this.deviceLableList.length && this.checkLocalPrint(this.deviceLableList[0].config.deviceName) ) { let pids = this.deviceLableList[0].config.categoryList.map( (item) => item.id ); 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++) { sum++; } } }); props.carts.map((item) => { if (pids.some((el) => el == item.categoryId)) { for (let i = 0; i < item.number; i++) { count++; this.labelList.push({ outNumber: props.outNumber, name: item.name, skuName: item.skuName, masterId: props.orderInfo.tableName, deviceName: this.deviceLableList[0].config.deviceName, // deviceName: "Xprinter XP-T202UA", createdAt: dayjs(props.createdAt).format("YYYY-MM-DD HH:mm:ss"), isPrint: false, count: `${count}/${sum}`, }); } } }); // 执行打印操作 this.startLabelPrint(); } else { console.log("没有标签打印机"); } }, // 开始打印标签数据 startLabelPrint() { if (this.printTimer != null) return; this.printTimer = setInterval(() => { let item = ""; if (!this.labelList.length) { clearInterval(this.printTimer); this.printTimer = null; } else { item = this.labelList[0]; if (!item.isPrint) { ipcRenderer.send("printerTagSync", JSON.stringify(item)); this.labelList[0].isPrint = true; this.labelList.splice(0, 1); } } }, 800); }, // 添加小票打印对列表数据 pushReceiptData(props, isDevice = true) { console.log("pushReceiptData===", props); if (!isDevice) { // 测试打印,无需校验本地打印机 const store = useUser(); props.shop_name = store.userInfo.shopName; props.loginAccount = store.userInfo.loginAccount; 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; this.receiptList.push(props); this.startReceiptPrint(); } else { if ( this.deviceNoteList.length && this.checkLocalPrint(this.deviceNoteList[0].config.deviceName) ) { const store = useUser(); props.deviceName = this.deviceNoteList[0].config.deviceName; props.shop_name = store.userInfo.shopName; props.loginAccount = store.userInfo.loginAccount; 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("订单小票:没有小票打印机"); } } }, // 开始打印小票 startReceiptPrint() { if (this.receiptTimer !== null) return; this.receiptTimer = setInterval(() => { if (!this.receiptList.length) { clearInterval(this.receiptTimer); this.receiptTimer = null; } else { receiptPrint(this.receiptList[0]); this.receiptList.splice(0, 1); } }, 800); }, // 打印交班小票 printWork(data) { if ( this.deviceNoteList.length && this.checkLocalPrint(this.deviceNoteList[0].config.deviceName) ) { data.deviceName = this.deviceNoteList[0].config.deviceName; lodopPrintWork(data); } else { console.log("交班小票:没有小票打印机"); } }, // 打印订单发票 printInvoice(data) { if ( this.deviceNoteList.length && this.checkLocalPrint(this.deviceNoteList[0].config.deviceName) ) { data.deviceName = this.deviceNoteList[0].config.deviceName; invoicePrint(data); } else { console.log("订单发票:没有小票打印机"); } }, // 打印退单小票 printRefund(data) { if ( this.deviceNoteList.length && this.checkLocalPrint(this.deviceNoteList[0].config.deviceName) ) { data.deviceName = this.deviceNoteList[0].config.deviceName; refundPrint(data); } else { console.log("退单小票:没有小票打印机"); } }, }, });