banxia 1 місяць тому
батько
коміт
982fa823a2
4 змінених файлів з 50 додано та 11 видалено
  1. 11 0
      electron/main/express.ts
  2. 2 0
      src/constants/index.ts
  3. 13 8
      src/pages/home/home.tsx
  4. 24 3
      src/router/beforeEnter.tsx

+ 11 - 0
electron/main/express.ts

@@ -4,6 +4,14 @@ import { mainWindow } from "../win/mainWin";
 import log from "electron-log/main";
 import { handeGet } from "./store";
 
+const sendNavigateMessage = (path: string) => {
+  mainWindow!.webContents.send("navigate", {
+    type: "navigate",
+    path: `/?iframePath=${encodeURIComponent(path)}`,  // 通过 URL 参数传递
+    setIframeFlag: true,
+  });
+};
+
 export const initExpress = () => {
   const app = express();
   app.use(express.json());
@@ -21,6 +29,7 @@ export const initExpress = () => {
       log.error("缺少path参数" + new Date());
       return;
     }
+    sendNavigateMessage(path);
     mainWindow!.webContents.send("iframe", {
       type: "iframe",
       data: path,
@@ -47,6 +56,7 @@ export const initExpress = () => {
       log.error("缺少path参数" + new Date());
       return;
     }
+    sendNavigateMessage(path);
     mainWindow!.webContents.send("iframe", {
       type: "iframe",
       data: path,
@@ -76,6 +86,7 @@ export const initExpress = () => {
       log.error("缺少path参数" + new Date());
       return;
     }
+    sendNavigateMessage(path);
     mainWindow!.webContents.send("iframe", {
       type: "iframe",
       data: path,

+ 2 - 0
src/constants/index.ts

@@ -10,3 +10,5 @@ export const MESSAGE_STATUS = {
 };
 
 export const STORAGE_UID = "uid";
+
+export const FROM_IFRAME = "fromIframe";

+ 13 - 8
src/pages/home/home.tsx

@@ -1,22 +1,27 @@
 import React, { useEffect, useState } from "react";
-import { Layout } from "antd";
+import { useSearchParams } from "react-router-dom";
 import "./home.scss";
 
 const HomePage: React.FC = () => {
-  const [path, setPath] = useState("http://106.225.184.197:8686/#/CDSSPages");
-  useEffect(() => {
-    init();
-  }, []);
+  const [searchParams] = useSearchParams();
+  const [path, setPath] = useState(() => {
+    // 优先使用 URL 参数
+    return (
+      searchParams.get("iframePath") ||
+      "http://106.225.184.197:8686/#/CDSSPages"
+    );
+  });
 
-  const init = () => {
+  useEffect(() => {
     window["electronAPI"].on(
       "iframe",
       (result: { type: string; data: string }) => {
-        const { type, data } = result;
+        const { data } = result;
         setPath(data);
       }
     );
-  };
+  }, []);
+
   return (
     <div className="home">
       <webview

+ 24 - 3
src/router/beforeEnter.tsx

@@ -2,7 +2,7 @@ import { useLocation, useNavigate, useRoutes } from "react-router-dom";
 import { useState, useEffect } from "react";
 import storage from "@/utils/storage";
 import LayoutHeader from "@/layouts/Header";
-import { STORAGE_UID } from "@/constants";
+import { FROM_IFRAME, STORAGE_UID } from "@/constants";
 
 const BeforeEnter = ({ routers }) => {
   //1.在路由数组中找当前页面路由的对应路由项
@@ -34,8 +34,13 @@ const BeforeEnter = ({ routers }) => {
 
     // 重定向到登录页
     if (!uid && findRoute.path !== "/login") {
-      navigate("/login");
-      return;
+      // 检查是否是来自 iframe 的导航
+      const fromIframe = storage.getItem(FROM_IFRAME) === "true";
+
+      if (!fromIframe) {
+        navigate("/login");
+        return;
+      }
     }
 
     // 已登录但访问登录页
@@ -68,6 +73,22 @@ const BeforeEnter = ({ routers }) => {
   // 如果是登录页或未登录,不显示Header
   const showHeader = uid && findRoute?.path !== "/login";
 
+  useEffect(() => {
+    // 添加导航监听
+    window.electronAPI.on("navigate", (result) => {
+      const { type, path, state, setIframeFlag } = result;
+      if (type === "navigate") {
+        if (setIframeFlag) {
+          storage.setItem(FROM_IFRAME, "true");
+        }
+        navigate(path, { state }); // 传递 state
+      }
+    });
+    return () => {
+      storage.clearItem(FROM_IFRAME);
+    };
+  }, []);
+
   return (
     <div className="h-full">
       <LayoutHeader isLoginPage={findRoute?.path === "/login"} />