127 lines
3.5 KiB
JavaScript
127 lines
3.5 KiB
JavaScript
const { BrowserWindow } = require('electron');
|
|
const path = require('path');
|
|
|
|
/**
|
|
* 启动状态管理器
|
|
* 管理启动流程和显示启动进度
|
|
*/
|
|
class StartupManager {
|
|
constructor() {
|
|
this.loadingWindow = null;
|
|
this.steps = [
|
|
{ id: 'init', label: '正在初始化应用...', progress: 0 },
|
|
{ id: 'check-mysql-port', label: '正在检查MySQL服务...', progress: 20 },
|
|
{ id: 'wait-mysql', label: '确保MySQL服务运行...', progress: 40 },
|
|
{ id: 'check-java-port', label: '正在检测后端服务端口...', progress: 60 },
|
|
{ id: 'generate-config', label: '正在生成配置文件...', progress: 70 },
|
|
{ id: 'start-java', label: '正在启动后端服务...', progress: 80 },
|
|
{ id: 'wait-java', label: '等待后端服务就绪...', progress: 90 },
|
|
{ id: 'done', label: '启动完成!', progress: 100 }
|
|
];
|
|
this.currentStep = 0;
|
|
}
|
|
|
|
/**
|
|
* 创建 Loading 窗口
|
|
*/
|
|
createLoadingWindow() {
|
|
const isDev = !process.resourcesPath;
|
|
const iconPath = isDev
|
|
? path.join(__dirname, '..', 'public', 'images', 'icon.png')
|
|
: path.join(process.resourcesPath, 'app.asar.unpacked', 'public', 'images', 'icon.png');
|
|
|
|
this.loadingWindow = new BrowserWindow({
|
|
width: 500,
|
|
height: 300,
|
|
frame: false,
|
|
transparent: true,
|
|
resizable: false,
|
|
alwaysOnTop: true,
|
|
skipTaskbar: true, // 不在任务栏显示
|
|
icon: iconPath,
|
|
webPreferences: {
|
|
nodeIntegration: true,
|
|
contextIsolation: false
|
|
}
|
|
});
|
|
|
|
// 加载 loading 页面
|
|
const isDev2 = !process.resourcesPath;
|
|
const loadingHtml = isDev2
|
|
? path.join(__dirname, '..', 'public', 'html', 'loading.html')
|
|
: path.join(process.resourcesPath, 'app.asar', 'public', 'html', 'loading.html');
|
|
|
|
console.log('[StartupManager] Loading HTML from:', loadingHtml);
|
|
this.loadingWindow.loadFile(loadingHtml);
|
|
|
|
return this.loadingWindow;
|
|
}
|
|
|
|
/**
|
|
* 更新启动进度
|
|
* @param {string} stepId - 步骤ID
|
|
* @param {object} extraInfo - 额外信息
|
|
*/
|
|
updateProgress(stepId, extraInfo = {}) {
|
|
const stepIndex = this.steps.findIndex(s => s.id === stepId);
|
|
|
|
if (stepIndex !== -1) {
|
|
this.currentStep = stepIndex;
|
|
const step = this.steps[stepIndex];
|
|
|
|
const progressData = {
|
|
step: stepId,
|
|
label: step.label,
|
|
progress: step.progress,
|
|
...extraInfo
|
|
};
|
|
|
|
// 发送进度到 loading 窗口
|
|
if (this.loadingWindow && !this.loadingWindow.isDestroyed()) {
|
|
this.loadingWindow.webContents.send('startup-progress', progressData);
|
|
}
|
|
|
|
console.log(`[StartupManager] ${step.label} (${step.progress}%)`, extraInfo);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 显示错误信息
|
|
* @param {string} error - 错误信息
|
|
*/
|
|
showError(error) {
|
|
if (this.loadingWindow && !this.loadingWindow.isDestroyed()) {
|
|
this.loadingWindow.webContents.send('startup-error', { error });
|
|
}
|
|
console.error('[StartupManager] Error:', error);
|
|
}
|
|
|
|
/**
|
|
* 关闭 Loading 窗口
|
|
*/
|
|
closeLoadingWindow() {
|
|
if (this.loadingWindow && !this.loadingWindow.isDestroyed()) {
|
|
// 使用 destroy() 而不是 close() 确保窗口被完全销毁
|
|
this.loadingWindow.destroy();
|
|
this.loadingWindow = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取所有步骤
|
|
*/
|
|
getSteps() {
|
|
return this.steps;
|
|
}
|
|
|
|
/**
|
|
* 获取当前步骤
|
|
*/
|
|
getCurrentStep() {
|
|
return this.steps[this.currentStep];
|
|
}
|
|
}
|
|
|
|
module.exports = StartupManager;
|
|
|