ソースを参照

拉取exe功能初版(简版)

刘洋 9 ヶ月 前
コミット
f0a5e65825
3 ファイル変更22 行追加92 行削除
  1. 14 85
      src/main/index.ts
  2. 7 6
      src/main/preload/index.ts
  3. 1 1
      src/render/views/Login/index.vue

+ 14 - 85
src/main/index.ts

@@ -3,7 +3,7 @@ import fs from "fs";
 import { app, BrowserWindow, Menu, screen, dialog, ipcMain } from "electron";
 import installExtension, { VUEJS3_DEVTOOLS } from "electron-devtools-installer";
 
-import child_process from "child_process";
+import { exec } from "child_process";
 import process from "process";
 
 const isDev = process.env.NODE_ENV === "development";
@@ -32,37 +32,6 @@ function createWin() {
     win.loadURL(`http://localhost:8090`);
   } else {
     win.loadFile(path.resolve(__dirname, "index.html"));
-    //现场环境可能需要配置config.json来获取web页面地址
-    // const filePath = path.join(
-    //   process.env.PORTABLE_EXECUTABLE_DIR as string,
-    //   "config.json"
-    // );
-    // if (!fs.existsSync(filePath)) {
-    //   dialog.showErrorBox("找不到配置文件", "config.json文件不存在!");
-    //   app.quit();
-    // } else {
-    //   fs.readFile(filePath, "utf-8", function (err, data) {
-    //     if (!err) {
-    //       let da: { url?: string } = {};
-    //       try {
-    //         da = JSON.parse(data);
-    //       } catch (error) {
-    //         dialog.showErrorBox(
-    //           "错误",
-    //           "您的config.json文件内容格式可能不正确,请检查!"
-    //         );
-    //         app.quit();
-    //       }
-    //       const url = da.url || "";
-    //       if (!!url) {
-    //         win.loadURL(url);
-    //       }
-    //     } else {
-    //       dialog.showErrorBox("错误", JSON.stringify(err));
-    //       app.quit();
-    //     }
-    //   });
-    // }
   }
 
   win.webContents.once("dom-ready", () => {
@@ -134,61 +103,21 @@ ipcMain.on("window-min", () => {
 });
 
 function startExe(exePath: string) {
-  const childProcess = child_process.spawn(exePath);
-
-  childProcess.on("close", (code: any) => {
-    console.log("close---- code:", code);
-  });
+  console.log("主进程接收到的exe路径:", exePath);
+  let checkPath = exePath.includes(".exe ")
+    ? exePath.split(".exe ")[0] + ".exe"
+    : exePath;
+  const fileExists = fs.existsSync(checkPath);
+  if (fileExists) {
+    exec(exePath);
+  } else {
+    dialog.showErrorBox("tip", `${checkPath}不存在!`);
+  }
 }
 
-// ipcMain.on("startExe", (event, args: string) => {
-//   startExe(args);
-// });
-
-// ipcMain.handle("checkProcessExist", async (event, processName) => {
-//   try {
-//     const processExists = await checkProcessExist(processName);
-//     if (processExists) {
-//       dialog.showErrorBox("tip", `${processName}已启动`);
-//     } else {
-//       const exePath = path.join(
-//         process.env.PORTABLE_EXECUTABLE_DIR as string,
-//         "text.exe"
-//       );
-//       const fileExists = fs.existsSync(exePath);
-//       if (fileExists) {
-//         startExe(exePath);
-//       } else {
-//         dialog.showErrorBox("tip", ".exe not exist!");
-//       }
-//     }
-//     return processExists;
-//   } catch (error) {
-//     console.error(error);
-//     return false;
-//   }
-// });
-
-// function checkProcessExist(processName: string) {
-//   return new Promise((resolve, reject) => {
-//     const command = process.platform === "win32" ? "tasklist" : "ps aux";
-//     const childProcess = child_process.spawn(command);
-
-//     childProcess.stdout.on("data", (data: any) => {
-//       if (data.toString().indexOf(processName) !== -1) {
-//         resolve(true);
-//       }
-//     });
-
-//     childProcess.stderr.on("data", (data: any) => {
-//       reject(data.toString());
-//     });
-
-//     childProcess.on("close", (code: any) => {
-//       resolve(false);
-//     });
-//   });
-// }
+ipcMain.on("startExe", (event, args: string) => {
+  startExe(args);
+});
 
 app.on("ready", async () => {
   if (isDev) {

+ 7 - 6
src/main/preload/index.ts

@@ -6,6 +6,7 @@
 const { contextBridge, ipcRenderer } = require("electron");
 const os = require("os");
 const path = require("path");
+const isDev = process.env.NODE_ENV === "development";
 
 contextBridge.exposeInMainWorld("electronApi", {
   getComputerName: () => {
@@ -18,11 +19,11 @@ contextBridge.exposeInMainWorld("electronApi", {
     ipcRenderer.send("window-min");
   },
   startScanExe: () => {
-    console.log("process.env", process.env.INIT_CWD);
-    const exePath = path.join(
-      process.env.PORTABLE_EXECUTABLE_DIR as string,
-      "text.exe"
-    );
-    ipcRenderer.send("startExe", exePath);
+    let scanExeFold: string = isDev
+      ? process.cwd()
+      : process.env.PORTABLE_EXECUTABLE_DIR || "";
+    const exePath = path.join(scanExeFold as string, "test.exe");
+    console.log("exePath", exePath);
+    ipcRenderer.send("startExe", exePath + " 参数1 参数2 参数3");
   },
 });

+ 1 - 1
src/render/views/Login/index.vue

@@ -47,7 +47,7 @@ const checkEnvHandler = () => {
 
     //todo如果请求异常,则清除本地local里的baseUrl
     //...
-  }, 2000);
+  }, 1000);
 };
 const willClose = () => {
   if (curStepIndex.value == 1 && envCheckLoading.value) {