| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 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 profile = process.argv[process.argv.length - 1];
- const extDir = profile == 'local' ? '../build/production/saas' : 'dist';
- let updateWindow;
- let loginWindow;
- let mainWindow;
- // 监听session变化
- ipcMain.on('session.change', (event, arg) => {
- if (arg) {
- loginWindow && loginWindow.close();
- if (!mainWindow) {
- createMainWindow();
- }
- if (!mainWindow.isVisible()) {
- loadMainContents(arg);
- }
- } else {
- if (mainWindow) {
- mainWindow.hide();
- mainWindow.webContents.reload();
- }
- !loginWindow && createLoginWindow(true);
- }
- });
- 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
- }));
- loginWindow.on('closed', function () {
- loginWindow = null;
- if (mainWindow && !mainWindow.isVisible()) {
- mainWindow.close();
- }
- });
- }
- // 主窗口
- 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.on('closed', function () {
- mainWindow = null;
- });
- }
- function loadMainContents(session) {
- mainWindow.webContents.send("session", session);
- mainWindow.maximize();
- mainWindow.show();
- }
- 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.on('activate', function () {
- if (mainWindow === null) {
- onReady();
- }
- });
|