123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- import path from "path";
- import fs from "fs";
- import { app, BrowserWindow, Menu, screen, dialog, ipcMain } from "electron";
- import installExtension, { VUEJS3_DEVTOOLS } from "electron-devtools-installer";
- import { exec } from "child_process";
- import process from "process";
- import IniParser from "ini-parser";
- const isDev = process.env.NODE_ENV === "development";
- let win: BrowserWindow | null = null;
- let loadWin: any = null;
- const additionalData = { lockKey: "scanAdmin" };
- const gotTheLock = app.requestSingleInstanceLock(additionalData);
- console.log("gotTheLock", gotTheLock);
- if (!gotTheLock) {
- app.quit();
- }
- function createWin() {
- const { width, height } = screen.getPrimaryDisplay().bounds;
- // Menu.setApplicationMenu(null);
- // 创建浏览器窗口
- win = new BrowserWindow({
- width: 860,
- // width: width,
- height: 520,
- // height: height,
- frame: false,
- resizable: false,
- transparent: true,
- webPreferences: {
- preload: path.resolve(__dirname, "preload.js"),
- contextIsolation: true,
- webSecurity: false,
- sandbox: false,
- },
- });
- if (isDev) {
- win.loadURL(`http://localhost:8090`);
- } else {
- win.loadFile(path.resolve(__dirname, "index.html"));
- }
- win.webContents.once("dom-ready", () => {
- win?.show();
- });
- win.webContents.on("before-input-event", (event: any, input: any) => {
- if (input.key === "F12") {
- win?.webContents.openDevTools();
- }
- });
- // loadWin.destroy();
- win.on("closed", () => {
- win = null;
- });
- }
- function createLoadWin() {
- Menu.setApplicationMenu(null);
- loadWin = new BrowserWindow({
- width: 840,
- height: 500,
- backgroundColor: "#222",
- frame: false,
- transparent: true,
- skipTaskbar: true,
- resizable: false,
- // webPreferences: { experimentalFeatures: true },
- });
- loadWin.loadFile(path.resolve(__dirname, "static/load/index.html"));
- loadWin.show();
- setTimeout(async () => {
- if (isDev) {
- try {
- await installExtension(VUEJS3_DEVTOOLS);
- } catch (e: any) {
- console.error("Vue Devtools failed to install:", e.toString());
- }
- }
- createWin();
- }, 2200);
- loadWin.on("closed", () => {
- loadWin = null;
- });
- }
- // app.isReady()
- // ? createLoadWin()
- // : app.on("ready", () => {
- // createLoadWin();
- // });
- ipcMain.on("change-win-size", (event, args: string) => {
- // const { width, height } = screen.getPrimaryDisplay().workAreaSize;
- if (!win) return;
- const windowBounds = win.getBounds();
- const { width, height } =
- screen.getDisplayMatching(windowBounds).workAreaSize;
- let w = args === "big" ? width : 840;
- let h = args === "big" ? height : 500;
- if (args === "small") {
- win.setMinimumSize(w, h);
- }
- win.setSize(w, h);
- win.center();
- });
- ipcMain.on("window-min", () => {
- win?.minimize();
- });
- const isRunning = (query: string, cb: Function) => {
- exec(
- "cmd /c chcp 65001>nul && tasklist /FO CSV",
- (err: any, stdout: any, stderr: any) => {
- cb(stdout.toLowerCase().indexOf(query.toLowerCase()) > -1);
- }
- );
- };
- function startExe(exePath: string) {
- console.log("主进程接收到的exe路径:", exePath);
- let checkPath = exePath.includes(".exe ")
- ? exePath.split(".exe ")[0] + ".exe"
- : exePath;
- const fileExists = fs.existsSync(checkPath);
- if (fileExists) {
- // exec(exePath, (error, stdout, stderr) => {
- // console.log("子进程关闭了!");
- // win?.show();
- // });
- exec(exePath, (error, stdout, stderr) => {
- console.log("子进程关闭了!");
- setTimeout(() => {
- isRunning("client.exe", (status: boolean) => {
- if (!status) {
- win?.show();
- }
- });
- }, 200);
- });
- } else {
- dialog.showErrorBox("tip", `${checkPath}不存在!`);
- }
- }
- ipcMain.on("startExe", (event, args: string) => {
- startExe(args);
- });
- ipcMain.on("hide-app", () => {
- win?.hide();
- });
- ipcMain.on("get-page-size-ini", (event: any) => {
- const appPath = isDev ? process.cwd() : path.dirname(app.getPath("exe"));
- const iniFilePath = path.resolve(appPath, "page_size.ini");
- fs.readFile(iniFilePath, "utf8", (err: any, data: any) => {
- if (err) dialog.showErrorBox("tip", `page_size.ini文件不存在!`);
- const content = IniParser.parse(data);
- event.sender.send("got-page-size-ini", content);
- });
- });
- app.on("ready", async () => {
- if (isDev) {
- try {
- await installExtension(VUEJS3_DEVTOOLS);
- } catch (e: any) {
- console.error("Vue Devtools failed to install:", e.toString());
- }
- }
- createWin();
- });
|