优化标签小票打印

This commit is contained in:
gyq
2024-06-26 14:01:12 +08:00
parent 87627e7b35
commit f3dfd4a121
9 changed files with 203 additions and 423 deletions

View File

@@ -72,13 +72,14 @@ import { onMounted, ref } from "vue";
import { useUser } from "@/store/user.js";
import payCard from "@/components/payCard/payCard.vue";
import { print } from "@/api/pay";
import { bySubType } from "@/api/device";
import { ElMessage } from "element-plus";
import dayjs from "dayjs";
import useStorage from '@/utils/useStorage'
import { ipcRenderer } from "electron";
import { usePrint } from '@/store/print.js'
const printStore = usePrint()
const store = useUser();
const emit = defineEmits("paySuccess");
@@ -115,157 +116,40 @@ const props = defineProps({
const isPrint = ref(true);
// 小票打印机列表
const printList = ref([]);
// 标签打印机列表
const printLabelList = ref([]);
const localPrintList = ref([])
// 获取打印机状态
async function bySubTypeAjax() {
try {
const res1 = await bySubType({
shopId: store.userInfo.shopId,
contentType: "local",
subType: "cash",
});
const res2 = await bySubType({
shopId: store.userInfo.shopId,
contentType: "local",
subType: "label",
});
printList.value = res1;
printLabelList.value = res2;
} catch (error) {
console.log(error);
}
}
// 获取本地打印机列表
function getPrintList() {
ipcRenderer.send("getPrintList");
ipcRenderer.on("printList", (event, arg) => {
localPrintList.value = arg;
// console.log(localPrintList.value);
});
}
// 检查本地打印机是否能正常使用
function checkLocalPrint(deviceName) {
let print = ''
for (let item of localPrintList.value) {
if (item.name == deviceName) {
print = item
}
}
if (!print.name) {
return false
} else {
return true
}
}
// 检测是否打印标签小票
function checkLabelPrint(props) {
// console.log(props);
if (!checkLocalPrint(printLabelList.value[0].config.deviceName)) {
ElMessage.error("本地打印机无法使用请检查打印机是否正确连接1");
} else {
let pids = printLabelList.value[0].config.categoryList.map(item => item.id)
let labelList = []
// 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++
labelList.push(
{
outNumber: props.outNumber,
name: item.name,
skuName: item.skuName,
masterId: props.orderInfo.tableName,
deviceName: printLabelList.value[0].config.deviceName,
createdAt: dayjs(props.createdAt).format('YYYY-MM-DD HH:mm:ss'),
// count: `${count}/${sum}`
}
)
}
}
})
printLabel(labelList)
}
}
// 打印标签
const printLabel = _.throttle(function (list) {
if (!checkLocalPrint(printLabelList.value[0].config.deviceName)) {
ElMessage.error("本地打印机无法使用,请检查打印机是否正确连接");
} else {
for (let i = 0; i <= list.length - 1; i++) {
// ipcRenderer.send('printerTagSync', JSON.stringify(list[i]))
setTimeout(() => {
ipcRenderer.send('printerTagSync', JSON.stringify(list[i]))
}, i * 800)
}
}
}, 1500, { leading: true, trailing: false })
// 打印操作
async function printHandle() {
const printHandle = _.throttle(async function () {
try {
if (!isPrint.value) return;
if (printLabelList.value.length) {
const data = {
shop_name: store.userInfo.merchantName,
carts: props.cart,
amount: props.amount,
remark: props.remark,
orderInfo: props.orderInfo,
deviceName: printLabelList.value[0].config.deviceName,
createdAt: dayjs(props.orderInfo.createdAt).format(
"YYYY-MM-DD HH:mm:ss"
),
printTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
};
checkLabelPrint(data)
// if (!checkLocalPrint(printList.value[0].config.deviceName)) {
// ElMessage.error("本地小票打印机无法使用请检查打印机是否正确连接3");
// } else {
// ipcRenderer.send("printerInfoSync", JSON.stringify(data));
// }
} else {
// ElMessage.error("您还没有添加本地打印设备,将使用网络打印");
try {
printLoading.value = true;
await print({
type: "normal",
ispre: true,
orderId: props.orderInfo.id,
});
printLoading.value = false;
// ElMessage.success("打印成功");
} catch (error) {
printLoading.value = false;
console.log(error);
}
const data = {
shop_name: store.userInfo.merchantName,
carts: props.cart,
amount: props.amount,
remark: props.remark,
orderInfo: props.orderInfo,
createdAt: dayjs(props.orderInfo.createdAt).format(
"YYYY-MM-DD HH:mm:ss"
),
printTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
};
printStore.labelPrint(data)
try {
printLoading.value = true;
await print({
type: "normal",
ispre: true,
orderId: props.orderInfo.id,
});
printLoading.value = false;
// ElMessage.success("打印成功");
} catch (error) {
printLoading.value = false;
console.log(error);
}
} catch (error) {
console.log(error);
}
}
}, 1500, { leading: true, trailing: false })
// 订单已支付
function paySuccess() {
@@ -297,8 +181,6 @@ function getLocalMemberInfo() {
}
onMounted(() => {
getPrintList();
bySubTypeAjax();
getLocalMemberInfo()
});
</script>