You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.0 KiB

1 year ago
  1. // Modules to control application life and create native browser window
  2. const { app, BrowserWindow ,dialog} = require('electron')
  3. const { execSync } = require('child_process');
  4. const path = require('node:path')
  5. function createWindow () {
  6. // Create the browser window.
  7. const mainWindow = new BrowserWindow({
  8. width: 1300,
  9. height: 800,
  10. // frame: false,
  11. icon: path.join(__dirname, 'icon.ico'),
  12. webPreferences: {
  13. preload: path.join(__dirname, 'preload.js'),
  14. webSecurity: false, // 允许跨域
  15. }
  16. })
  17. //隐藏菜单
  18. mainWindow.setMenu(null);
  19. // and load the index.html of the app.
  20. //mainWindow.loadFile('index.html')
  21. // window.isElectron = true;
  22. mainWindow.loadFile('./dist/index.html')
  23. // Open the DevTools.
  24. // mainWindow.webContents.openDevTools()
  25. mainWindow.on('close', e => {
  26. const choice = dialog.showMessageBoxSync(mainWindow, {
  27. type: 'info',
  28. buttons: ['取消', '确认'],
  29. title: '提示',
  30. message: '确定要退出系统吗?',
  31. defaultId: 0, // 默认选中“取消”按钮
  32. cancelId: 1, // 按下 Esc 键时的默认行为是“取消”
  33. })
  34. const leave = choice === 0;
  35. if (!leave) {
  36. clearCache(mainWindow)
  37. } else {
  38. // 点击取消按钮
  39. e.preventDefault();
  40. }
  41. })
  42. mainWindow.on('closed', () => {
  43. app.quit() // 关闭应用程序
  44. })
  45. }
  46. /**
  47.  * @description 清除缓存
  48.  */
  49. function clearCache(mainWindow) {
  50. // 在窗口即将关闭时清除 localStorage 和 sessionStorage
  51. mainWindow.webContents.executeJavaScript('localStorage.clear();')
  52. mainWindow.webContents.executeJavaScript('sessionStorage.clear();')
  53. }
  54. // This method will be called when Electron has finished
  55. // initialization and is ready to create browser windows.
  56. // Some APIs can only be used after this event occurs.
  57. app.whenReady().then(() => {
  58. createWindow()
  59. app.on('activate', function () {
  60. // On macOS it's common to re-create a window in the app when the
  61. // dock icon is clicked and there are no other windows open.
  62. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  63. })
  64. })
  65. // Quit when all windows are closed, except on macOS. There, it's common
  66. // for applications and their menu bar to stay active until the user quits
  67. // explicitly with Cmd + Q.
  68. app.on('window-all-closed', function () {
  69. if (process.platform !== 'darwin') app.quit()
  70. })
  71. app.on('before-quit', () => {
  72. // 获取端口
  73. let port = 30523;
  74. // 执行杀死进程的操作
  75. try {
  76. const command = `netstat -ano | findstr :${port}`;
  77. const output = execSync(command).toString();
  78. const lines = output.trim().split('\n');
  79. for (const line of lines) {
  80. const pidMatch = line.match(/LISTENING\s+(\d+)/);
  81. if (pidMatch) {
  82. const pid = pidMatch[1];
  83. execSync(`taskkill /PID ${pid} -t -f`);
  84. break;
  85. }
  86. }
  87. } catch (error) {
  88. console.error('无法杀死进程', error);
  89. }
  90. });
  91. // In this file you can include the rest of your app's specific main process
  92. // code. You can also put them in separate files and require them here.