main.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const electron = require('electron');
  2. const app = electron.app;
  3. const BrowserWindow = electron.BrowserWindow;
  4. const ipcMain = electron.ipcMain;
  5. const globalShortcut = electron.globalShortcut;
  6. const { autoUpdater } = require('electron-updater');
  7. const path = require('path');
  8. const url = require('url');
  9. const profile = process.argv[process.argv.length - 1];
  10. const extDir = profile == 'local' ? '../build/production/saas' : 'dist';
  11. let updateWindow;
  12. let loginWindow;
  13. let mainWindow;
  14. // 监听session变化
  15. ipcMain.on('session.change', (event, arg) => {
  16. if (arg) {
  17. loginWindow && loginWindow.close();
  18. if (!mainWindow) {
  19. createMainWindow();
  20. }
  21. if (!mainWindow.isVisible()) {
  22. loadMainContents(arg);
  23. }
  24. } else {
  25. if (mainWindow) {
  26. mainWindow.hide();
  27. mainWindow.webContents.reload();
  28. }
  29. !loginWindow && createLoginWindow(true);
  30. }
  31. });
  32. function sendUpdateMessage(channel, message) {
  33. if (updateWindow.webContents.isLoading()) {
  34. updateWindow.webContents.on("did-finish-load", function() {
  35. updateWindow.webContents.send(channel, message);
  36. });
  37. } else {
  38. updateWindow.webContents.send(channel, message);
  39. }
  40. }
  41. autoUpdater.on('error', function (error) {
  42. loginWindow.show();
  43. // 预加载主页
  44. createMainWindow(false);
  45. updateWindow && updateWindow.close();
  46. });
  47. autoUpdater.on('update-available', function (info) {
  48. updateWindow && updateWindow.show();
  49. sendUpdateMessage('message', info);
  50. });
  51. autoUpdater.on('update-not-available', function (info) {
  52. loginWindow.show();
  53. // 预加载主页
  54. createMainWindow(false);
  55. updateWindow && updateWindow.close();
  56. });
  57. // 下载进度
  58. autoUpdater.on('download-progress', function (progressObj) {
  59. sendUpdateMessage('downloadProgress', progressObj);
  60. });
  61. autoUpdater.on('update-downloaded', function () {
  62. // 下载完自动安装
  63. autoUpdater.quitAndInstall();
  64. });
  65. // 自动更新窗口
  66. function createUpdateWindow() {
  67. updateWindow = new BrowserWindow({ width: 453, height: 538, frame: false, show: false });
  68. updateWindow.loadURL(url.format({
  69. pathname: path.join(__dirname, 'update.html'),
  70. protocol: 'file:',
  71. slashes: true
  72. }));
  73. updateWindow.on('closed', function () {
  74. updateWindow = null;
  75. });
  76. autoUpdater.checkForUpdates();
  77. // updateWindow.webContents.on("did-finish-load", function() {
  78. // autoUpdater.checkForUpdates();
  79. // });
  80. }
  81. // 登录窗口
  82. function createLoginWindow(show) {
  83. loginWindow = new BrowserWindow({ width: 453, height: 538, show: !!show, title: '账户登录 - U企云服', fullscreenable: false, maximizable: false, resizable: false });
  84. loginWindow.loadURL(url.format({
  85. pathname: path.join(__dirname, 'login.html'),
  86. protocol: 'file:',
  87. slashes: true
  88. }));
  89. loginWindow.on('closed', function () {
  90. loginWindow = null;
  91. if (mainWindow && !mainWindow.isVisible()) {
  92. mainWindow.close();
  93. }
  94. });
  95. }
  96. // 主窗口
  97. function createMainWindow (show) {
  98. const electronScreen = electron.screen;
  99. const size = electronScreen.getPrimaryDisplay().workAreaSize;
  100. mainWindow = new BrowserWindow({ width: size.width, height: size.height, show: !!show });
  101. if (profile == 'dev') {
  102. mainWindow.loadURL('http://127.0.0.1:1841/');
  103. } else {
  104. mainWindow.loadURL(url.format({
  105. pathname: path.join(__dirname, extDir + '/index.html'),
  106. protocol: 'file:',
  107. slashes: true
  108. }));
  109. }
  110. mainWindow.on('closed', function () {
  111. mainWindow = null;
  112. });
  113. }
  114. function loadMainContents(session) {
  115. mainWindow.webContents.send("session", session);
  116. mainWindow.maximize();
  117. mainWindow.show();
  118. }
  119. function onReady() {
  120. globalShortcut.register('ctrl+shift+d', function() {
  121. const win = BrowserWindow.getFocusedWindow();
  122. win && win.webContents.openDevTools();
  123. });
  124. createUpdateWindow();
  125. // 预加载登录页
  126. createLoginWindow(false);
  127. }
  128. app.on('ready', onReady);
  129. app.on('will-quit', function() {
  130. globalShortcut.unregisterAll();
  131. });
  132. // Quit when all windows are closed, except for Mac users
  133. app.on('window-all-closed', function () {
  134. if (process.platform !== 'darwin') {
  135. app.quit();
  136. }
  137. });
  138. app.on('activate', function () {
  139. if (mainWindow === null) {
  140. onReady();
  141. }
  142. });