|
|
@@ -1,8 +1,14 @@
|
|
|
-const { app, BrowserWindow, ipcMain } = require('electron');
|
|
|
+const electron = require('electron');
|
|
|
+const app = electron.app;
|
|
|
+const BrowserWindow = electron.BrowserWindow;
|
|
|
+const ipcMain = electron.ipcMain;
|
|
|
+const globalShortcut = electron.globalShortcut;
|
|
|
+const { autoUpdater } = require('electron-updater');
|
|
|
const path = require('path');
|
|
|
const url = require('url');
|
|
|
-const isLocal = process.argv[process.argv.length - 1] === 'local';
|
|
|
-const extDir = isLocal ? '../build/production/saas' : 'dist';
|
|
|
+const profile = process.argv[process.argv.length - 1];
|
|
|
+const extDir = profile == 'local' ? '../build/production/saas' : 'dist';
|
|
|
+let updateWindow;
|
|
|
let loginWindow;
|
|
|
let mainWindow;
|
|
|
|
|
|
@@ -10,66 +16,135 @@ let mainWindow;
|
|
|
ipcMain.on('session.change', (event, arg) => {
|
|
|
if (arg) {
|
|
|
loginWindow && loginWindow.close();
|
|
|
- createMainWindow(arg);
|
|
|
+ if (!mainWindow) {
|
|
|
+ createMainWindow();
|
|
|
+ }
|
|
|
+ if (!mainWindow.isVisible()) {
|
|
|
+ loadMainContents(arg);
|
|
|
+ }
|
|
|
} else {
|
|
|
- mainWindow && mainWindow.close();
|
|
|
- createLoginWindow();
|
|
|
+ if (mainWindow) {
|
|
|
+ mainWindow.hide();
|
|
|
+ mainWindow.webContents.reload();
|
|
|
+ }
|
|
|
+ !loginWindow && createLoginWindow(true);
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
-function createLoginWindow() {
|
|
|
- loginWindow = new BrowserWindow({ width: 453, height: 513, fullscreenable: false, maximizable: false, resizable: false });
|
|
|
+function sendUpdateMessage(channel, message) {
|
|
|
+ if (updateWindow.webContents.isLoading()) {
|
|
|
+ updateWindow.webContents.on("did-finish-load", function() {
|
|
|
+ updateWindow.webContents.send(channel, message);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ updateWindow.webContents.send(channel, message);
|
|
|
+ }
|
|
|
+}
|
|
|
+autoUpdater.on('error', function (error) {
|
|
|
+ loginWindow.show();
|
|
|
+ // 预加载主页
|
|
|
+ createMainWindow(false);
|
|
|
+ updateWindow && updateWindow.close();
|
|
|
+});
|
|
|
+autoUpdater.on('update-available', function (info) {
|
|
|
+ updateWindow && updateWindow.show();
|
|
|
+ sendUpdateMessage('message', info);
|
|
|
+});
|
|
|
+autoUpdater.on('update-not-available', function (info) {
|
|
|
+ loginWindow.show();
|
|
|
+ // 预加载主页
|
|
|
+ createMainWindow(false);
|
|
|
+ updateWindow && updateWindow.close();
|
|
|
+});
|
|
|
+// 下载进度
|
|
|
+autoUpdater.on('download-progress', function (progressObj) {
|
|
|
+ sendUpdateMessage('downloadProgress', progressObj);
|
|
|
+});
|
|
|
+autoUpdater.on('update-downloaded', function () {
|
|
|
+ // 下载完自动安装
|
|
|
+ autoUpdater.quitAndInstall();
|
|
|
+});
|
|
|
+// 自动更新窗口
|
|
|
+function createUpdateWindow() {
|
|
|
+ updateWindow = new BrowserWindow({ width: 453, height: 538, frame: false, show: false });
|
|
|
+ updateWindow.loadURL(url.format({
|
|
|
+ pathname: path.join(__dirname, 'update.html'),
|
|
|
+ protocol: 'file:',
|
|
|
+ slashes: true
|
|
|
+ }));
|
|
|
+ updateWindow.on('closed', function () {
|
|
|
+ updateWindow = null;
|
|
|
+ });
|
|
|
+ autoUpdater.checkForUpdates();
|
|
|
+ // updateWindow.webContents.on("did-finish-load", function() {
|
|
|
+ // autoUpdater.checkForUpdates();
|
|
|
+ // });
|
|
|
+}
|
|
|
+// 登录窗口
|
|
|
+function createLoginWindow(show) {
|
|
|
+ loginWindow = new BrowserWindow({ width: 453, height: 538, show: !!show, title: '账户登录 - U企云服', fullscreenable: false, maximizable: false, resizable: false });
|
|
|
loginWindow.loadURL(url.format({
|
|
|
pathname: path.join(__dirname, 'login.html'),
|
|
|
protocol: 'file:',
|
|
|
slashes: true
|
|
|
}));
|
|
|
|
|
|
- if (isLocal) {
|
|
|
- loginWindow.webContents.openDevTools();
|
|
|
- }
|
|
|
-
|
|
|
loginWindow.on('closed', function () {
|
|
|
loginWindow = null;
|
|
|
+ if (mainWindow && !mainWindow.isVisible()) {
|
|
|
+ mainWindow.close();
|
|
|
+ }
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
-function createMainWindow (session) {
|
|
|
- mainWindow = new BrowserWindow({ width: 1280, height: 720, show: false });
|
|
|
- mainWindow.once('ready-to-show', function(){
|
|
|
- mainWindow.maximize();
|
|
|
- mainWindow.show();
|
|
|
- });
|
|
|
- mainWindow.loadURL(url.format({
|
|
|
- pathname: path.join(__dirname, extDir + '/index.html'),
|
|
|
- protocol: 'file:',
|
|
|
- slashes: true
|
|
|
- }));
|
|
|
-
|
|
|
- if (isLocal) {
|
|
|
- mainWindow.webContents.openDevTools();
|
|
|
+// 主窗口
|
|
|
+function createMainWindow (show) {
|
|
|
+ const electronScreen = electron.screen;
|
|
|
+ const size = electronScreen.getPrimaryDisplay().workAreaSize;
|
|
|
+ mainWindow = new BrowserWindow({ width: size.width, height: size.height, show: !!show });
|
|
|
+ if (profile == 'dev') {
|
|
|
+ mainWindow.loadURL('http://127.0.0.1:1841/');
|
|
|
+ } else {
|
|
|
+ mainWindow.loadURL(url.format({
|
|
|
+ pathname: path.join(__dirname, extDir + '/index.html'),
|
|
|
+ protocol: 'file:',
|
|
|
+ slashes: true
|
|
|
+ }));
|
|
|
}
|
|
|
-
|
|
|
- mainWindow.webContents.on("did-finish-load", function() {
|
|
|
- mainWindow.webContents.executeJavaScript('\
|
|
|
- localStorage.setItem("app-state-session", decodeURIComponent("' + encodeURIComponent(session) + '"))');
|
|
|
- });
|
|
|
+
|
|
|
mainWindow.on('closed', function () {
|
|
|
mainWindow = null;
|
|
|
});
|
|
|
}
|
|
|
+function loadMainContents(session) {
|
|
|
+ mainWindow.webContents.send("session", session);
|
|
|
+ mainWindow.maximize();
|
|
|
+ mainWindow.show();
|
|
|
+}
|
|
|
|
|
|
-app.on('ready', createLoginWindow);
|
|
|
+function onReady() {
|
|
|
+ globalShortcut.register('ctrl+shift+d', function() {
|
|
|
+ const win = BrowserWindow.getFocusedWindow();
|
|
|
+ win && win.webContents.openDevTools();
|
|
|
+ });
|
|
|
+ createUpdateWindow();
|
|
|
+ // 预加载登录页
|
|
|
+ createLoginWindow(false);
|
|
|
+}
|
|
|
+
|
|
|
+app.on('ready', onReady);
|
|
|
+
|
|
|
+app.on('will-quit', function() {
|
|
|
+ globalShortcut.unregisterAll();
|
|
|
+});
|
|
|
|
|
|
// Quit when all windows are closed, except for Mac users
|
|
|
app.on('window-all-closed', function () {
|
|
|
if (process.platform !== 'darwin') {
|
|
|
- app.quit()
|
|
|
+ app.quit();
|
|
|
}
|
|
|
});
|
|
|
|
|
|
app.on('activate', function () {
|
|
|
if (mainWindow === null) {
|
|
|
- createMainWindow();
|
|
|
+ onReady();
|
|
|
}
|
|
|
});
|