123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- const { app, BrowserWindow, Tray, Menu } = require('electron');
- const path = require('path');
- const serverManager = require('./serverManager');
- const menuManager = require('./menuManager');
- const updateManager = require('./updateManager');
- const constants = require('./constants');
- const { getNetworkInterfaces } = require('./networkService');
- const { exec } = require('child_process');
-
- //app.disableHardwareAcceleration(); //使用windows7 ,或者虚拟机的时候 需要验证 禁用 GPU 加速
- //app.commandLine.appendSwitch('ignore-certificate-errors')
- let win = null;
- let tray = null;
- app.isQuitting = false; // 添加标志位
- // 创建 Electron 窗口的函数
- const createWindow = async () => {
- try {
- const networks = getNetworkInterfaces();
- // console.log('Available networks:', networks);
- console.log("运行地址:", networks[0].ip)
- try {
- let filePath = path.join(constants.serverPath,'certificate.bat');
- filePath = `"${filePath}"`;
- // 使用 execFile 执行 .bat 文件
-
- // 执行 .bat 文件
- // 设置超时时间为 60 秒
- exec(filePath, (error, stdout, stderr) => {
- if (error) {
- if (error.code === 'ETIMEDOUT') {
- console.error('执行 .bat 文件超时');
- } else {
- console.error(`执行 .bat 文件时出错: ${error.message}`);
- }
- return;
- }
- if (stderr) {
- console.error(`.bat 文件执行过程中出现错误: ${stderr}`);
- return;
- }
- console.log(`.bat 文件执行结果: ${stdout}`);
- });
- } catch (error)
- {
- console.log(`脚本启动执行错误: ${error}`);
- }
- console.log("开始检查是否启动服务...")
- const isServerRunning = await serverManager.checkServerHealth();
- if (!isServerRunning) {
- console.log('Server is not running, starting it...');
- await serverManager.startServer(); // 启动 Web API
- }
- win = new BrowserWindow({
- width: 800,
- height: 600,
- webPreferences: {
- nodeIntegration: true,
- contextIsolation: false,
- },
- });
- //win.webContents.session.setCertificateVerifyProc((request, callback) => {
- // // 始终返回 0 表示验证通过
- // callback(0);
- //});///login/admin
- win.maximize();
- win.loadURL(`${constants.baseUrl}/login/admin`, {
- agent: constants.agent
- });
- // 监听窗口关闭事件,隐藏窗口而不是关闭
- win.on('close', (event) => {
- if (!app.isQuitting) {
- event.preventDefault();
- win.hide();
- }
- });
- } catch (error) {
- console.error('Error starting server or loading window:', error);
- }
- };
- const createTray = () => {
- const iconPath = path.join(__dirname, 'logo.ico'); // 你的托盘图标路径
- tray = new Tray(iconPath);
- const contextMenu = Menu.buildFromTemplate([
- {
- label: '显示',
- click: () => {
- if (win) {
- win.show();
- }
- }
- },
- {
- label: '退出',
- click: () => {
- require('electron').app.quit();
- //app.isQuitting = true;
- //app.quit();
- }
- }
- ]);
- tray.setToolTip('评测教师端');
- tray.setContextMenu(contextMenu);
- // 监听双击托盘图标事件,恢复窗口
- tray.on('double-click', () => {
- if (win) {
- if (win.isVisible()) {
- win.focus(); // 如果窗口已经显示,则聚焦窗口
- } else {
- win.show(); // 如果窗口隐藏,则显示窗口
- }
- }
- });
- };
- // 定义回调函数
- const checkForUpdatesHandler = () => {
- updateManager.checkForUpdates(win, () => {
- menuManager.createMenu(checkForUpdatesHandler); // 重新创建菜单并传递回调函数
- });
- };
- // 当 Electron 应用准备好时创建窗口
- app.whenReady().then(() => {
- process.env.NODE_OPTIONS = '--tls-min-v1.2';
- createWindow();
- createTray();
- // 创建菜单并传递回调函数
- menuManager.createMenu(checkForUpdatesHandler);
- app.on('activate', () => {
- if (BrowserWindow.getAllWindows().length === 0) {
- createWindow();
- }
- });
- // 监听 before-quit 事件,关闭 IES.ExamServer.exe 进程
- app.on('before-quit', async (event) => {
- if (app.isQuitting) {
- return; // 如果已经在退出流程中,则直接返回
- }
- app.isQuitting = true; // 标记正在退出
- event.preventDefault(); // 阻止默认的退出行为
- await serverManager.shutdownServer(); // 关闭服务器
- app.quit(); // 触发退出流程
- });
- });
- // 当所有窗口关闭时退出应用(macOS 除外)
- app.on('window-all-closed', function () {
- if (process.platform !== 'darwin') {
- app.quit();
- }
- });
|