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.

119 lines
3.5 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
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, globalShortcut} = 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. // mainWindow.loadURL('http://localhost:8888/')
  25. // Open the DevTools.
  26. // mainWindow.webContents.openDevTools()
  27. mainWindow.on('close', e => {
  28. const choice = dialog.showMessageBoxSync(mainWindow, {
  29. type: 'info',
  30. buttons: ['取消', '确认'],
  31. title: '提示',
  32. message: '确定要退出系统吗?',
  33. defaultId: 0, // 默认选中“取消”按钮
  34. cancelId: 1, // 按下 Esc 键时的默认行为是“取消”
  35. })
  36. const leave = choice === 0;
  37. if (!leave) {
  38. clearCache(mainWindow)
  39. } else {
  40. // 点击取消按钮
  41. e.preventDefault();
  42. }
  43. })
  44. mainWindow.on('closed', () => {
  45. app.quit() // 关闭应用程序
  46. })
  47. // 注册快捷键
  48. // mainWindow.on('focus', () => {
  49. // globalShortcut.register('CommandOrControl+Alt+F', function () {
  50. // mainWindow && mainWindow.webContents.toggleDevTools()
  51. // })
  52. // })
  53. // mainWindow.on('blur', () => {
  54. // globalShortcut.unregisterAll() // 注销键盘事件
  55. // })
  56. }
  57. /**
  58.  * @description 清除缓存
  59.  */
  60. function clearCache(mainWindow) {
  61. // 在窗口即将关闭时清除 localStorage 和 sessionStorage
  62. mainWindow.webContents.executeJavaScript('localStorage.removeItem("Admin-Token");')
  63. mainWindow.webContents.executeJavaScript('sessionStorage.clear();')
  64. }
  65. // This method will be called when Electron has finished
  66. // initialization and is ready to create browser windows.
  67. // Some APIs can only be used after this event occurs.
  68. app.whenReady().then(() => {
  69. createWindow()
  70. app.on('activate', function () {
  71. // On macOS it's common to re-create a window in the app when the
  72. // dock icon is clicked and there are no other windows open.
  73. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  74. })
  75. })
  76. // Quit when all windows are closed, except on macOS. There, it's common
  77. // for applications and their menu bar to stay active until the user quits
  78. // explicitly with Cmd + Q.
  79. app.on('window-all-closed', function () {
  80. if (process.platform !== 'darwin') app.quit()
  81. })
  82. app.on('before-quit', () => {
  83. // 获取端口
  84. let port = 30523;
  85. // 执行杀死进程的操作
  86. try {
  87. const command = `netstat -ano | findstr :${port}`;
  88. const output = execSync(command).toString();
  89. const lines = output.trim().split('\n');
  90. for (const line of lines) {
  91. const pidMatch = line.match(/LISTENING\s+(\d+)/);
  92. if (pidMatch) {
  93. const pid = pidMatch[1];
  94. execSync(`taskkill /PID ${pid} -t -f`);
  95. break;
  96. }
  97. }
  98. } catch (error) {
  99. console.error('无法杀死进程', error);
  100. }
  101. });
  102. // In this file you can include the rest of your app's specific main process
  103. // code. You can also put them in separate files and require them here.