From 78205628b348720e2ce5e0bb48529292852d4589 Mon Sep 17 00:00:00 2001
From: he wei <858544502@qq.com>
Date: 星期二, 29 三月 2022 13:59:10 +0800
Subject: [PATCH] UA 添加加载等待动画 打包配置修改

---
 src/main/demo/src/background.js |  150 +++++++++++++++++++++++++++++++------------------
 1 files changed, 94 insertions(+), 56 deletions(-)

diff --git a/src/main/demo/src/background.js b/src/main/demo/src/background.js
index 21c468e..e13fa68 100644
--- a/src/main/demo/src/background.js
+++ b/src/main/demo/src/background.js
@@ -1,9 +1,12 @@
 'use strict'
 
-import { app, protocol, BrowserWindow, Menu } from 'electron'
-import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
+const { app, protocol, BrowserWindow, Menu, ipcMain, ipcRenderer } = require('electron');
+const { createProtocol } = require('vue-cli-plugin-electron-builder/lib');
+const path = require('path');
+
 const isDevelopment = process.env.NODE_ENV !== 'production'
 let win = null;
+let loadingWin = null;
 
 // 鍗曚緥閿�
 const gotTheLock = app.requestSingleInstanceLock()
@@ -26,59 +29,74 @@
   { scheme: 'app', privileges: { secure: true, standard: true } }
 ])
 
+// loading 绐楀彛
+const loadingURL = isDevelopment ? path.join(__dirname, './loading.html') : `file://${__dirname}/loading.html`;
+const showLoading = (cb) => {
+  loadingWin = new BrowserWindow({
+    // show: false,
+    frame: false,
+    width: 260,
+    height: 260,
+    resizable: false,
+    transparent: true,
+    webPreferences: {
+      preload: path.join(__dirname, './preload.js')
+    }
+  });
+  // loadingWin.once('show', () => {
+  //   cb(true);
+  // });
+  // loadingWin.webContents.openDevTools();
+  loadingWin.loadURL(loadingURL);
+  loadingWin.setSkipTaskbar(true);
+  // loadingWin.show();
+  cb(true);
+  // 鍚姩java绋嬪簭
+  if (isDevelopment) {
+    serverProcess = true;
+  } else {
+    if (platform === 'win32') {
+      ipcMain.once('renderer-ready', (event, data) => {
+        let dir = path.resolve(__dirname, '..');
+        serverProcess = require('child_process').execFile(dir + '/app_x64.exe');
+      });
+    }
+  }
+}
 
-let appUrl = 'http://localhost:8099/test/hello';
+// let appUrl = 'http://localhost:8099/test/hello';
 const platform = process.platform
 let appStarted = false
 let serverProcess
-if (isDevelopment) {
-  serverProcess = true
-} else {
-  if (platform === 'win32') {
-    // let pathArr = app.getPath('exe').split('\\');
-    // pathArr.length = pathArr.length - 1;
-    // let path = pathArr.join('\\');
-    // require('child_process').spawn('cmd.exe', ['/c', 'echo '+app.getAppPath()+ '===========' + __dirname +'===========' +   __filename +'===========' +  __static + ' >> 123.txt']);
 
-    // serverProcess = require('child_process').spawn('cmd.exe', ['/c', 'testElectronJ.bat'], {
-    //   cwd: path + '/bundled/bin'
-    // })
-    serverProcess = require('child_process').execFile(__dirname + '/app_x64.exe');
-    // serverProcess = require('child_process').spawn('cmd.exe', ['/c', 'testElectronJ.bat'], {
-    //   cwd: __dirname + '/bin'
-    //   // cwd: app.getAppPath() + '/bin'
-    // });
-  }
-}
 if (!isDevelopment) {
   Menu.setApplicationMenu(null);
 }
