소스 검색

跳转外部登陆

李富豪 5 달 전
부모
커밋
df6bd86236
2개의 변경된 파일48개의 추가작업 그리고 6개의 파일을 삭제
  1. 13 1
      app/api/api.ts
  2. 35 5
      app/components/home.tsx

+ 13 - 1
app/api/api.ts

@@ -8,7 +8,14 @@ const axiosInstance = axios.create({
 
 // 请求拦截器
 axiosInstance.interceptors.request.use(
-    (config: any) => {
+    (config) => {
+        const userInfoStr = localStorage.getItem('userInfo');
+        if (userInfoStr) {
+            const userInfo = JSON.parse(userInfoStr);
+            if (userInfo.token) {
+                config.headers['Authorization'] = userInfo.token;
+            }
+        }
         return config;
     }
 );
@@ -23,6 +30,11 @@ axiosInstance.interceptors.response.use(
             if (data.code === 200) {// 成功
                 return Promise.resolve(data);
             } else {// 失败
+                if (data.code === 401) {
+                    localStorage.removeItem('userInfo');
+                    const originUrl = window.location.origin;
+                    window.open(originUrl, '_self');
+                }
                 return Promise.reject(data);
             }
         }

+ 35 - 5
app/components/home.tsx

@@ -22,6 +22,7 @@ import { AuthPage } from "./auth";
 import { getClientConfig } from "../config/client";
 import { type ClientApi, getClientApi } from "../client/api";
 import { useAccessStore } from "../store";
+import api from "../api/api";
 
 export function Loading() {
   /** second版本注释掉进度条 */
@@ -159,7 +160,6 @@ const MaskPage = dynamic(
   }
 );
 
-
 export function useSwitchTheme() {
   const config = useAppConfig();
 
@@ -241,7 +241,6 @@ function Screen() {
   const isArtifact = location.pathname.includes(Path.Artifacts);
   const isAuth = location.pathname === Path.Auth;
 
-
   const isMobileScreen = useMobileScreen();
   const shouldTightBorder =
     getClientConfig()?.isApp || (config.tightBorder && !isMobileScreen);
@@ -261,7 +260,6 @@ function Screen() {
   const renderContent = () => {
     if (isAuth) return <AuthPage />;
 
-
     return (
       <>
         {
@@ -301,7 +299,6 @@ export function useLoadData() {
       const models = await api.llm.models();
       config.mergeModels(models);
     })();
-    // eslint-disable-next-line react-hooks/exhaustive-deps
   }, []);
 }
 
@@ -311,10 +308,43 @@ export function Home() {
   useHtmlLang();
 
   useEffect(() => {
-    // console.log("[Config] got config from build time", getClientConfig());
     useAccessStore.getState().fetch();
   }, []);
 
+  const jkLogin = async (code: string, url: string) => {
+    console.log(code, '使用code换取token');
+    console.log(url, '登录成功后要跳转回登陆前的页面');
+    try {
+      const res = await api.post('jk_code_login', { code: code });
+      localStorage.setItem('userInfo', JSON.stringify(res.data));
+      window.open(url, '_self');
+    } catch (error) {
+      const originUrl = window.location.origin;
+      window.open(originUrl, '_self');
+    }
+  }
+
+  useEffect(() => {
+    const originUrl = window.location.origin;
+    const fullUrl = window.location.href;
+    const urlParams = new URLSearchParams(new URL(fullUrl).search);
+    const code = urlParams.get('code');
+    const state = urlParams.get('state');
+    const userInfo = localStorage.getItem('userInfo');
+
+    if (fullUrl.includes(originUrl + '/?code') && code && state) {// 通过code登陆
+      if (!userInfo) {
+        jkLogin(code, state);
+      }
+    } else {
+      const loginUrl = 'https://esctest.sribs.com.cn/esc-sso/oauth2.0/authorize?client_id=e971e84b574c40b2&response_type=code';
+      const externalLoginUrl = loginUrl + `&redirect_uri=${encodeURIComponent(originUrl)}&state=${encodeURIComponent(fullUrl)}`;
+      if (!userInfo) {
+        window.open(externalLoginUrl, '_self');
+      }
+    }
+  }, []);
+
   if (!useHasHydrated()) {
     return <Loading />;
   }