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.

99 lines
2.7 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
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. 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. } else {
  37. // 点击取消按钮
  38. e.preventDefault();
  39. }
  40. })
  41. mainWindow.on('closed', () => {
  42. app.quit() // 关闭应用程序
  43. })
  44. }
  45. // This method will be called when Electron has finished
  46. // initialization and is ready to create browser windows.
  47. // Some APIs can only be used after this event occurs.
  48. app.whenReady().then(() => {
  49. createWindow()
  50. app.on('activate', function () {
  51. // On macOS it's common to re-create a window in the app when the
  52. // dock icon is clicked and there are no other windows open.
  53. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  54. })
  55. })
  56. // Quit when all windows are closed, except on macOS. There, it's common
  57. // for applications and their menu bar to stay active until the user quits
  58. // explicitly with Cmd + Q.
  59. app.on('window-all-closed', function () {
  60. if (process.platform !== 'darwin') app.quit()
  61. })
  62. app.on('before-quit', () => {
  63. // 获取端口
  64. let port = 30523;
  65. // 执行杀死进程的操作
  66. try {
  67. const command = `netstat -ano | findstr :${port}`;
  68. const output = execSync(command).toString();
  69. const lines = output.trim().split('\n');
  70. for (const line of lines) {
  71. const pidMatch = line.match(/LISTENING\s+(\d+)/);
  72. if (pidMatch) {
  73. const pid = pidMatch[1];
  74. execSync(`taskkill /PID ${pid} -t -f`);
  75. break;
  76. }
  77. }
  78. } catch (error) {
  79. console.error('无法杀死进程', error);
  80. }
  81. });
  82. // In this file you can include the rest of your app's specific main process
  83. // code. You can also put them in separate files and require them here.