-async function createWindow() {
+async function createWindow(wait) {
   // Create the browser window.
   win = new BrowserWindow({
+    show: !wait,
     width: 1200,
     height: 800,
     icon: `${__static}/icon/icon.png`,
     webPreferences: {
-      webSecurity: false,
+      // webSecurity: false,
       // Use pluginOptions.nodeIntegration, leave this alone
       // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
-      nodeIntegration: true,
+      // nodeIntegration: true,
+      // contextIsolation: false
       // nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
-      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
+      // contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
+      nodeIntegration: false,
+      webSecurity: true,
+      allowEval: false,
+      allowRunningInsecureContent: false,
+      contextIsolation: true,
+      enableRemoteModule: false,
+      preload: path.join(__dirname, './preload.js')
+      // preload: `${__dirname}/preload.js`
     }
-  })
-
-  win.webContents.on('new-window', (event, url, frameName, disposition, options, additionalFeatures) => {
-    // open window as modal
-    event.preventDefault()
-    Object.assign(options, {
-      parent: win,
-      center: true
-    })
-    event.newGuest = new BrowserWindow(options)
-    event.newGuest.loadURL(url)
-  })
+  });
 
   if (process.env.WEBPACK_DEV_SERVER_URL) {
     // Load the url of the dev server if in development mode
@@ -89,6 +107,14 @@
     // Load the index.html when not in development
     // win.webContents.openDevTools();
     win.loadURL('app://./index.html')
+  }
+
+  if (wait) {
+    ipcMain.once("main-window-ready", () => {
+      loadingWin.hide();
+      loadingWin.close();
+      win.show();
+    });
   }
 }
 
@@ -121,30 +147,42 @@
 // initialization and is ready to create browser windows.
 // Some APIs can only be used after this event occurs.
 
-const startUp = function () {
-  const requestPromise = require('minimal-request-promise')
-  requestPromise.get(appUrl).then(function (response) {
-    // require('child_process').spawn('cmd.exe', ['/c', 'echo '+JSON.stringify(response)+' success >> 123.txt'])
-    console.log(response);
-    console.log('Server started!');
-    createWindow();
-    appStarted = true
-  }, function (response) {
-    console.log(response)
-    console.log('Waiting for the server start...');
-    // require('child_process').spawn('cmd.exe', ['/c', 'echo '+JSON.stringify(response)+' error >> 123.txt'])
-    setTimeout(function () {
-      startUp()
-    }, 500)
-  })
-}
-
+// const startUp = function () {
+//   const requestPromise = require('minimal-request-promise')
+//   requestPromise.get(appUrl).then(function (response) {
+//     // require('child_process').spawn('cmd.exe', ['/c', 'echo '+JSON.stringify(response)+' success >> 123.txt'])
+//     console.log(response);
+//     console.log('Server started!');
+//     ipcRenderer.sendSync('java-ready');
+//     // createWindow();
+//     appStarted = true
+//   }, function (response) {
+//     console.log(response)
+//     // console.log('Waiting for the server start...');
+//     // require('child_process').spawn('cmd.exe', ['/c', 'echo '+JSON.stringify(response)+' error >> 123.txt'])
+//     setTimeout(function () {
+//       startUp()
+//     }, 500)
+//   })
+// }
+// // 鍚姩java绋嬪簭
+// if (isDevelopment) {
+//   serverProcess = true;
+// } else {
+//   if (platform === 'win32') {
+//     ipcMain.once('renderer-ready', (event, data) => {
+//       serverProcess = require('child_process').execFile(__dirname + '/app_x64.exe');
+//       startUp();
+//     });
+//   }
+// }
 // 绂佺敤鏈湴缂撳瓨
 // app.commandLine.appendSwitch("--disable-http-cache");
 
 app.on('ready', async () => {
   // require('child_process').spawn('cmd.exe', ['/c', 'echo ready >> 123.txt'])
-  startUp()
+  // startUp();
+  showLoading(createWindow);
 })
 // Exit cleanly on request from parent process in development mode.
 if (isDevelopment) {

--
Gitblit v1.9.1