From 7b98fca398da6a0e8d4e3755200c2bcfa99a3e90 Mon Sep 17 00:00:00 2001 From: fhysy <1149505133@qq.com> Date: Mon, 15 Jul 2024 13:57:35 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A5=20feat(=E6=96=AD=E8=B7=AF=E5=99=A8?= =?UTF-8?q?):=20=E8=B0=83=E6=95=B4=E4=BE=A7=E8=BE=B9=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F=E3=80=81=E9=BB=98=E8=AE=A4=E9=80=89=E4=B8=AD?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E3=80=81=E6=B7=BB=E5=8A=A0=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E9=80=80=E5=87=BA=E5=85=B3=E9=97=AD=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=E6=9C=8D=E5=8A=A1=E5=8F=8A=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/index.js | 76 ++++++++++++++++++++------- src/renderer/src/components/Aside.vue | 7 +-- src/renderer/src/views/HomeView.vue | 2 +- 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index 2ce4c67..ac2ef6e 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -3,7 +3,22 @@ import { join } from 'path' import { electronApp, optimizer, is } from '@electron-toolkit/utils' import icon from '../../resources/icon.png?asset' -app.commandLine.appendSwitch('disable-web-security'); +//关闭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({ @@ -11,7 +26,6 @@ function createWindow() { height: 700, show: false, autoHideMenuBar: true, - // ...(process.platform === 'linux' ? { icon } : {}), icon, webPreferences: { preload: join(__dirname, '../preload/index.js'), @@ -60,38 +74,47 @@ if (!gotTheLock) { app.quit() } else { // 如果检测到有同样的该程序正在试图启动... - app.on( - 'second-instance', - (event, commandLine, workingDirectory, additionalData) => { - if (myWindow) { - // 弹出系统提示对话框 - dialog.showMessageBox({ - message: '此程序已经正在运行', - }) - // 如果该程序窗口处于最小化状态,则恢复窗口 - if (myWindow.isMinimized()) myWindow.restore() - // 将该程序窗口置为当前聚焦态 - myWindow.focus() - } + 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 { spawn } = require('child_process') 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') } - const child = spawn('./main.exe', [], { cwd: exePath, stdio: ['ignore', 'pipe', 'pipe'] }) + //查询所有插件目录下的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()) @@ -108,6 +131,11 @@ if (!gotTheLock) { 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') @@ -124,7 +152,7 @@ if (!gotTheLock) { myWindow = createWindow() - app.on('activate', function() { + 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() @@ -136,6 +164,14 @@ if (!gotTheLock) { // 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() } }) diff --git a/src/renderer/src/components/Aside.vue b/src/renderer/src/components/Aside.vue index 78a549e..d18e082 100644 --- a/src/renderer/src/components/Aside.vue +++ b/src/renderer/src/components/Aside.vue @@ -13,11 +13,11 @@ const settings = defineProps({ - 断路器调试 + 断路器调试 @@ -35,10 +35,11 @@ const settings = defineProps({ } .el-menu{ background: #f6f6f6; + padding: 0 10px; } .el-menu-item{ - padding: 0 10px; border-radius: 10px; + /*padding-left: 5px !important;*/ } .el-menu-item.is-active{ background: #c8cde4; diff --git a/src/renderer/src/views/HomeView.vue b/src/renderer/src/views/HomeView.vue index 724c6d3..33334fc 100644 --- a/src/renderer/src/views/HomeView.vue +++ b/src/renderer/src/views/HomeView.vue @@ -7,7 +7,7 @@ import Aside from '@renderer/components/Aside.vue'
- +