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.

108 lines
3.1 KiB

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