fgv2.0 平台 electron 打包桌面应用
he wei
2026-03-16 886fa105718a4aca7ffa1e6ed5fe0238e04328c1
main.js
@@ -1,6 +1,10 @@
const { app, BrowserWindow, Menu } = require("electron");
const { app, BrowserWindow, Menu, ipcMain } = require("electron");
const path = require("path");
const { readConfigFile } = require('./getConfig');
const child_process = require("child_process");
// import log from "electron-log";
// 不支持 import
const log = require("electron-log");
// 获取配置文件
const config = readConfigFile();
@@ -32,6 +36,27 @@
  });
}
const appUrl = remoteUrl + 'server/getCookie';
const requestPromise = require("minimal-request-promise");
const checkService = function (cb) {
  requestPromise.get(appUrl).then(
    function (response) {
      log.info(response);
      log.info("Server started!");
      cb();
    },
    function (response) {
      log.warn(response);
      log.info("Waiting for the server start...");
      setTimeout(function () {
        checkService(cb);
      }, 500);
    }
  );
};
// Scheme must be registered before the app is ready
// protocol.registerSchemesAsPrivileged([
//   { scheme: "app", privileges: { secure: true, standard: true, stream: true } },
@@ -47,7 +72,7 @@
//   ? path.join(__dirname, "./loading.html")
//   : `file://${__dirname}/loading.html`;
const loadingURL = `file://${__dirname}/loading.html`;
const showLoading = (cb) => {
const showLoading = (cb, argFn) => {
  loadingWin = new BrowserWindow({
    // show: false,
    frame: false,
@@ -55,15 +80,15 @@
    height: 260,
    resizable: false,
    transparent: true,
    // webPreferences: {
    //   preload: path.join(__dirname, "./preload.js"),
    // },
    webPreferences: {
      preload: path.join(__dirname, "./preload.js"),
    },
  });
  // loadingWin.webContents.openDevTools();
  loadingWin.loadURL(loadingURL);
  loadingWin.setSkipTaskbar(true);
  // loadingWin.show();
  cb(true);
  cb(true, argFn);
  // 启动java程序
  // if (platform === 'win32') {
  //   ipcMain.once('renderer-ready', (event, data) => {
@@ -71,8 +96,53 @@
  //     serverProcess = require('child_process').execFile(dir + '/app_x64.exe');
  //   });
  // }
  requestPromise.get(appUrl).then(
    function () {
      log.info("窗口初始化 服务正常");
      // ipcRenderer.send('java-ready');
      loadingWin.webContents.send("java-ready");
    },
    function () {
      log.info("窗口初始化 接口不通");
      log.info("正在终止java进程,然后重启服务");
      // update(win.webContents, true);
      let stop = child_process.spawn("cmd.exe", ["/c", `${process.cwd()}\\stop.bat`]);
      log.info(`${process.cwd()}\\stop.bat`);
      stop.on("exit", function (code) {
        if (code > 0) {
          log.info('执行stop.bat没有正确exit, 错误码' + code);
          // return false;
        }
        log.info("java进程被终止,准备重启服务");
        child_process.exec(
          `"${process.cwd()}\\fgv2.exe" restart`,
          (err) => {
            if (err) {
              log.info("重启服务出错了stderr: " + JSON.stringify(err));
              // 重新运行setup批处理 尝试重新注册服务
              child_process.spawn("cmd.exe", ["/c", "setup_service.bat"]);
              log.info("服务异常 正在尝试重新注册服务");
            } else {
              log.info("服务重启成功,连接中");
            }
            // 等到服务就绪再重启
            checkService(() => {
              loadingWin.webContents.send("java-ready");
            });
          }
        );
      });
      stop.on('error', (code, data) => {
        log.info('stop.bat 执行 error');
        log.info(code);
        log.info(data);
      });
    }
  );
};
async function createWindow(wait) {
async function createWindow(wait, cb) {
  // Create the browser window.
  win = new BrowserWindow({
    show: !wait,
@@ -84,12 +154,13 @@
      // nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      // contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
      nodeIntegration: false,
      // nodeIntegration: true,
      webSecurity: true,
      allowEval: false,
      allowRunningInsecureContent: false,
      contextIsolation: true,
      enableRemoteModule: false,
      // preload: path.join(__dirname, "preload.js"),
      preload: path.join(__dirname, "preload.js"),
    },
  });
@@ -97,6 +168,8 @@
  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL);
    // DEBUG
    // win.webContents.openDevTools();
    if (!process.env.IS_TEST) win.webContents.openDevTools();
  } else {
    // createProtocol("app");
@@ -106,7 +179,10 @@
    // win.loadURL("http://118.89.139.230:8919/fg/");
    // win.loadURL('http://192.168.10.79:8919/fg/');
    // win.loadURL('http://192.168.10.80:8919/fg/');
    win.loadURL(remoteUrl);
    // 远程文件 不能提前加载
    // win.loadURL(remoteUrl + 'index.html');
    // DEBUG
    // win.webContents.openDevTools();
  }
  if (wait) {
@@ -115,11 +191,21 @@
    //   loadingWin.close();
    //   win.show();
    // }, 6000);
    // win.once("ready-to-show", () => {
    //   loadingWin.hide();
    //   loadingWin.close();
    //   win.show();
    // });
    ipcMain.on("java-ready", () => {
      log.info("java-ready!");
      win.loadURL(remoteUrl + 'index.html');
    win.once("ready-to-show", () => {
      loadingWin.hide();
      loadingWin.close();
      win.show();
    });
    });
  }
}