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.

75 lines
2.1 KiB

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. // Modules to control application life and create native browser window
  2. const { app, BrowserWindow } = 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: 1600,
  9. height: 1000,
  10. icon: path.join(__dirname, 'icon.ico'),
  11. webPreferences: {
  12. preload: path.join(__dirname, 'preload.js'),
  13. webSecurity: false, // 允许跨域
  14. }
  15. })
  16. //隐藏菜单
  17. mainWindow.setMenu(null);
  18. // and load the index.html of the app.
  19. //mainWindow.loadFile('index.html')
  20. mainWindow.loadFile('./dist/index.html')
  21. // Open the DevTools.
  22. //mainWindow.webContents.openDevTools()
  23. }
  24. // This method will be called when Electron has finished
  25. // initialization and is ready to create browser windows.
  26. // Some APIs can only be used after this event occurs.
  27. app.whenReady().then(() => {
  28. createWindow()
  29. app.on('activate', function () {
  30. // On macOS it's common to re-create a window in the app when the
  31. // dock icon is clicked and there are no other windows open.
  32. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  33. })
  34. })
  35. // Quit when all windows are closed, except on macOS. There, it's common
  36. // for applications and their menu bar to stay active until the user quits
  37. // explicitly with Cmd + Q.
  38. app.on('window-all-closed', function () {
  39. if (process.platform !== 'darwin') app.quit()
  40. })
  41. app.on('before-quit', () => {
  42. // 获取端口
  43. let port = 30523;
  44. // 执行杀死进程的操作
  45. try {
  46. const command = `netstat -ano | findstr :${port}`;
  47. const output = execSync(command).toString();
  48. const lines = output.trim().split('\n');
  49. for (const line of lines) {
  50. const pidMatch = line.match(/LISTENING\s+(\d+)/);
  51. if (pidMatch) {
  52. const pid = pidMatch[1];
  53. execSync(`taskkill /PID ${pid} -t -f`);
  54. break;
  55. }
  56. }
  57. } catch (error) {
  58. console.error('无法杀死进程', error);
  59. }
  60. });
  61. // In this file you can include the rest of your app's specific main process
  62. // code. You can also put them in separate files and require them here.