优化打印标签逻辑

This commit is contained in:
gyq
2024-06-22 09:15:27 +08:00
parent 0b2b4b44d0
commit 44495c3ee7
9 changed files with 136 additions and 62 deletions

View File

@@ -32,8 +32,15 @@ import { bySubType } from "@/api/device";
import { dayjs, ElMessage } from "element-plus";
import { ipcRenderer } from 'electron'
if (!useStorage.get('uuid')) {
useStorage.set('uuid', uuidv4())
const uuid = ref('')
function createUUID() {
if (!useStorage.get('uuid')) {
useStorage.set('uuid', uuidv4())
uuid.value = useStorage.get('uuid')
} else {
uuid.value = useStorage.get('uuid')
}
}
const store = useUser();
@@ -57,12 +64,14 @@ watch(route, (to) => {
if (ws.value != null) {
console.log('关闭ws');
ws.value.close()
ws.value = null
wsIsClose.value = true
}
} else {
// 打开ws
console.log('打开ws');
openWs()
// setTimeout(() => {
// }, 1000)
}
});
@@ -168,9 +177,20 @@ function checkLabelPrint(props) {
} else {
let pids = printLabelList.value[0].config.categoryList.map(item => item.id)
let labelList = []
let count = 0
let sum = 0
props.carts.map(item => {
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++
labelList.push(
{
outNumber: props.outNumber,
@@ -178,7 +198,8 @@ function checkLabelPrint(props) {
skuName: item.skuName,
masterId: props.orderInfo.tableName,
deviceName: printLabelList.value[0].config.deviceName,
createdAt: dayjs(props.createdAt).format('YYYY-MM-DD HH:mm:ss')
createdAt: dayjs(props.createdAt).format('YYYY-MM-DD HH:mm:ss'),
count: `${count}/${sum}`
}
)
}
@@ -190,24 +211,13 @@ function checkLabelPrint(props) {
}
// 打印标签
let labelCount = ref(0)
let labelPrintTimer = ref(null)
function printLabel(list) {
console.log(list);
if (!checkLocalPrint(printLabelList.value[0].config.deviceName)) {
ElMessage.error("本地打印机无法使用,请检查打印机是否正确连接");
} else {
labelPrintTimer.value = setInterval(() => {
// console.log('labelCount', labelCount.value);
list[labelCount.value].count = `${list.length - labelCount.value}/${list.length}`
ipcRenderer.send('printerTagSync', JSON.stringify(list[labelCount.value]))
labelCount.value++
if (labelCount.value > list.length - 1) {
clearInterval(labelPrintTimer.value)
labelPrintTimer.value = null
labelCount.value = 0
}
}, 1000)
list.map(item => {
ipcRenderer.send('printerTagSync', JSON.stringify(item))
})
}
}
@@ -215,9 +225,11 @@ let ws = ref(null)
let wsIsClose = ref(false)
// 初始化websocket
function initWebSocket(wsUrl = import.meta.env.VITE_API_WSS) {
createUUID()
wsIsClose.value = false
ws.value = new WebSocket(wsUrl);
console.log("websocket:", ws.value);
// console.log("websocket:", ws.value);
ws.value.onopen = function () {
console.log('wss连接成功');
@@ -234,7 +246,7 @@ function initWebSocket(wsUrl = import.meta.env.VITE_API_WSS) {
ws.value.send(JSON.stringify({
type: "connect",
shopId: store.userInfo.shopId,
clientId: useStorage.get('uuid')
clientId: uuid.value
}))
};
@@ -308,7 +320,7 @@ function reConnect(wsUrl) {
}
onMounted(() => {
getPrintList();
getPrintList()
})
</script>