refactor: 后端发送ws,收到后前端会自杀

This commit is contained in:
2025-09-25 22:52:56 +08:00
parent 69fac94058
commit 74ea0af9bc
5 changed files with 58 additions and 2 deletions

View File

@@ -41,7 +41,7 @@
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue'
import { BorderOutlined, CloseOutlined, CopyOutlined, MinusOutlined } from '@ant-design/icons-vue'
import { BorderOutlined, CloseOutlined, MinusOutlined } from '@ant-design/icons-vue'
import { useTheme } from '@/composables/useTheme'
import type { UpdateCheckOut } from '@/api'
import { Service, type VersionOut } from '@/api'

View File

@@ -9,6 +9,12 @@ export interface ElectronAPI {
windowMaximize: () => Promise<void>
windowClose: () => Promise<void>
windowIsMaximized: () => Promise<boolean>
appQuit: () => Promise<void>
// 进程管理
getRelatedProcesses: () => Promise<any[]>
killAllProcesses: () => Promise<{ success: boolean; error?: string }>
forceExit: () => Promise<{ success: boolean }>
// 初始化相关API
checkEnvironment: () => Promise<any>

View File

@@ -16,7 +16,12 @@ export interface ElectronAPI {
// 重启为管理员
restartAsAdmin: () => Promise<void>
appQuit: () => Promise<void>
// 进程管理
getRelatedProcesses: () => Promise<any[]>
killAllProcesses: () => Promise<{ success: boolean; error?: string }>
forceExit: () => Promise<{ success: boolean }>
// 环境检查
checkEnvironment: () => Promise<{
pythonExists: boolean

View File

@@ -99,7 +99,11 @@ export function handleMainMessage(wsMessage: any) {
if (!wsMessage || typeof wsMessage !== 'object') return
const { type, data } = wsMessage
try {
if (type === 'Message' && data && data.type === 'Countdown') {
if (type === 'Signal' && data && data.RequestClose) {
// 处理后端请求前端关闭的信号
console.log('收到后端关闭请求,开始执行应用自杀...')
handleRequestClose()
} else if (type === 'Message' && data && data.type === 'Countdown') {
// 存储倒计时消息,供 UI 回放
storePendingCountdown(data)
if (uiHooks.onCountdown) {
@@ -118,6 +122,40 @@ export function handleMainMessage(wsMessage: any) {
}
}
// 处理后端请求关闭的函数
async function handleRequestClose() {
try {
console.log('开始执行前端自杀流程...')
// 使用更激进的强制退出方法
if (window.electronAPI?.forceExit) {
console.log('执行强制退出...')
await window.electronAPI.forceExit()
} else if (window.electronAPI?.windowClose) {
// 备用方法:先尝试正常关闭
console.log('执行窗口关闭...')
await window.electronAPI.windowClose()
setTimeout(async () => {
if (window.electronAPI?.appQuit) {
await window.electronAPI.appQuit()
}
}, 500)
} else {
// 最后的备用方法
console.log('使用页面重载作为最后手段...')
window.location.reload()
}
} catch (error) {
console.error('执行自杀流程失败:', error)
// 如果所有方法都失败,尝试页面重载
try {
window.location.reload()
} catch (e) {
console.error('页面重载也失败:', e)
}
}
}
// UI 在挂载时调用,消费并回放 pending 数据
export function consumePendingTabIds(): string[] {
return popPendingTabs()

View File

@@ -793,6 +793,13 @@ export function useSchedulerLogic() {
const { type, data } = wsMessage
console.log('[Scheduler] 收到Main消息:', { type, data })
// 首先调用 schedulerHandlers 的处理函数,确保 RequestClose 等信号被正确处理
try {
schedulerHandlers.handleMainMessage(wsMessage)
} catch (e) {
console.warn('[Scheduler] schedulerHandlers.handleMainMessage error:', e)
}
if (type === 'Message' && data && data.type === 'Countdown') {
// 收到倒计时消息,由全局组件处理
console.log('[Scheduler] 收到倒计时消息,由全局组件处理:', data)