index.ts 3.8 KB

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