import { app, shell, BrowserWindow, ipcMain, globalShortcut, dialog } from 'electron' import { join } from 'path' import { electronApp, optimizer, is } from '@electron-toolkit/utils' import icon from '../../resources/icon.png?asset' //关闭exe命令 function killProcessByName(processName) { const command = `taskkill /IM ${processName} /F` const { exec } = require('child_process') exec(command, (error, stdout, stderr) => { if (error) { console.error(`执行的错误: ${error}`) return } console.log(`stdout: ${stdout}`) console.error(`stderr: ${stderr}`) }) } app.commandLine.appendSwitch('disable-web-security') function createWindow() { // Create the browser window. const mainWindow = new BrowserWindow({ width: 1100, height: 700, show: false, autoHideMenuBar: true, icon, webPreferences: { preload: join(__dirname, '../preload/index.js'), sandbox: false, nodeIntegration: true, // 禁用同源策略,允许跨域请求 webSecurity: false, contextIsolation: false, csp: "default-src 'self'; connect-src 'self' http://127.0.0.1:8000 http://192.168.1.17:8000 ws://192.168.1.17:8000 ws://127.0.0.1:8000", // 禁止build环境使用DevTool // devTools: is.dev ? true : false devTools: true } }) //开启调试 // mainWindow.webContents.openDevTools() mainWindow.on('ready-to-show', () => { mainWindow.show() }) mainWindow.webContents.setWindowOpenHandler((details) => { shell.openExternal(details.url) return { action: 'deny' } }) // HMR for renderer base on electron-vite cli. // Load the remote URL for development or the local html file for production. if (is.dev && process.env['ELECTRON_RENDERER_URL']) { mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL']) } else { mainWindow.loadFile(join(__dirname, '../renderer/index.html')) } // 设置DevTools快捷键 // 设置DevTools快捷键 globalShortcut.register('CommandOrControl+Shift+i', function () { mainWindow.webContents.openDevTools() }) return mainWindow } // 程序单例模式 let myWindow = null const gotTheLock = app.requestSingleInstanceLock() if (!gotTheLock) { // 如果已经有同样的该程序正在运行,则不启动 app.quit() } else { // 如果检测到有同样的该程序正在试图启动... app.on('second-instance', () => { if (myWindow) { // 弹出系统提示对话框 dialog.showMessageBox({ message: '此程序已经正在运行' }) // 如果该程序窗口处于最小化状态,则恢复窗口 if (myWindow.isMinimized()) myWindow.restore() // 将该程序窗口置为当前聚焦态 myWindow.focus() } }) // 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. const { spawn } = require('child_process') let child = null let exePluginsExeList = [] app.whenReady().then(() => { const fs = require('fs') const path = require('path') const logStream = fs.createWriteStream('app.log', { flags: 'a' }) // 日志文件 let exePath = path.resolve('./resources/app.asar.unpacked/resources/plugin-test') let exePluginsPath = path.resolve('./resources/app.asar.unpacked/resources/plugin-test/plugins') if (process.env.NODE_ENV === 'development') { exePath = path.resolve('./resources/plugin-test') exePluginsPath = path.resolve('./resources/plugin-test/plugins') } //查询所有插件目录下的exe文件 fs.readdir(exePluginsPath, (err, files) => { if (err) { return console.log('Unable to scan directory: ' + err) } // 筛选出以.exe结尾的文件 exePluginsExeList = files.filter((file) => path.extname(file).toLowerCase() === '.exe') }) child = spawn('./main.exe', [], { cwd: exePath, stdio: ['ignore', 'pipe', 'pipe'] }) child.stdout.on('data', (data) => { console.log('stdout:', data.toString()) logStream.write(data) }) child.stderr.on('data', (data) => { console.error('stderr:', data.toString()) logStream.write(data) }) child.on('close', (code) => { console.log(`子进程已退出,退出码 ${code}`) logStream.end() }) child.on('exit', (code, signal) => { console.log(`子进程已退出,退出码 ${code},信号: ${signal}`) logStream.write(`子进程已退出,退出码 ${code},信号: ${signal}`) logStream.end() }) // Set app user model id for windows electronApp.setAppUserModelId('com.electron') // Default open or close DevTools by F12 in development // and ignore CommandOrControl + R in production. // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils app.on('browser-window-created', (_, window) => { optimizer.watchWindowShortcuts(window) }) // IPC test ipcMain.on('ping', () => console.log('pong')) myWindow = createWindow() app.on('activate', function () { // 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() }) }) // Quit when all windows are closed, except on macOS. There, it's common // for applications and their menu bar to stay active until the user quits // explicitly with Cmd + Q. app.on('window-all-closed', () => { if (process.platform !== 'darwin') { // 循环关闭插件 EXE 列表 exePluginsExeList.forEach((exe) => { killProcessByName(exe) }) // child.kill('SIGTERM'); // 或者使用 'SIGKILL' 来强制关闭 // killProcessByName('gateway.exe'); // 循环关闭后端服务 killProcessByName('main.exe') app.quit() } }) } // In this file you can include the rest of your app"s specific main process // code. You can also put them in separate files and require them here.