cashier_desktop/src/store/print.js

119 lines
3.4 KiB
JavaScript

import { defineStore } from "pinia";
import { ipcRenderer } from "electron";
import { bySubType } from "@/api/device";
import { useUser } from "@/store/user.js";
import dayjs from "dayjs";
export const usePrint = defineStore({
id: "print",
state: () => ({
localDevices: [], // 本地打印机列表
deviceNoteList: [], // 添加的打印机
deviceLableList: [], // 添加的打印机
labelList: [], // 要打印的队列数据
printTimer: 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.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();
}
},
// 开始打印标签数据
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);
},
},
});