refactor: 后端发送ws,收到后前端会自杀
This commit is contained in:
@@ -41,7 +41,7 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { onBeforeUnmount, onMounted, ref } from 'vue'
|
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 { useTheme } from '@/composables/useTheme'
|
||||||
import type { UpdateCheckOut } from '@/api'
|
import type { UpdateCheckOut } from '@/api'
|
||||||
import { Service, type VersionOut } from '@/api'
|
import { Service, type VersionOut } from '@/api'
|
||||||
|
|||||||
6
frontend/src/types/electron.d.ts
vendored
6
frontend/src/types/electron.d.ts
vendored
@@ -9,6 +9,12 @@ export interface ElectronAPI {
|
|||||||
windowMaximize: () => Promise<void>
|
windowMaximize: () => Promise<void>
|
||||||
windowClose: () => Promise<void>
|
windowClose: () => Promise<void>
|
||||||
windowIsMaximized: () => Promise<boolean>
|
windowIsMaximized: () => Promise<boolean>
|
||||||
|
appQuit: () => Promise<void>
|
||||||
|
|
||||||
|
// 进程管理
|
||||||
|
getRelatedProcesses: () => Promise<any[]>
|
||||||
|
killAllProcesses: () => Promise<{ success: boolean; error?: string }>
|
||||||
|
forceExit: () => Promise<{ success: boolean }>
|
||||||
|
|
||||||
// 初始化相关API
|
// 初始化相关API
|
||||||
checkEnvironment: () => Promise<any>
|
checkEnvironment: () => Promise<any>
|
||||||
|
|||||||
@@ -16,7 +16,12 @@ export interface ElectronAPI {
|
|||||||
|
|
||||||
// 重启为管理员
|
// 重启为管理员
|
||||||
restartAsAdmin: () => Promise<void>
|
restartAsAdmin: () => Promise<void>
|
||||||
|
appQuit: () => Promise<void>
|
||||||
|
|
||||||
|
// 进程管理
|
||||||
|
getRelatedProcesses: () => Promise<any[]>
|
||||||
|
killAllProcesses: () => Promise<{ success: boolean; error?: string }>
|
||||||
|
forceExit: () => Promise<{ success: boolean }>
|
||||||
// 环境检查
|
// 环境检查
|
||||||
checkEnvironment: () => Promise<{
|
checkEnvironment: () => Promise<{
|
||||||
pythonExists: boolean
|
pythonExists: boolean
|
||||||
|
|||||||
@@ -99,7 +99,11 @@ export function handleMainMessage(wsMessage: any) {
|
|||||||
if (!wsMessage || typeof wsMessage !== 'object') return
|
if (!wsMessage || typeof wsMessage !== 'object') return
|
||||||
const { type, data } = wsMessage
|
const { type, data } = wsMessage
|
||||||
try {
|
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 回放
|
// 存储倒计时消息,供 UI 回放
|
||||||
storePendingCountdown(data)
|
storePendingCountdown(data)
|
||||||
if (uiHooks.onCountdown) {
|
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 数据
|
// UI 在挂载时调用,消费并回放 pending 数据
|
||||||
export function consumePendingTabIds(): string[] {
|
export function consumePendingTabIds(): string[] {
|
||||||
return popPendingTabs()
|
return popPendingTabs()
|
||||||
|
|||||||
@@ -793,6 +793,13 @@ export function useSchedulerLogic() {
|
|||||||
const { type, data } = wsMessage
|
const { type, data } = wsMessage
|
||||||
console.log('[Scheduler] 收到Main消息:', { type, data })
|
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') {
|
if (type === 'Message' && data && data.type === 'Countdown') {
|
||||||
// 收到倒计时消息,由全局组件处理
|
// 收到倒计时消息,由全局组件处理
|
||||||
console.log('[Scheduler] 收到倒计时消息,由全局组件处理:', data)
|
console.log('[Scheduler] 收到倒计时消息,由全局组件处理:', data)
|
||||||
|
|||||||
Reference in New Issue
Block a user