app.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. const { app, BrowserWindow, Tray, Menu } = require('electron');
  2. const path = require('path');
  3. const serverManager = require('./serverManager');
  4. const menuManager = require('./menuManager');
  5. const updateManager = require('./updateManager');
  6. const constants = require('./constants');
  7. const { getNetworkInterfaces } = require('./networkService');
  8. const { exec } = require('child_process');
  9. //app.disableHardwareAcceleration(); //使用windows7 ,或者虚拟机的时候 需要验证 禁用 GPU 加速
  10. //app.commandLine.appendSwitch('ignore-certificate-errors')
  11. let win = null;
  12. let tray = null;
  13. app.isQuitting = false; // 添加标志位
  14. // 创建 Electron 窗口的函数
  15. const createWindow = async () => {
  16. try {
  17. const networks = getNetworkInterfaces();
  18. // console.log('Available networks:', networks);
  19. console.log("运行地址:", networks[0].ip)
  20. try {
  21. let filePath = path.join(constants.serverPath,'certificate.bat');
  22. filePath = `"${filePath}"`;
  23. // 使用 execFile 执行 .bat 文件
  24. // 执行 .bat 文件
  25. // 设置超时时间为 60 秒
  26. exec(filePath, (error, stdout, stderr) => {
  27. if (error) {
  28. if (error.code === 'ETIMEDOUT') {
  29. console.error('执行 .bat 文件超时');
  30. } else {
  31. console.error(`执行 .bat 文件时出错: ${error.message}`);
  32. }
  33. return;
  34. }
  35. if (stderr) {
  36. console.error(`.bat 文件执行过程中出现错误: ${stderr}`);
  37. return;
  38. }
  39. console.log(`.bat 文件执行结果: ${stdout}`);
  40. });
  41. } catch (error)
  42. {
  43. console.log(`脚本启动执行错误: ${error}`);
  44. }
  45. console.log("开始检查是否启动服务...")
  46. const isServerRunning = await serverManager.checkServerHealth();
  47. if (!isServerRunning) {
  48. console.log('Server is not running, starting it...');
  49. await serverManager.startServer(); // 启动 Web API
  50. }
  51. win = new BrowserWindow({
  52. width: 800,
  53. height: 600,
  54. webPreferences: {
  55. nodeIntegration: true,
  56. contextIsolation: false,
  57. },
  58. });
  59. //win.webContents.session.setCertificateVerifyProc((request, callback) => {
  60. // // 始终返回 0 表示验证通过
  61. // callback(0);
  62. //});///login/admin
  63. win.maximize();
  64. win.loadURL(`${constants.baseUrl}/login/admin`, {
  65. agent: constants.agent
  66. });
  67. // 监听窗口关闭事件,隐藏窗口而不是关闭
  68. win.on('close', (event) => {
  69. if (!app.isQuitting) {
  70. event.preventDefault();
  71. win.hide();
  72. }
  73. });
  74. } catch (error) {
  75. console.error('Error starting server or loading window:', error);
  76. }
  77. };
  78. const createTray = () => {
  79. const iconPath = path.join(__dirname, 'logo.ico'); // 你的托盘图标路径
  80. tray = new Tray(iconPath);
  81. const contextMenu = Menu.buildFromTemplate([
  82. {
  83. label: '显示',
  84. click: () => {
  85. if (win) {
  86. win.show();
  87. }
  88. }
  89. },
  90. {
  91. label: '退出',
  92. click: () => {
  93. require('electron').app.quit();
  94. //app.isQuitting = true;
  95. //app.quit();
  96. }
  97. }
  98. ]);
  99. tray.setToolTip('评测教师端');
  100. tray.setContextMenu(contextMenu);
  101. // 监听双击托盘图标事件,恢复窗口
  102. tray.on('double-click', () => {
  103. if (win) {
  104. if (win.isVisible()) {
  105. win.focus(); // 如果窗口已经显示,则聚焦窗口
  106. } else {
  107. win.show(); // 如果窗口隐藏,则显示窗口
  108. }
  109. }
  110. });
  111. };
  112. // 定义回调函数
  113. const checkForUpdatesHandler = () => {
  114. updateManager.checkForUpdates(win, () => {
  115. menuManager.createMenu(checkForUpdatesHandler); // 重新创建菜单并传递回调函数
  116. });
  117. };
  118. // 当 Electron 应用准备好时创建窗口
  119. app.whenReady().then(() => {
  120. process.env.NODE_OPTIONS = '--tls-min-v1.2';
  121. createWindow();
  122. createTray();
  123. // 创建菜单并传递回调函数
  124. menuManager.createMenu(checkForUpdatesHandler);
  125. app.on('activate', () => {
  126. if (BrowserWindow.getAllWindows().length === 0) {
  127. createWindow();
  128. }
  129. });
  130. // 监听 before-quit 事件,关闭 IES.ExamServer.exe 进程
  131. app.on('before-quit', async (event) => {
  132. if (app.isQuitting) {
  133. return; // 如果已经在退出流程中,则直接返回
  134. }
  135. app.isQuitting = true; // 标记正在退出
  136. event.preventDefault(); // 阻止默认的退出行为
  137. await serverManager.shutdownServer(); // 关闭服务器
  138. app.quit(); // 触发退出流程
  139. });
  140. });
  141. // 当所有窗口关闭时退出应用(macOS 除外)
  142. app.on('window-all-closed', function () {
  143. if (process.platform !== 'darwin') {
  144. app.quit();
  145. }
  146. });