import { printRecordService } from './database/PrintRecordService' import { getMainWindow } from 'ee-core/electron/window' import { logger } from 'ee-core/log' import { BrowserWindow } from 'electron' import {Event} from '../utils/Event' import {PrintEvent} from "../event/PrintEvent"; import {Channel} from "../channel/Channel"; import {PrintTaskQueue} from "../utils/PrintTaskQueue"; import {randomUUID} from "node:crypto"; import { appConfigService } from './database/AppConfigService' /** * 打印服务 */ class PrintService { private static queue = new PrintTaskQueue() constructor() { } _init() { Event.on(PrintEvent.PRINT_DATA, async (res) => { if (res instanceof Array) { for (let print of res) { PrintService.queue.enqueue({taskId: randomUUID(), data: print}) } } }) this.printLoop() } printLoop() { new Promise(async (resolve, reject) => { try { while (!PrintService.queue.isEmpty()) { getMainWindow().webContents.send(Channel.PRINT_TASK_LIST, PrintService.queue.list()) const task = PrintService.queue.dequeue() if (task) { // @ts-ignore await this.printData(task.data) } await new Promise(resolve2 => setTimeout(resolve2, 1000)) resolve(true) } } catch (e) { logger.error('print loop ex ---> ', e) reject(e) } }).then((res: any) => { logger.info("print then --->", res) }).catch((e: any) => { logger.error('print catch ex ---> ', e) }) } /** * 获取本地打印机列表 */ async listLocalPrinters(): Promise { return getMainWindow().webContents.getPrintersAsync(); } async printData(data: any) { return new Promise(async (resolve, reject) => { const appConfig = await appConfigService.getConfig() getMainWindow().webContents.send(Channel.PRINT_START) const printWindow = new BrowserWindow({ show: false }); const printData = `${data.html}` getMainWindow().webContents.send(Channel.PRINT_DATA, printData) await printWindow.loadURL(`data:text/html, ${printData}`); // @ts-ignore const deviceName = appConfig ? appConfig.defaultPrintName ? appConfig.defaultPrintName : '' : '' if (!deviceName) { getMainWindow().webContents.send(Channel.PRINT_RESULT, { success: false, failureReason: '未找到默认打印机' }) return } printWindow.webContents.on('did-finish-load', () => { getMainWindow().webContents.send(Channel.PRINT_PROGRESS) printWindow.webContents.print({ silent: true, printBackground: true, deviceName: deviceName, margins:data.margins||{ marginType :'none' }, landscape:data.landscape||false, copies:data.copies||1, dpi:data.dpi, header:data.header, footer:data.footer, pageSize:data.pageSize //A4,Legal | {height:} }, (success: boolean, failureReason: string) => { logger.info('打印结果:', success, failureReason) getMainWindow().webContents.send(Channel.PRINT_RESULT, { success, failureReason }) printRecordService.add({ printerName: data.deviceName, printHtml: data.html, printStatus: success ? 'success' : 'failure', failureReason: failureReason }) if (success) { resolve(true) }else { reject(failureReason) } printWindow.close(); }); // 静默打印 }); }) } } PrintService.toString = () => '[class PrintService]' const printService = new PrintService() export { PrintService, printService }