Kaynağa Gözat

add postMessage

code4eat 3 yıl önce
ebeveyn
işleme
b6bf5bdd8a

+ 8 - 8
.umirc.ts

@@ -1,7 +1,7 @@
 /*
  * @Author: your name
  * @Date: 2021-11-09 11:20:18
- * @LastEditTime: 2021-12-20 10:23:40
+ * @LastEditTime: 2021-12-20 16:33:57
  * @LastEditors: Please set LastEditors
  * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  * @FilePath: /KC-MiddlePlatform/.umirc.ts
@@ -29,11 +29,11 @@ export default defineConfig({
   },
   fastRefresh: {},
   mfsu: {},
-  qiankun: {
-    master: {
-      sandbox: {
-        experimentalStyleIsolation: true, // 严格沙箱
-      },
-    },
-  },
+  // qiankun: {
+  //   master: {
+  //     sandbox: {
+  //       experimentalStyleIsolation: true, // 严格沙箱
+  //     },
+  //   },
+  // },
 });

+ 3 - 3
mock/login.ts

@@ -1,7 +1,7 @@
 /*
  * @Author: your name
  * @Date: 2021-11-11 11:46:42
- * @LastEditTime: 2021-12-16 11:21:20
+ * @LastEditTime: 2021-12-20 11:48:56
  * @LastEditors: Please set LastEditors
  * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  * @FilePath: /KC-MiddlePlatform/mock/login.ts
@@ -71,13 +71,13 @@ const getHospSubSystemList = (req: Request, res: Response) => {
         icon: 'https://i.postimg.cc/J4fsWx1V/2x.png',
         id: 0,
         name: '平台管理',
-        url: 'http://localhost:2000',
+        url: '//localhost:2000',
       },
       {
         icon: 'https://i.postimg.cc/yNrSZ4pN/2x.png',
         id: 1,
         name: '智慧查检系统',
-        url: 'http://localhost:8804',
+        url: '//localhost:8804',
       },
     ],
     success: true,

+ 1 - 2
src/app.tsx

@@ -1,7 +1,7 @@
 /*
  * @Author: your name
  * @Date: 2021-11-09 13:57:41
- * @LastEditTime: 2021-12-20 10:23:27
+ * @LastEditTime: 2021-12-20 17:45:12
  * @LastEditors: Please set LastEditors
  * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  * @FilePath: /KC-MiddlePlatform/src/app.ts
@@ -136,7 +136,6 @@ export const request: RequestConfig = {
   timeout: 10000,
   errorConfig: {
     adaptor: (resData) => {
-      console.log({ resData });
       if (resData) {
         return {
           ...resData,

+ 27 - 0
src/global.tsx

@@ -0,0 +1,27 @@
+/*
+ * @Author: your name
+ * @Date: 2021-12-21 11:05:37
+ * @LastEditTime: 2021-12-21 15:13:11
+ * @LastEditors: Please set LastEditors
+ * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
+ * @FilePath: /KC-MiddlePlatform/src/global.tsx
+ */
+
+
+
+    window.stateFromTop = {   //挂载通信数据
+    
+    }
+
+    // window.addEventListener('message',event=>{
+    //         if(event.origin == 'http://localhost:8804'){
+    //              console.log({event});
+    //              window.stateFromTop = {  
+    //                   ...event
+    //              }
+    //         }
+    // });
+
+    window.postMessageToChild = async (data)=>{   //添加全局监听事件
+          window.postMessage({...data},'http://localhost:8804')
+    }

+ 2 - 2
src/pages/index/components/iframePage/index.tsx

@@ -1,7 +1,7 @@
 /*
  * @Author: your name
  * @Date: 2021-11-19 15:20:40
- * @LastEditTime: 2021-12-16 16:22:58
+ * @LastEditTime: 2021-12-20 11:23:06
  * @LastEditors: Please set LastEditors
  * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  * @FilePath: /KC-MiddlePlatform/src/pages/index/components/iframePage/index.tsx
@@ -31,7 +31,7 @@ const Iframe = ({ id, url }: SystemListItem) => {
     <iframe
       key={id}
       onLoad={onLoadHandle}
-      className="iframe"
+      className={`iframe`}
       src={url}
     ></iframe>
   );

+ 19 - 2
src/pages/index/index.tsx

@@ -1,7 +1,7 @@
 /*
  * @Author: your name
  * @Date: 2021-11-10 09:33:30
- * @LastEditTime: 2021-12-20 10:24:05
+ * @LastEditTime: 2021-12-21 11:04:18
  * @LastEditors: Please set LastEditors
  * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  * @FilePath: /KC-MiddlePlatform/src/pages/index.tsx
@@ -10,7 +10,7 @@
 import { useState, useEffect } from 'react';
 import TopBar from './components/topBar';
 import IframePage from './components/iframePage';
-import { useModel, MicroApp } from 'umi';
+import { useModel,history } from 'umi';
 import './index.less';
 
 const IndexPage = () => {
@@ -87,10 +87,27 @@ const IndexPage = () => {
   useEffect(() => {
     if (systemLists) setSelectableSysList(systemLists);
     if (openedSysLists) setOpenTabs(openedSysLists);
+
+
+    window.addEventListener('message', function(event){
+      if (event.origin === "http://localhost:8804"){
+               console.log(event.data);
+               if(event.data.type == 'LOGOUT'){
+                     history.replace('/login') 
+               }
+      }
+      
+    }, false);
+
+    return ()=>{
+      window.removeEventListener('message',()=>{}); 
+    }
   }, []);
 
   if (loading) return <h1>Loading...</h1>;
 
+  
+
   return (
     <div className="indexPage">
       <TopBar

+ 10 - 2
src/pages/login/index.tsx

@@ -1,7 +1,7 @@
 /*
  * @Author: your name
  * @Date: 2021-11-09 14:58:08
- * @LastEditTime: 2021-12-16 15:17:06
+ * @LastEditTime: 2021-12-21 15:51:11
  * @LastEditors: Please set LastEditors
  * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  * @FilePath: /KC-MiddlePlatform/src/pages/login/index.tsx
@@ -83,9 +83,17 @@ const LoginPage: React.FC<any> = () => {
       const data = await getHospSubSystemListFunc();
       await setInitialState((s) => ({
         ...s,
-        userData: resp,
+        userData:{},
         systemLists: data,
       }));
+      await window.postMessageToChild({
+             type:'DATA',
+             data:{
+                name: "管理员",
+                token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjEwMjgwMDU2MjAyLCJ1c2VySWQiOjMwNTd9.6nCb_0c4ZW4bgN1ogq6SRGeEhFXw_p2Rf22FLFEHtXc",
+                userId: 3057
+             }
+      });
       localStorage.setItem('KC-MiddlePlatformUserData', JSON.stringify(resp));
 
       if (data.length > 1) {

+ 11 - 1
src/typings.d.ts

@@ -1,7 +1,7 @@
 /*
  * @Author: your name
  * @Date: 2021-11-19 16:31:19
- * @LastEditTime: 2021-12-15 14:09:45
+ * @LastEditTime: 2021-12-21 11:16:16
  * @LastEditors: Please set LastEditors
  * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  * @FilePath: /KC-MiddlePlatform/src/typings.d.ts
@@ -14,3 +14,13 @@ type SystemListItem = {
   name: string;
   url: string;
 };
+
+
+declare interface PostMessageData {
+    type:'LOGOUT'|'REFRESH'|'DATA',
+    data?:any
+}
+declare interface Window  {
+  stateFromTop: any,
+  postMessageToChild:(data:PostMessageData)=>Promise<any>
+}