@@ -7,6 +7,7 @@ const fs = require('fs')
7
7
const os = require ( 'os' ) ;
8
8
9
9
let pythonExec ;
10
+ let isQuitting = false ;
10
11
11
12
// 判断操作系统
12
13
if ( os . platform ( ) === 'win32' ) {
@@ -436,6 +437,16 @@ app.whenReady().then(async () => {
436
437
y : arg . y
437
438
} ) ;
438
439
} ) ;
440
+ // 监听关闭事件
441
+ ipcMain . handle ( 'request-stop-qqbot' , async ( event ) => {
442
+ const win = BrowserWindow . getAllWindows ( ) [ 0 ] ; // 获取主窗口
443
+ if ( win && ! win . isDestroyed ( ) ) {
444
+ // 通过webContents执行渲染进程方法
445
+ await win . webContents . executeJavaScript ( `
446
+ window.stopQQBotHandler && window.stopQQBotHandler()
447
+ ` ) ;
448
+ }
449
+ } ) ;
439
450
// 其他IPC处理...
440
451
ipcMain . on ( 'open-external' , ( event , url ) => {
441
452
shell . openExternal ( url )
@@ -481,17 +492,48 @@ app.whenReady().then(async () => {
481
492
}
482
493
} )
483
494
495
+
496
+
484
497
// 应用退出处理
485
- app . on ( 'before-quit' , ( ) => {
486
- app . isQuitting = true
487
- if ( backendProcess ) {
488
- if ( process . platform === 'win32' ) {
489
- spawn ( 'taskkill' , [ '/pid' , backendProcess . pid , '/f' , '/t' ] )
490
- } else {
491
- backendProcess . kill ( 'SIGKILL' )
498
+ app . on ( 'before-quit' , async ( event ) => {
499
+ // 防止重复处理退出事件
500
+ if ( isQuitting ) return ;
501
+
502
+ // 标记退出状态并阻止默认退出行为
503
+ isQuitting = true ;
504
+ event . preventDefault ( ) ;
505
+
506
+ try {
507
+ const mainWindow = BrowserWindow . getAllWindows ( ) [ 0 ] ;
508
+
509
+ // 1. 尝试停止QQ机器人
510
+ if ( mainWindow && ! mainWindow . isDestroyed ( ) ) {
511
+ await mainWindow . webContents . executeJavaScript ( `
512
+ if (window.stopQQBotHandler) {
513
+ window.stopQQBotHandler();
514
+ }
515
+ ` ) ;
516
+
517
+ // 等待机器人停止(最多1秒)
518
+ await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
519
+ }
520
+
521
+ // 2. 停止后端进程
522
+ if ( backendProcess ) {
523
+ if ( process . platform === 'win32' ) {
524
+ spawn ( 'taskkill' , [ '/pid' , backendProcess . pid , '/f' , '/t' ] ) ;
525
+ } else {
526
+ backendProcess . kill ( 'SIGKILL' ) ;
527
+ }
528
+ backendProcess = null ;
492
529
}
530
+ } catch ( error ) {
531
+ console . error ( '退出时发生错误:' , error ) ;
532
+ } finally {
533
+ // 3. 最终退出应用
534
+ app . exit ( 0 ) ;
493
535
}
494
- } )
536
+ } ) ;
495
537
496
538
// 自动退出处理
497
539
app . on ( 'window-all-closed' , ( ) => {
0 commit comments