lis8.0-vue3/src/utils/hiprintPrinter.ts

170 lines
4.5 KiB
TypeScript
Raw Normal View History

2025-08-28 17:47:20 +08:00
// src/utils/hiprintPrinter.ts
2025-08-26 17:31:31 +08:00
import { io, Socket } from "socket.io-client";
import { ElMessage, ElMessageBox } from 'element-plus'
import { usePrintStore } from '@/store/modules/printStore'
2025-08-29 17:40:32 +08:00
import { onUnmounted } from "vue"
// @ts-ignore
import Download from '@/plugins/download';
2025-08-26 17:31:31 +08:00
// ... 类型定义 ...
type PrinterInfo = {
name: string;
isDefault?: boolean;
[key: string]: any;
};
type ClientInfo = any;
type OnPrinterUpdate = (printerList: PrinterInfo[]) => void;
let socket: Socket | null = null;
let clientInfo: ClientInfo = null;
// 初始化 Socket 连接
function initSocket(onPrinterUpdate?: OnPrinterUpdate) {
const printStore = usePrintStore();
if (socket) {
socket.disconnect();
}
socket = io("http://localhost:17521", {
transports: ["websocket"],
reconnection: false, //闭自动重连
auth: {
token: "vue-plugin-hiprint",
},
});
// 连接成功
socket.on("connect", () => {
printStore.setConnected(true);
console.log("✅ 已连接 electron-hiprint 客户端");
socket?.emit("getClientInfo");
socket?.emit("refreshPrinterList");
});
// 客户端信息
socket.on("clientInfo", (info: ClientInfo) => {
printStore.setClientInfo(info);
console.log("📱 客户端信息:", info);
});
// 打印机列表 (更新默认打印机)
socket.on("printerList", (list: PrinterInfo[]) => {
printStore.setPrinterList(list);
onPrinterUpdate?.(list);
console.log("🖨️ 打印机列表更新:", list);
});
// 连接断开
socket.on("disconnect", (reason: string) => {
printStore.setConnected(false);
console.error("❌ 客户端连接断开:", reason);
if (reason === "io server disconnect") {
socket?.connect();
}
});
socket.on("connect_error", (err: Error) => {
console.log('连接错误', err);
ElMessageBox.alert(
2025-09-05 17:03:56 +08:00
`连接失败!<br>请确保目标服务器已<a style="color: #1f79db" onclick="downExe()">下载安装</a> 并<a style="color: #1f79db" onclick="yxHiprint()"> 运行 </a> 打印服务!`,
2025-08-26 17:31:31 +08:00
"客户端未连接",
{
dangerouslyUseHTMLString: true,
}
)
printStore.setConnected(false);
socket?.close();
});
2025-08-29 17:40:32 +08:00
2025-08-26 17:31:31 +08:00
}
2025-08-29 17:40:32 +08:00
const downExe = () => {
// 下载逻辑实现
2025-09-05 17:03:56 +08:00
// console.log('开始下载打印服务');
Download.name('hiprint_win_x64-1.0.13.exe', false)
};
const yxHiprint = () => {
// 唤起客户端
window.open("hiprint://");
2025-08-29 17:40:32 +08:00
};
// 挂载到window,使其能被全局访问
2025-09-05 17:03:56 +08:00
(window as any).downExe = downExe;
(window as any).yxHiprint = yxHiprint;
2025-08-26 17:31:31 +08:00
function silentPrintPdf(url: string, printOptions?: object) {
const printStore = usePrintStore();
if (!url) {
console.log("请传入有效的PDF文件路径!");
return;
}
const templateId = `pdf-print-${Date.now()}`;
// 检查是否连接客户端
if (!printStore.isConnected) {
initSocket((printerList) => {
if (printerList.length > 0) {
socket?.emit("news", {
client: clientInfo,
printer: targetPrinter,
type: "url_pdf",
templateId: templateId,
pdf_path: url,
});
ElMessage.success('正在打印文件,请稍候...')
}
return
});
}
const targetPrinter = printStore.defaultPrinter;
if (!targetPrinter) {
console.log("未检测到打印机,请先连接打印机!");
return;
}
socket?.emit("news", {
client: clientInfo,
printer: targetPrinter,
type: "url_pdf",
templateId: templateId,
pdf_path: url,
});
ElMessage.success('正在打印文件,请稍候...')
// console.log(`🚀 PDF打印请求已发送,任务ID:${templateId},打印机:${targetPrinter}`);
}
// 销毁
function destroy() {
const printStore = usePrintStore();
if (socket) {
socket.disconnect();
socket = null;
printStore.setConnected(false);
console.log("🔌 已断开与 electron-hiprint 的连接");
}
}
// 更新 getter 函数以从 store 获取值
export const HiprintPrinter = {
initSocket,
silentPrintPdf,
destroy,
get isConnected() {
const printStore = usePrintStore();
return printStore.isConnected;
},
get clientInfo() {
const printStore = usePrintStore();
return printStore.clientInfo;
},
get printerList() {
const printStore = usePrintStore();
return printStore.printerList;
},
get defaultPrinter() {
const printStore = usePrintStore();
return printStore.defaultPrinter;
},
2025-08-29 17:40:32 +08:00
};
onUnmounted(() => {
if ('downExe' in window) {
delete (window as any).downExe;
}
});