main.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { app, BrowserWindow, ipcMain } from "electron";
  2. import { fileURLToPath } from "url";
  3. import path from "path";
  4. import { execFile } from "child_process";
  5. import fs from "fs";
  6. process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
  7. const __filename = fileURLToPath(import.meta.url);
  8. const __dirname = path.dirname(__filename);
  9. let mainWindow = null;
  10. function createWindow() {
  11. mainWindow = new BrowserWindow({
  12. width: 1000,
  13. height: 800,
  14. webPreferences: {
  15. preload: path.join(__dirname, "./preload.mjs"),
  16. contextIsolation: true,
  17. nodeIntegration: false,
  18. allowRunningInsecureContent: false,
  19. },
  20. });
  21. //环境判断引入不同的页面
  22. if (process.env.NODE_ENV === "development") {
  23. mainWindow.loadURL("http://localhost:5173");
  24. mainWindow.webContents.openDevTools();
  25. } else {
  26. //生产环境将引入打包好的文件
  27. mainWindow.loadFile(path.join(__dirname, "../dist/index.html"));
  28. mainWindow.webContents.openDevTools();
  29. }
  30. mainWindow.on("closed", () => {
  31. mainWindow = null;
  32. });
  33. }
  34. // **🔹 监听渲染进程调用 Python EXE**
  35. ipcMain.handle("run-python-exe", async (event, apiName, params = {}) => {
  36. return new Promise((resolve, reject) => {
  37. let pythonExePath = null;
  38. if (process.env.NODE_ENV === "development") {
  39. // **开发环境**
  40. pythonExePath = path.join(__dirname, "../serves/dist/api_test.exe");
  41. } else {
  42. //这里需要区分开发环境还是生产环境 生产环境用到这个路径
  43. pythonExePath = path.join(
  44. process.resourcesPath, // `app.asar.unpacked` 的默认路径
  45. "app.asar.unpacked",
  46. "serves",
  47. "dist",
  48. "api_test.exe"
  49. );
  50. }
  51. // **修改 Python EXE 的路径**
  52. console.log("🔹 正在执行 Python EXE:", pythonExePath);
  53. if (!fs.existsSync(pythonExePath)) {
  54. reject(`Python EXE 不存在: ${pythonExePath}`);
  55. return;
  56. }
  57. // **参数列表:API 名称 + JSON 格式参数**
  58. const args = [apiName, JSON.stringify(params)];
  59. console.log("📡 调用 Python EXE:", pythonExePath, "参数:", args);
  60. execFile(pythonExePath, args, (error, stdout, stderr) => {
  61. if (error) {
  62. console.error("❌ Python EXE 运行失败:", error);
  63. reject(`Error: ${error.message}`);
  64. return;
  65. }
  66. if (stderr) {
  67. console.warn("⚠️ Python EXE 输出警告:", stderr);
  68. }
  69. // **尝试解析 JSON**
  70. try {
  71. console.log("✅ Python EXE 输出:", stdout);
  72. resolve(JSON.parse(stdout)); // 返回 JSON 数据
  73. } catch (parseError) {
  74. console.warn("⚠️ Python EXE 返回的不是 JSON:", stdout);
  75. resolve(stdout.trim()); // 返回原始文本
  76. }
  77. });
  78. });
  79. });
  80. app.whenReady().then(createWindow);
  81. app.on("window-all-closed", () => {
  82. if (process.platform !== "darwin") app.quit();
  83. });
  84. app.on("activate", () => {
  85. if (BrowserWindow.getAllWindows().length === 0) createWindow();
  86. });
  87. export { createWindow };