签出调整

This commit is contained in:
2025-11-27 17:10:08 +08:00
parent 1119d6a7a8
commit a6d1195f13
10 changed files with 61 additions and 291 deletions

View File

@@ -267,40 +267,41 @@ class Lifecycle {
const win = getMainWindow();
win.show();
win.focus();
// 添加主窗口关闭事件监听
// 窗口关闭时,使用 Electron 原生对话框确认
win.on('close', async (event) => {
// 总是弹出确认对话框
if (global.isConfirmedExit) {
logger.info('[lifecycle] Exit already confirmed, proceeding');
return;
}
event.preventDefault();
logger.info('[lifecycle] Window close intercepted, showing exit confirm dialog');
const { dialog } = require('electron');
const { app } = require('electron');
const response = await dialog.showMessageBox(win, {
const { response } = await dialog.showMessageBox(win, {
type: 'question',
buttons: ['取消', '退出'],
defaultId: 1,
cancelId: 0,
title: '退出确认',
message: '确定要退出应用吗?',
buttons: ['取消', '退出'],
defaultId: 0,
cancelId: 0
noLink: true
});
if (response.response === 1) {
// 用户确认退出
if (response === 1) {
logger.info('[lifecycle] User confirmed exit');
// 移除所有监听器避免循环
win.removeAllListeners('close');
// 执行清理
global.isConfirmedExit = true;
await this.cleanup();
// 退出应用
if (win && !win.isDestroyed()) {
win.destroy();
}
app.quit();
} else {
logger.info('[lifecycle] User cancelled exit');
}
// 用户取消,什么都不做(已经 preventDefault
});
// 立即刷新一次,确保显示最新内容
setTimeout(() => {
if (win && !win.isDestroyed()) {
@@ -496,22 +497,14 @@ class Lifecycle {
try {
// 显示提示对话框
const { response } = await dialog.showMessageBox({
await dialog.showMessageBox({
type: 'warning',
title: '需要管理员权限',
message: '安装 MySQL 服务需要管理员权限',
detail: '应用将以管理员身份重新启动',
buttons: ['取消', '确定'],
defaultId: 1,
cancelId: 0
title: '告警',
message: '需要管理员权限',
buttons: ['确定']
});
if (response === 0) {
// 用户取消,关闭应用
logger.info('[lifecycle] User cancelled admin elevation');
app.quit();
return;
}
// 用户点击确定,以管理员身份重启
// 获取应用可执行文件路径
const exePath = app.getPath('exe');
@@ -588,63 +581,6 @@ class Lifecycle {
*/
async electronAppReady() {
logger.info('[lifecycle] electron-app-ready');
// 检查管理员权限
const hasAdminPrivileges = this.checkAdminPrivileges();
logger.info('[lifecycle] Has admin privileges:', hasAdminPrivileges);
if (!hasAdminPrivileges) {
logger.warn('[lifecycle] No admin privileges, will request elevation when needed');
// 立即请求管理员权限
const { dialog, app } = require('electron');
const { spawn } = require('child_process');
setTimeout(async () => {
const { response } = await dialog.showMessageBox({
type: 'warning',
title: '需要管理员权限',
message: 'NPQS9100 需要管理员权限来管理 MySQL 服务',
detail: '应用将以管理员身份重新启动。\n\n如果您拒绝应用可能无法正常工作。',
buttons: ['退出应用', '以管理员身份启动'],
defaultId: 1,
cancelId: 0
});
if (response === 0) {
logger.info('[lifecycle] User declined admin elevation, quitting');
app.quit();
return;
}
// 以管理员身份重启
try {
const exePath = app.getPath('exe');
logger.info('[lifecycle] Restarting with admin privileges:', exePath);
const psCommand = `Start-Process -FilePath "${exePath}" -Verb RunAs`;
const child = spawn('powershell.exe', ['-Command', psCommand], {
detached: true,
stdio: 'ignore',
windowsHide: true
});
child.unref();
// 设置标记,跳过清理
this.isRestartingForAdmin = true;
// 退出当前实例
setTimeout(() => {
app.quit();
}, 500);
} catch (error) {
logger.error('[lifecycle] Failed to restart as admin:', error);
dialog.showErrorBox('启动失败', '无法以管理员身份启动应用,请手动右键选择"以管理员身份运行"');
app.quit();
}
}, 1000);
}
}
/**
@@ -653,6 +589,17 @@ class Lifecycle {
async windowReady() {
logger.info('[lifecycle] window-ready hook triggered');
// 在创建任何窗口之前,先检查管理员权限
const hasAdminPrivileges = this.checkAdminPrivileges();
logger.info('[lifecycle] Has admin privileges:', hasAdminPrivileges);
if (!hasAdminPrivileges) {
logger.warn('[lifecycle] No admin privileges, requesting elevation');
// 调用已有的 restartAsAdmin 方法,避免代码重复
await this.restartAsAdmin();
return; // 阻止后续代码执行
}
// 创建日志管理器(但不显示窗口,仅用于写日志文件)
logger.info('[lifecycle] Creating log window manager...');
this.logWindowManager = new LogWindowManager();