huiqi 4 месяцев назад
Родитель
Сommit
50018c1ca1
1 измененных файлов с 66 добавлено и 6 удалено
  1. 66 6
      app/components/home.tsx

+ 66 - 6
app/components/home.tsx

@@ -9,6 +9,7 @@ import {
   Routes,
   Route,
   useLocation,
+  useNavigate
 } from "react-router-dom";
 import styles from "./home.module.scss";
 import BotIcon from "../icons/bot.svg";
@@ -337,6 +338,38 @@ export function Home() {
     }
   }
 
+  const frameLogin = async (data: { clientId: string,
+    workspaceId: string,
+    workspaceName: string,
+    userName: string,
+    timestamp: string,
+    signature: string }, url: string,fullUrl:string) => {
+    try {
+      const res = await api.post('frame_login', data);
+      localStorage.setItem('userInfo', JSON.stringify(res.data));
+      location.replace(url);
+    } catch (error: any) {
+      Modal.error({
+        title: '登录失败',
+        content: error.msg,
+      });
+      //停留5秒
+      setTimeout(() => {
+        toUninLogin(url,fullUrl);
+      }, 5000);
+
+    }
+  }
+
+  const toUninLogin = async (originUrl:string, fullUrl:string) => {
+    //测试环境
+    //const loginUrl = 'https://esctest.sribs.com.cn/esc-sso/oauth2.0/authorize?client_id=e97f94cf93761f4d69e8&response_type=code';
+    //生产环境
+    const loginUrl = 'http://esc.sribs.com.cn:8080/esc-sso/oauth2.0/authorize?client_id=e97f94cf93761f4d69e8&response_type=code';
+    const externalLoginUrl = loginUrl + `&redirect_uri=${encodeURIComponent(originUrl)}&state=${encodeURIComponent(fullUrl)}`;
+    location.replace(externalLoginUrl);
+  }
+
   useEffect(() => {
     // const loginRes = { "nickName": "建科咨询虚拟账号", "userId": "2222331845498970571", "token": "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjAyMDE4Mzg0LTFmNjctNDhkYi05NjNiLTJkOGNhMDMxNTkzMiJ9.zTkTv8gDgJN7tfyxJko_zG1VsESlZACeYkpdMbITqnIpIfvHkZo8l8_Kcv6zo77GnuDyzdpOEt-GzPufD2Ye8A" };
     // if (loginRes) {
@@ -354,13 +387,40 @@ export function Home() {
         jkLogin({ code: code, redirectUrl: encodeURIComponent(originUrl) }, state);
       }
     } else {
-      //测试环境
-      //const loginUrl = 'https://esctest.sribs.com.cn/esc-sso/oauth2.0/authorize?client_id=e97f94cf93761f4d69e8&response_type=code';
-      //生产环境
-      const loginUrl = 'http://esc.sribs.com.cn:8080/esc-sso/oauth2.0/authorize?client_id=e97f94cf93761f4d69e8&response_type=code';
-      const externalLoginUrl = loginUrl + `&redirect_uri=${encodeURIComponent(originUrl)}&state=${encodeURIComponent(fullUrl)}`;
       if (!userInfo) {
-        location.replace(externalLoginUrl);
+        //判断是否是frame方式联登/frame
+        if (fullUrl.includes(originUrl + '/?frame=Y')) {
+
+          const workspaceId = urlParams.get('workspace_id');
+          const workspaceName = urlParams.get('workspace_name');
+          const username = urlParams.get('username');
+          const clientId = urlParams.get('client_id');
+          const timestamp = urlParams.get('timestamp');
+          const signature = urlParams.get('signature');
+          if (!clientId || !workspaceId || !workspaceName || !username || !timestamp || !signature) {
+            // 处理缺失参数的情况
+            Modal.error({
+              title: '参数错误',
+              content: '缺少必要的参数',
+            });
+            //停留5秒
+            setTimeout(() => {
+              toUninLogin(originUrl,fullUrl);
+            }, 5000);
+            return;
+          }
+          frameLogin({
+            clientId: clientId,
+            workspaceId: workspaceId,
+            workspaceName: workspaceName,
+            userName: username,
+            timestamp: timestamp,
+            signature: signature
+          },originUrl,fullUrl);
+
+        } else {
+          toUninLogin(originUrl,fullUrl);
+        }
       }
     }
   }, []);