"use strict"; const { app, protocol, BrowserWindow, Menu, ipcMain, dialog, shell, } = require("electron"); const { createProtocol } = require("vue-cli-plugin-electron-builder/lib"); const path = require("path"); const child_process = require("child_process"); const log = require("electron-log"); // const appUrl = "http://localhost:8099/fbx/test/hello"; const appUrl = "http://localhost:8099/fbx/testParam/callService?seconds=3"; const requestPromise = require("minimal-request-promise"); const remote = require("@electron/remote/main"); remote.initialize(); const isDevelopment = process.env.NODE_ENV !== "production"; 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(); // TODO 双击文件 打开程序时 发送的事件 if (commandLine[2]) { win.webContents.send( "selected-file", { filePaths: [commandLine[2]] }, "MenuList" ); } } }); } // Scheme must be registered before the app is ready protocol.registerSchemesAsPrivileged([ { scheme: "app", privileges: { secure: true, standard: true } }, ]); // loading 窗口 const loadingURL = isDevelopment ? path.join(__dirname, "./loading.html") : `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.once('show', () => { // cb(true); // }); // 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进程,然后重启服务"); // let stop = child_process.spawn("cmd.exe", ["/c", "stop.bat"]); // let stop; // try { // stop = child_process.spawn("cmd.exe", ["/c", "stop.bat"]); // stop.on("close", function (code) { // log.info("子进程关闭,代码: " + code); // }); // stop.on("disconnect", function () { // log.info("子进程断开连接"); // }); // stop.on("error", function (err) { // log.info("子进程发生错误: " + err); // }); // stop.on("exit", function (code) { // log.info("子进程退出,代码: " + code); // }); // stop.on("message", function (message) { // log.info("子进程发送消息: " + message); // }); // stop.on("spawn", function () { // log.info("子进程被创建"); // }); // stop.on("stdio", function (stdio) { // log.info("子进程的标准输入、输出或错误流发生变化: " + stdio); // }); // stop.stdout.on("data", function (data) { // log.info("子进程的标准输出: " + data.toString()); // }); // stop.stderr.on("data", function (data) { // log.info("子进程的标准错误输出: " + data.toString()); // }); // // ... // } catch (err) { // log.info("catch stop.bat 执行失败, " + err); // } // stop.on("error", function (err) { // log.info("error: ===="); // log.info("error: " + err.toString()); // log.info("on error stop.bat 执行失败, " + err); // }); // stop.on("exit", function (code) { // if (code > 0) { // log.info("stop.bat 执行失败, " + stop.stderr); // log.info("stop.bat 失败日志:" + stop.stderr.toString()); // return false; // } // log.info("java进程被终止,准备重启服务"); // child_process.exec( // `"${process.cwd()}\\FbxAnalysis.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"); // }); // } // ); // }); // TODO // stop.stderr.on("data", (data) => { // log.info("stop.bat 执行失败: " + data.toString()); // child_process.exec( // `"${process.cwd()}\\FbxAnalysis.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"); // }); // } // ); // }); // TODO child_process.exec("cmd.exe /c stop.bat", (err, stdout, stderr) => { if (err) { log.info("stop.bat 执行失败, " + err); log.info("stop.bat 失败日志:" + err.toString()); return false; } else { log.info("stdout: " + stdout.toString()); log.info("java进程被终止,准备重启服务"); child_process.exec( `"${process.cwd()}\\FbxAnalysis.exe" restart`, (err) => { if (err) { log.info("重启服务出错了stderr: " + JSON.stringify(err)); // 重新运行setup批处理 尝试重新注册服务 // child_process.spawn("cmd.exe", ["/c", "setup_service.bat"]); // try { // child_process.exec( // `${process.cwd()}\\setup_service.bat`, // (err2, stdout2, stderr) => { // log.info("服务异常 正在尝试重新注册服务"); // if (err2) { // log.info("setup_service.bat 执行失败, " + err2); // log.info( // "setup_service.bat 失败日志:" + err2.toString() // ); // } else { // log.info("setup stdout: " + stdout2.toString()); // } // } // ); // } catch (error) { // log.info("setup_service.bat 执行失败, catch " + error); // } child_process.exec("cmd.exe /c setup_service.bat", (err2, stdout2, stderr) => { log.info("服务异常 正在尝试重新注册服务"); if (err2) { log.info("setup_service.bat 执行失败, " + err2); log.info("setup_service.bat 失败日志:" + err2.toString()); } else { log.info("setup stdout: " + stdout2.toString()); } }); } else { log.info("服务重启成功,连接中"); } // 等到服务就绪再重启 checkService(() => { loadingWin.webContents.send("java-ready"); }); } ); } }); // log.info('窗口初始化 接口不通 重启服务'); // child_process.exec(`"${process.cwd()}\\FbxAnalysis.exe" restart`, (err) => { // if (err) { // log.info('重启服务出错了stderr: ' + JSON.stringify(err)); // return // } // log.info('服务重启成功,连接中'); // // 等到服务就绪再重启 // checkService(() => { // loadingWin.webContents.send('java-ready'); // }); // }); } ); }; 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); } ); }; // const platform = process.platform let serverProcess; if (!isDevelopment) { Menu.setApplicationMenu(null); } 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, 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); if (!process.env.IS_TEST) win.webContents.openDevTools(); } else { createProtocol("app"); // Load the index.html when not in development win.loadURL("app://./index.html"); } if (wait) { ipcMain.on("java-ready", () => { log.info("java-ready!"); loadingWin.hide(); loadingWin.close(); win.show(); }); } if (cb && "function" == typeof cb) { win.once("ready-to-show", () => { cb(win.webContents); }); } // 自动检测更新 软件的第一个实例打开后 为了不影响加载速度 在指定时间后做一次自检 // win.once('ready-to-show', () => { // log.info('ready event'); // // TODO // setTimeout(() => { // update(win.webContents, true); // }, 1000 * 30); // }); win.webContents.session.on("will-download", (e, item) => { //监听下载结束事件 item.on("done", (e, state) => { //如果窗口还在的话,去掉进度条 // if (!win.isDestroyed()) { // win.setProgressBar(-1); // } //下载被取消或中断了 if (state === "interrupted") { dialog.showErrorBox( "下载失败", `文件 ${item.getFilename()} 因为某些原因被中断下载` ); } // 下载成功后打开文件所在文件夹 if (state === "completed") { setTimeout(() => { shell.showItemInFolder(item.getSavePath()); }, 1000); } }); }); } // 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", () => { // 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(); }); // 禁用本地缓存 // app.commandLine.appendSwitch("--disable-http-cache"); app.on("ready", async () => { log.info("app ready event"); let cb = undefined; if (process.argv[1]) { cb = (sender) => { log.info("call selected-file.." + process.argv[1]); sender.send( "selected-file", { filePaths: [process.argv[1]] }, "MenuList" ); }; } if (isDevelopment) { createWindow(); } else { showLoading(createWindow, cb); } }); ipcMain.on("open-file-dialog", (event, data) => { log.info("open file dialog"); dialog .showOpenDialog({ filters: [ { name: "all", extensions: ["fbx", "fbxc", "mcp", "mch", "bres", "alm", "bcp", "chr"], }, { name: "fbx", extensions: ["fbx", "fbxc"] }, { name: "fbs", extensions: ["mcp", "mch"] }, { name: "res", extensions: ["bres"] }, { name: "alm", extensions: ["alm"] }, { name: "bcp", extensions: ["bcp"] }, { name: "chr", extensions: ["chr"] }, ], properties: ["openFile"], }) .then((files) => { // console.log(files, '0000000') if (files) { event.sender.send("selected-file", files, data); } }); }); // app.on('will-quit', () => { // // 注销快捷键 // globalShortcut.unregister('CommandOrControl+X') // // 注销所有快捷键 // globalShortcut.unregisterAll() // }) // Exit cleanly on request from parent process in development mode. if (isDevelopment) { if (process.platform === "win32") { process.on("message", (data) => { if (data === "graceful-exit") { app.quit(); } }); } else { process.on("SIGTERM", () => { app.quit(); }); } }