"use strict"; const electron = require("electron"); const path = require("path"); function printUtils(params) { return new Promise(async (resolvePrint, rejectPrint) => { let subMainWindow = new electron.BrowserWindow({ // show: false, webPreferences: { nodeIntegration: true, // For electron >= 4.0.0 contextIsolation: false, webSecurity: false, enableRemoteModule: true } }); subMainWindow.loadFile(path.join(__dirname, "../public/print.html")); subMainWindow.webContents.on("did-finish-load", async (res) => { subMainWindow.webContents.openDevTools(); console.log("网页加载完成", res); subMainWindow.webContents.send("getParams", params); return; }); electron.ipcMain.on("printStart", () => { console.log("开始打印"); subMainWindow.webContents.print({ silent: true }); }); }); } electron.app.whenReady().then(() => { const win = new electron.BrowserWindow({ title: "银收客", width: 1200, height: 800, fullscreenable: true, fullscreen: false, simpleFullscreen: true, frame: true, webPreferences: { // 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法 nodeIntegration: true, contextIsolation: false } }); if (process.env.VITE_DEV_SERVER_URL) { win.loadURL(process.env.VITE_DEV_SERVER_URL); } else { win.loadFile(path.resolve(__dirname, "../dist/index.html")); } win.webContents.openDevTools(); electron.app.on("activate", () => { if (electron.BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); electron.ipcMain.on("quitHandler", (_, msg) => { electron.app.quit(); }); electron.ipcMain.on("printerInfoSync", async (event, params) => { await printUtils(params); }); }); electron.app.on("window-all-closed", () => { if (process.platform !== "darwin") electron.app.quit(); });