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();
|
// console.log(config);
|
const {
|
protocol,
|
serviceIp,
|
port
|
} = config;
|
const remoteUrl = `${protocol}://${serviceIp}:${port}/fg/`;
|
|
// console.log(remoteUrl);
|
|
let win = null;
|
let loadingWin = null;
|
|
// 单例锁
|
const gotTheLock = app.requestSingleInstanceLock();
|
|
if (!gotTheLock) {
|
app.quit();
|
} else {
|
app.on("second-instance", (event, commandLine, workingDirectory) => {
|
// 当运行第二个实例时,将会聚焦到myWindow这个窗口
|
if (win) {
|
if (win.isMinimized()) win.restore();
|
win.focus();
|
}
|
});
|
}
|
|
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 } },
|
// ]);
|
|
// TODO debug;
|
// win.webContents.openDevTools()
|
|
Menu.setApplicationMenu(null);
|
|
// loading 窗口
|
// const loadingURL = isDevelopment
|
// ? path.join(__dirname, "./loading.html")
|
// : `file://${__dirname}/loading.html`;
|
const loadingURL = `file://${__dirname}/loading.html`;
|
const showLoading = (cb, argFn) => {
|
loadingWin = new BrowserWindow({
|
// show: false,
|
frame: false,
|
width: 260,
|
height: 260,
|
resizable: false,
|
transparent: true,
|
webPreferences: {
|
preload: path.join(__dirname, "./preload.js"),
|
},
|
});
|
// loadingWin.webContents.openDevTools();
|
loadingWin.loadURL(loadingURL);
|
loadingWin.setSkipTaskbar(true);
|
// loadingWin.show();
|
cb(true, argFn);
|
// 启动java程序
|
// if (platform === 'win32') {
|
// ipcMain.once('renderer-ready', (event, data) => {
|
// let dir = path.resolve(__dirname, '..');
|
// 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);
|
// 如果是win平台
|
if (process.platform == 'darwin' || process.platform == 'win32') {
|
stopForWin();
|
} else if (process.platform == 'linux') {
|
stopForLinux();
|
}
|
}
|
);
|
|
};
|
|
// 终止java进程 并重启服务 win平台
|
function stopForWin() {
|
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);
|
});
|
}
|
|
// 终止java进程 并重启服务 linux平台
|
function stopForLinux() {
|
let dir = path.join(__dirname, './stop.sh');
|
let stop = child_process.spawn('bash', [dir]);
|
|
// 监听脚本执行
|
stop.stdout.on('data', (data) => {
|
log.info(`stdout: ${data}`);
|
});
|
|
stop.stderr.on('data', (data) => {
|
log.info(`stderr: ${data}`);
|
});
|
|
stop.on('close', (code) => {
|
log.info(`子进程退出, 退出码:${code}`);
|
|
log.info("java进程被终止,准备重启服务");
|
child_process.exec(`systemctl --user restart fgv2.service`, (err, out, stde) => {
|
log.info('==========');
|
log.info(err);
|
log.info(out);
|
log.info(stde);
|
if (err) {
|
log.error('重启服务失败:' + err);
|
let dir = path.join(__dirname, './setup_service.sh');
|
child_process.exec(`bash "${dir}"`, (error, stdout, stderr) => {
|
if (error) {
|
log.error('执行失败:' + error);
|
}
|
log.info(`脚本输出: ${stdout}`);
|
log.error(`脚本错误: ${stderr}`);
|
});
|
}
|
if (out) {
|
log.info(`重启服务输出: ${out}`);
|
}
|
if (stde) {
|
log.error(`重启服务错误: ${stde}`);
|
let dir = path.join(__dirname, './setup_service.sh');
|
child_process.exec(`bash "${dir}"`, (error, stdout, stderr) => {
|
log.info('0000000');
|
if (error) {
|
log.error('执行失败:' + error);
|
}
|
log.info(`脚本输出: ${stdout}`);
|
log.error(`脚本错误: ${stderr}`);
|
});
|
}
|
// 等到服务就绪再重启
|
checkService(() => {
|
loadingWin.webContents.send("java-ready");
|
});
|
});
|
});
|
|
}
|
|
async function createWindow(wait, cb) {
|
// Create the browser window.
|
win = new BrowserWindow({
|
show: !wait,
|
width: 1000,
|
height: 600,
|
webPreferences: {
|
// Use pluginOptions.nodeIntegration, leave this alone
|
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
|
// 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"),
|
},
|
});
|
|
// remote.enable(win.webContents);
|
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");
|
// Load the index.html when not in development
|
// win.loadURL("app://./index.html");
|
// 加载远程地址
|
// 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 + 'index.html');
|
// DEBUG
|
// win.webContents.openDevTools();
|
}
|
|
if (wait) {
|
// setTimeout(() => {
|
// loadingWin.hide();
|
// 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();
|
});
|
});
|
}
|
}
|
|
// Quit when all windows are closed.
|
app.on("window-all-closed", () => {
|
// On macOS it is common for applications and their menu bar
|
// to stay active until the user quits explicitly with Cmd + Q
|
if (process.platform !== "darwin") {
|
app.quit();
|
}
|
});
|
|
app.on("activate", () => {
|
log.info("app activate event");
|
// On macOS it's common to re-create a window in the app when the
|
// dock icon is clicked and there are no other windows open.
|
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
});
|
|
// This method will be called when Electron has finished
|
// initialization and is ready to create browser windows.
|
// Some APIs can only be used after this event occurs.
|
app.on("ready", async () => {
|
showLoading(createWindow);
|
});
|