index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import path from "path";
  2. import fs from "fs";
  3. import { app, BrowserWindow, Menu, screen, dialog, ipcMain } from "electron";
  4. import installExtension, { VUEJS3_DEVTOOLS } from "electron-devtools-installer";
  5. import { exec } from "child_process";
  6. import process from "process";
  7. import IniParser from "ini-parser";
  8. const isDev = process.env.NODE_ENV === "development";
  9. let win: BrowserWindow | null = null;
  10. let loadWin: any = null;
  11. const additionalData = { lockKey: "scanAdmin" };
  12. const gotTheLock = app.requestSingleInstanceLock(additionalData);
  13. console.log("gotTheLock", gotTheLock);
  14. if (!gotTheLock) {
  15. app.quit();
  16. }
  17. function createWin() {
  18. const { width, height } = screen.getPrimaryDisplay().bounds;
  19. // Menu.setApplicationMenu(null);
  20. // 创建浏览器窗口
  21. win = new BrowserWindow({
  22. width: 860,
  23. // width: width,
  24. height: 520,
  25. // height: height,
  26. frame: false,
  27. resizable: false,
  28. transparent: true,
  29. webPreferences: {
  30. preload: path.resolve(__dirname, "preload.js"),
  31. contextIsolation: true,
  32. webSecurity: false,
  33. sandbox: false,
  34. },
  35. });
  36. if (isDev) {
  37. win.loadURL(`http://localhost:8090`);
  38. } else {
  39. win.loadFile(path.resolve(__dirname, "index.html"));
  40. }
  41. win.webContents.once("dom-ready", () => {
  42. win?.show();
  43. });
  44. win.webContents.on("before-input-event", (event: any, input: any) => {
  45. if (input.key === "F12") {
  46. win?.webContents.openDevTools();
  47. }
  48. });
  49. // loadWin.destroy();
  50. win.on("closed", () => {
  51. win = null;
  52. });
  53. }
  54. function createLoadWin() {
  55. Menu.setApplicationMenu(null);
  56. loadWin = new BrowserWindow({
  57. width: 840,
  58. height: 500,
  59. backgroundColor: "#222",
  60. frame: false,
  61. transparent: true,
  62. skipTaskbar: true,
  63. resizable: false,
  64. // webPreferences: { experimentalFeatures: true },
  65. });
  66. loadWin.loadFile(path.resolve(__dirname, "static/load/index.html"));
  67. loadWin.show();
  68. setTimeout(async () => {
  69. if (isDev) {
  70. try {
  71. await installExtension(VUEJS3_DEVTOOLS);
  72. } catch (e: any) {
  73. console.error("Vue Devtools failed to install:", e.toString());
  74. }
  75. }
  76. createWin();
  77. }, 2200);
  78. loadWin.on("closed", () => {
  79. loadWin = null;
  80. });
  81. }
  82. // app.isReady()
  83. // ? createLoadWin()
  84. // : app.on("ready", () => {
  85. // createLoadWin();
  86. // });
  87. ipcMain.on("change-win-size", (event, args: string) => {
  88. // const { width, height } = screen.getPrimaryDisplay().workAreaSize;
  89. if (!win) return;
  90. const windowBounds = win.getBounds();
  91. const { width, height } =
  92. screen.getDisplayMatching(windowBounds).workAreaSize;
  93. let w = args === "big" ? width : 840;
  94. let h = args === "big" ? height : 500;
  95. if (args === "small") {
  96. win.setMinimumSize(w, h);
  97. }
  98. win.setSize(w, h);
  99. win.center();
  100. });
  101. ipcMain.on("window-min", () => {
  102. win?.minimize();
  103. });
  104. const isRunning = (query: string, cb: Function) => {
  105. exec(
  106. "cmd /c chcp 65001>nul && tasklist /FO CSV",
  107. (err: any, stdout: any, stderr: any) => {
  108. cb(stdout.toLowerCase().indexOf(query.toLowerCase()) > -1);
  109. }
  110. );
  111. };
  112. function startExe(exePath: string) {
  113. console.log("主进程接收到的exe路径:", exePath);
  114. let checkPath = exePath.includes(".exe ")
  115. ? exePath.split(".exe ")[0] + ".exe"
  116. : exePath;
  117. const fileExists = fs.existsSync(checkPath);
  118. if (fileExists) {
  119. // exec(exePath, (error, stdout, stderr) => {
  120. // console.log("子进程关闭了!");
  121. // win?.show();
  122. // });
  123. exec(exePath, (error, stdout, stderr) => {
  124. console.log("子进程关闭了!");
  125. setTimeout(() => {
  126. isRunning("client.exe", (status: boolean) => {
  127. if (!status) {
  128. win?.show();
  129. }
  130. });
  131. }, 200);
  132. });
  133. } else {
  134. dialog.showErrorBox("tip", `${checkPath}不存在!`);
  135. }
  136. }
  137. ipcMain.on("startExe", (event, args: string) => {
  138. startExe(args);
  139. });
  140. ipcMain.on("hide-app", () => {
  141. win?.hide();
  142. });
  143. ipcMain.on("get-page-size-ini", (event: any) => {
  144. const appPath = isDev ? process.cwd() : path.dirname(app.getPath("exe"));
  145. const iniFilePath = path.resolve(appPath, "page_size.ini");
  146. fs.readFile(iniFilePath, "utf8", (err: any, data: any) => {
  147. if (err) dialog.showErrorBox("tip", `page_size.ini文件不存在!`);
  148. const content = IniParser.parse(data);
  149. event.sender.send("got-page-size-ini", content);
  150. });
  151. });
  152. app.on("ready", async () => {
  153. if (isDev) {
  154. try {
  155. await installExtension(VUEJS3_DEVTOOLS);
  156. } catch (e: any) {
  157. console.error("Vue Devtools failed to install:", e.toString());
  158. }
  159. }
  160. createWin();
  161. });