|
|
@@ -5,17 +5,48 @@ 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";
|
|
|
/**
|
|
|
* 打印服务
|
|
|
*/
|
|
|
class PrintService {
|
|
|
-
|
|
|
+ private static queue = new PrintTaskQueue()
|
|
|
constructor() {
|
|
|
}
|
|
|
|
|
|
_init() {
|
|
|
Event.on(PrintEvent.PRINT_DATA, async (res) => {
|
|
|
- await this.printData(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)
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -27,41 +58,48 @@ class PrintService {
|
|
|
}
|
|
|
|
|
|
async printData(data: any) {
|
|
|
- getMainWindow().webContents.send(Channel.PRINT_START)
|
|
|
- const printWindow = new BrowserWindow({ show: false });
|
|
|
- const printData = `<html><body>${data.html}</body></html>`
|
|
|
- getMainWindow().webContents.send(Channel.PRINT_DATA, printData)
|
|
|
- await printWindow.loadURL(`data:text/html, ${printData}`);
|
|
|
- printWindow.webContents.on('did-finish-load', () => {
|
|
|
- getMainWindow().webContents.send(Channel.PRINT_PROGRESS)
|
|
|
- printWindow.webContents.print({
|
|
|
- silent: true,
|
|
|
- printBackground: true,
|
|
|
- deviceName: data.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
|
|
|
- })
|
|
|
- printWindow.close();
|
|
|
- }); // 静默打印
|
|
|
- });
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ getMainWindow().webContents.send(Channel.PRINT_START)
|
|
|
+ const printWindow = new BrowserWindow({ show: false });
|
|
|
+ const printData = `<html><body>${data.html}</body></html>`
|
|
|
+ getMainWindow().webContents.send(Channel.PRINT_DATA, printData)
|
|
|
+ await printWindow.loadURL(`data:text/html, ${printData}`);
|
|
|
+ printWindow.webContents.on('did-finish-load', () => {
|
|
|
+ getMainWindow().webContents.send(Channel.PRINT_PROGRESS)
|
|
|
+ printWindow.webContents.print({
|
|
|
+ silent: true,
|
|
|
+ printBackground: true,
|
|
|
+ deviceName: data.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();
|
|
|
+ }); // 静默打印
|
|
|
+ });
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
}
|