146 lines
3.9 KiB
TypeScript
146 lines
3.9 KiB
TypeScript
|
|
// src/utils/print-utils.ts
|
|||
|
|
import { io, Socket } from "socket.io-client";
|
|||
|
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
|||
|
|
import { usePrintStore } from '@/store/modules/printStore'
|
|||
|
|
|
|||
|
|
// ... 类型定义 ...
|
|||
|
|
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(
|
|||
|
|
`连接失败!<br>请确保目标服务器已<a style="color: #1f79db" href="https://gitee.com/CcSimple/electron-hiprint/releases" target="_blank"> 下载 </a> 并运行 打印服务!`,
|
|||
|
|
"客户端未连接",
|
|||
|
|
{
|
|||
|
|
dangerouslyUseHTMLString: true,
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
printStore.setConnected(false);
|
|||
|
|
socket?.close();
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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;
|
|||
|
|
},
|
|||
|
|
};
|