"use strict"; const path = require("path"); const electron = require("electron"); const os = require("os"); let win; electron.app.whenReady().then(() => { win = new electron.BrowserWindow({ title: "银收客", width: 1024, height: 768, fullscreenable: true, fullscreen: process.env.VITE_DEV_SERVER_URL ? false : true, simpleFullscreen: true, frame: process.env.VITE_DEV_SERVER_URL ? true : false, 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")); } electron.app.on("activate", () => { if (electron.BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); electron.ipcMain.on("quitHandler", (_, msg) => { win = null; electron.app.exit(); }); electron.ipcMain.on("getPrintList", () => { win.webContents.getPrintersAsync().then((res) => { win.webContents.send("printList", res); }); }); electron.ipcMain.on("getOSmacSync", () => { let mac = ""; if (os.networkInterfaces().WLAN) { mac = os.networkInterfaces().WLAN[0].mac; console.log("wlan.mac===", mac); } else { mac = os.networkInterfaces()["以太网"][0].mac; console.log("以太网.mac===", mac); } win.webContents.send("getOSmacRes", mac); }); const tagPrintWin = new electron.BrowserWindow({ show: false, width: 360, height: 240, webPreferences: { nodeIntegration: true, contextIsolation: false } }); if (process.env.VITE_DEV_SERVER_URL) { tagPrintWin.loadFile(path.join(__dirname, "../public/tag_print.html")); } else { tagPrintWin.loadFile(path.resolve(__dirname, "../dist/tag_print.html")); } electron.ipcMain.on("printerTagSync", (event, arg) => { console.log(arg); tagPrintWin.webContents.send("getParams", arg); }); electron.ipcMain.on("printTagStart", (event, arg) => { const _parmas = JSON.parse(arg); let name = _parmas.deviceName; tagPrintWin.webContents.print({ silent: true, deviceName: name, pageSize: { width: 45e3, height: 3e4 }, scaleFactor: 80, landscape: false, margins: { marginType: "none", top: 0, bottom: 0, left: 0, right: 0 }, dpi: { horizontal: 203, vertical: 203 } }); }); const gotTheLock = electron.app.requestSingleInstanceLock(); if (!gotTheLock) { electron.app.quit(); } else { electron.app.on("second-instance", (event, commandLine, workingDirectory) => { if (win) { if (win.isMinimized()) win.restore(); win.focus(); win.show(); } }); } win.on("close", (e) => { e.preventDefault(); win.webContents.send("showCloseDialog"); }); }); electron.app.on("window-all-closed", () => { if (process.platform !== "darwin") electron.app.quit(); });