212 lines
5.1 KiB
JavaScript
212 lines
5.1 KiB
JavaScript
// 确保所有导入都使用 ES 模块语法
|
||
import { app, BrowserWindow, ipcMain, dialog } from "electron";
|
||
import path from "path";
|
||
import { fileURLToPath } from "url";
|
||
import {
|
||
checkDatabaseInitialized,
|
||
initializeDatabase,
|
||
} from "./db/index.js";
|
||
// 导入配置项服务
|
||
import {
|
||
initConfigIpc,
|
||
} from "./service/configService.js";
|
||
// 导入字典服务 - 使用实际导出的函数名称
|
||
import {
|
||
initDictIpc,
|
||
} from "./service/dictService.js";
|
||
// 导入题干服务
|
||
import {
|
||
initQuestionIpc,
|
||
} from './service/questionService.js';
|
||
import {
|
||
initExamIpc,
|
||
} from "./service/examService.js";
|
||
|
||
// 添加考生服务导入
|
||
import {
|
||
initExamineeIpc
|
||
} from "./service/examineeService.js";
|
||
|
||
import {
|
||
initExamingIpc
|
||
} from "./service/examingService.js";
|
||
|
||
import {
|
||
initFileIpc
|
||
} from "./service/fileService.js";
|
||
|
||
// 定义 __dirname 和 __filename
|
||
const __filename = fileURLToPath(import.meta.url);
|
||
const __dirname = path.dirname(__filename);
|
||
|
||
// 确保在应用最开始处添加单实例锁检查
|
||
// 尝试获取单实例锁
|
||
const gotTheLock = app.requestSingleInstanceLock();
|
||
|
||
// 如果获取锁失败,说明已有实例在运行,直接退出
|
||
if (!gotTheLock) {
|
||
app.quit();
|
||
} else {
|
||
// 设置第二个实例启动时的处理
|
||
app.on("second-instance", (event, commandLine, workingDirectory) => {
|
||
// 当用户尝试启动第二个实例时,聚焦到已有的主窗口
|
||
const mainWindow = BrowserWindow.getAllWindows()[0];
|
||
if (mainWindow) {
|
||
if (mainWindow.isMinimized()) mainWindow.restore();
|
||
mainWindow.focus();
|
||
}
|
||
});
|
||
}
|
||
|
||
// 保存主窗口引用
|
||
let mainWindow = null;
|
||
|
||
function createWindow() {
|
||
// 如果已经有窗口,直接返回
|
||
if (mainWindow) {
|
||
return;
|
||
}
|
||
|
||
mainWindow = new BrowserWindow({
|
||
// 将fullscreen改为false,添加maximized: true
|
||
fullscreen: false,
|
||
maximized: true,
|
||
webPreferences: {
|
||
preload: path.join(__dirname, "../electron/preload.js"),
|
||
nodeIntegration: false,
|
||
contextIsolation: true,
|
||
},
|
||
});
|
||
|
||
if (process.env.NODE_ENV === "development") {
|
||
mainWindow.loadURL("http://localhost:5173/");
|
||
mainWindow.webContents.openDevTools();
|
||
} else {
|
||
mainWindow.loadFile(path.join(__dirname, "../dist/index.html"));
|
||
}
|
||
|
||
// 添加窗口关闭前的确认对话框
|
||
mainWindow.on('close', (event) => {
|
||
// 阻止默认关闭行为
|
||
event.preventDefault();
|
||
|
||
// 显示确认对话框
|
||
dialog.showMessageBox({
|
||
type: 'warning',
|
||
title: '关闭确认',
|
||
message: '系统关闭后,已进行中的试卷将不会被保存,是否确认要关闭?',
|
||
buttons: ['取消', '确认关闭'],
|
||
defaultId: 0
|
||
}).then((result) => {
|
||
// 如果用户点击确认关闭
|
||
if (result.response === 1) {
|
||
// 允许窗口关闭
|
||
mainWindow.destroy();
|
||
}
|
||
// 否则取消关闭
|
||
});
|
||
});
|
||
|
||
// 当窗口关闭时,清空引用
|
||
mainWindow.on("closed", () => {
|
||
mainWindow = null;
|
||
});
|
||
}
|
||
|
||
// Initalize app
|
||
app.whenReady().then(() => {
|
||
setupApp();
|
||
createWindow();
|
||
setupIpcMain();
|
||
});
|
||
|
||
app.on("window-all-closed", () => {
|
||
// 在所有窗口关闭后,清空主窗口引用
|
||
mainWindow = null;
|
||
// 对于非macOS平台,仍然需要退出应用
|
||
if (process.platform !== "darwin") {
|
||
app.quit();
|
||
}
|
||
});
|
||
|
||
app.on("activate", () => {
|
||
// 只有当没有窗口时才创建新窗口
|
||
if (BrowserWindow.getAllWindows().length === 0) {
|
||
createWindow();
|
||
}
|
||
});
|
||
|
||
// Check database initialization status
|
||
async function setupApp() {
|
||
try {
|
||
console.log("应用启动 - 检查数据库初始化状态...");
|
||
|
||
// 使用全局变量防止重复初始化检查
|
||
if (global.dbInitCheck) {
|
||
console.log("数据库初始化检查已完成");
|
||
return;
|
||
}
|
||
global.dbInitCheck = true;
|
||
|
||
const isInitialized = await checkDatabaseInitialized();
|
||
console.log("数据库初始化状态:", isInitialized);
|
||
|
||
// 只检查状态,不自动初始化
|
||
} catch (error) {
|
||
console.error("数据库检查过程中出错:", error);
|
||
}
|
||
}
|
||
|
||
function setupIpcMain() {
|
||
|
||
// 数据库相关
|
||
ipcMain.handle("check-database-initialized", async () => {
|
||
try {
|
||
return await checkDatabaseInitialized();
|
||
} catch (error) {
|
||
console.error("Failed to check database initialization:", error);
|
||
return false;
|
||
}
|
||
});
|
||
|
||
ipcMain.handle("initialize-database", async () => {
|
||
try {
|
||
return await initializeDatabase();
|
||
} catch (error) {
|
||
console.error("Failed to initialize database:", error);
|
||
return false;
|
||
}
|
||
});
|
||
|
||
// 初始化config相关IPC
|
||
initConfigIpc(ipcMain);
|
||
|
||
// 初始化dict相关IPC
|
||
initDictIpc(ipcMain);
|
||
|
||
// 初始化exam相关IPC
|
||
initExamIpc(ipcMain);
|
||
|
||
// 初始化question相关IPC
|
||
initQuestionIpc(ipcMain);
|
||
|
||
// 初始化考生相关IPC
|
||
initExamineeIpc(ipcMain);
|
||
|
||
initExamingIpc(ipcMain);
|
||
|
||
initFileIpc(ipcMain);
|
||
}
|
||
|
||
// 确保在 app.whenReady() 中调用 setupIpcMain()
|
||
app.whenReady().then(() => {
|
||
setupApp();
|
||
createWindow();
|
||
setupIpcMain();
|
||
});
|
||
|
||
// 在应用退出前关闭所有数据库连接
|
||
app.on("will-quit", () => {
|
||
console.log("应用即将退出...");
|
||
});
|