签出调整
This commit is contained in:
@@ -565,11 +565,86 @@ class Lifecycle {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有管理员权限(仅 Windows)
|
||||
*/
|
||||
checkAdminPrivileges() {
|
||||
if (process.platform !== 'win32') {
|
||||
return true; // 非 Windows 系统不需要检查
|
||||
}
|
||||
|
||||
try {
|
||||
const { execSync } = require('child_process');
|
||||
// 尝试执行需要管理员权限的命令
|
||||
execSync('net session', { stdio: 'ignore' });
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* electron app ready
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user