index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { app, shell, BrowserWindow, protocol, net } from 'electron';
  2. import { join, resolve } from 'path';
  3. import url from 'url';
  4. import { electronApp, optimizer, is } from '@electron-toolkit/utils';
  5. import icon from '../../resources/icon.png?asset';
  6. import useElectron from './useElectron';
  7. import useWinProcess from './useWinProcess';
  8. import log from './logger';
  9. // 免登录流程
  10. function silenceAuthorityHandle(mainWindow) {
  11. const arg = process.argv;
  12. log.info(`process.argv:${JSON.stringify(arg)}`);
  13. mainWindow.webContents.send('silence-authority', arg[1]);
  14. // mainWindow.webContents.send(
  15. // 'silence-authority',
  16. // 'zgiOtrHS2nq9q3SOr0iOYAW1JJsG2w1lrqdv0PB2IrT9ijPBsKxT7I7u2077RaKAPbwfEj+9f/zr+RHzOZFsaYZo5GuoJLMdJPKvvOnpG/3jk6GsdPFlMooCjs5LzJKutFiFOj/xsXoOGdxqIYXWWH6sROZQAKhvBIu2Mfem6+fZhy66baK/s0ihec59gOTQ8lsjyJzf8To2K0EyapRWGVpFwljAmaQ1HfhfUUoRIYTIHW89wqRU3KYjB5oJQRQnVYLA7v0H9kK0smmX3N3Kbc0V9LtHUIiU/teGvWaa6ty43SAJqci9IRZEh35xzM9IjtzvR/9LHQWZqTLuVSZ+JmCwfKKGFV39ewYjrjmFk34tIW5yvmQ9oUugpi2T3xhdMA7aeNkkoK0tgm3hwqri3KdziGcqdEdkqhfW2vapwPDy4m5g/JH7ulNLI0nfDmPpTNsWgWXjUnmAQNWAp4In1oH8TMRNHImMbsxz4RhcBz/z/YCWl89zk/vA4TOtQDV99s66+8kVhTQEjDXffz6z2T5hZTGaPMv/lSCo1Fo01VM='
  17. // );
  18. }
  19. function createWindow(): void {
  20. // Create the browser window.
  21. const mainWindow = new BrowserWindow({
  22. width: is.dev ? 1428 : 1024,
  23. height: 700,
  24. minWidth: 1024,
  25. minHeight: 600,
  26. show: false,
  27. ...(process.platform === 'linux' ? { icon } : {}),
  28. webPreferences: {
  29. preload: join(__dirname, '../preload/index.js'),
  30. sandbox: false,
  31. webSecurity: false,
  32. },
  33. });
  34. mainWindow.on('ready-to-show', () => {
  35. mainWindow.show();
  36. silenceAuthorityHandle(mainWindow);
  37. });
  38. mainWindow.webContents.setWindowOpenHandler((details) => {
  39. shell.openExternal(details.url);
  40. return { action: 'deny' };
  41. });
  42. // HMR for renderer base on electron-vite cli.
  43. // Load the remote URL for development or the local html file for production.
  44. if (is.dev && process.env.ELECTRON_RENDERER_URL) {
  45. mainWindow.loadURL(process.env.ELECTRON_RENDERER_URL);
  46. mainWindow.webContents.openDevTools();
  47. } else {
  48. mainWindow.loadFile(join(__dirname, '../renderer/index.html'));
  49. }
  50. }
  51. // This method will be called when Electron has finished
  52. // initialization and is ready to create browser windows.
  53. // Some APIs can only be used after this event occurs.
  54. app.whenReady().then(() => {
  55. // Set app user model id for windows
  56. electronApp.setAppUserModelId('com.electron');
  57. // Default open or close DevTools by F12 in development
  58. // and ignore CommandOrControl + R in production.
  59. // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
  60. app.on('browser-window-created', (_, window) => {
  61. optimizer.watchWindowShortcuts(window);
  62. });
  63. // use electron
  64. useElectron();
  65. // use multiple window porcess
  66. useWinProcess();
  67. createWindow();
  68. // protocol
  69. protocol.handle('local', (request) => {
  70. const filePath = request.url.slice('local://'.length);
  71. return net.fetch(
  72. url.pathToFileURL(resolve(__dirname, filePath)).toString()
  73. );
  74. });
  75. app.on('activate', () => {
  76. // On macOS it's common to re-create a window in the app when the
  77. // dock icon is clicked and there are no other windows open.
  78. if (BrowserWindow.getAllWindows().length === 0) createWindow();
  79. });
  80. });
  81. // Quit when all windows are closed, except on macOS. There, it's common
  82. // for applications and their menu bar to stay active until the user quits
  83. // explicitly with Cmd + Q.
  84. app.on('window-all-closed', () => {
  85. if (process.platform !== 'darwin') {
  86. app.quit();
  87. }
  88. });
  89. // In this file you can include the rest of your app"s specific main process
  90. // code. You can also put them in separate files and require them here.