'use strict' const { app, protocol, BrowserWindow, ipcMain } = require('electron') const { createProtocol } = require('vue-cli-plugin-electron-builder/lib') const path = require('path') const isDevelopment = process.env.NODE_ENV !== 'production' const additionalData = { myKey: 'myValue' } let myWindow = null // Scheme must be registered before the app is ready protocol.registerSchemesAsPrivileged([ { scheme: 'app', privileges: { secure: true, standard: true } } ]) async function createWindow() { // Create the browser window. const win = new BrowserWindow({ width: 1200, height: 800, webPreferences: { nodeIntegration: true, contextIsolation: false, enableRemoteModule: true, webSecurity: false, preload: path.join(__dirname, '../electron/preload.js') } }) if (process.env.WEBPACK_DEV_SERVER_URL) { // Load the url of the dev server if in development mode await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL) if (!process.env.IS_TEST) win.webContents.openDevTools() } else { createProtocol('app') // Load the index.html when not in development win.loadURL('app://./index.html') } // 处理打印请求 ipcMain.on('silent-print', (event, data) => { const { content, printerName } = data const printWindow = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } }) printWindow.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(content)}`) printWindow.webContents.on('did-finish-load', () => { // 获取所有打印机 const printers = printWindow.webContents.getPrinters() console.log('Available printers:', printers) // 检查指定的打印机是否存在 const targetPrinter = printers.find(p => p.name === printerName) if (!targetPrinter) { console.error('Printer not found:', printerName) event.reply('print-complete', { success: false, error: `Printer "${printerName}" not found` }) printWindow.close() return } // 设置打印选项 const options = { silent: true, printBackground: true, deviceName: printerName, margins: { marginType: 'none' }, landscape: false, scaleFactor: 100 } // 执行打印 printWindow.webContents.print(options, (success, errorType) => { console.log('Print result:', success, errorType) if (success) { event.reply('print-complete', { success: true }) } else { event.reply('print-complete', { success: false, error: errorType || 'Unknown error' }) } printWindow.close() }) }) // 处理加载错误 printWindow.webContents.on('did-fail-load', (error) => { console.error('Failed to load content:', error) event.reply('print-complete', { success: false, error: 'Failed to load content' }) printWindow.close() }) }) } const gotTheLock = app.requestSingleInstanceLock(additionalData) if (!gotTheLock) { app.quit() } else { app.on('second-instance', (event, commandLine, workingDirectory, additionalData) => { if (myWindow) { if (myWindow.isMinimized()) myWindow.restore() myWindow.focus() } }) // Quit when all windows are closed. app.on('window-all-closed', () => { // On macOS it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', () => { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (BrowserWindow.getAllWindows().length === 0) createWindow() }) // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.on('ready', async () => { createWindow() }) // Exit cleanly on request from parent process in development mode. if (isDevelopment) { if (process.platform === 'win32') { process.on('message', (data) => { if (data === 'graceful-exit') { app.quit() } }) } else { process.on('SIGTERM', () => { app.quit() }) } } }