Pārlūkot izejas kodu

修改token校验,时间过期自动重新登录

zyf 1 mēnesi atpakaļ
vecāks
revīzija
a32571cec2
2 mainītis faili ar 27 papildinājumiem un 3 dzēšanām
  1. 24 1
      src/components/App.tsx
  2. 3 2
      src/script/service.ts

+ 24 - 1
src/components/App.tsx

@@ -7,6 +7,7 @@ import './style.css';
 
 const TOKEN_KEY = 'julefood_token';
 const USER_INFO_KEY = 'julefood_user_info';
+const EXPIRATION_TIME_KEY = 'julefood_expiration_time';
 
 function saveToken(token: string): void {
   localStorage.setItem(TOKEN_KEY, token);
@@ -25,9 +26,24 @@ function getUserInfo(): UserInfo | null {
   return str ? JSON.parse(str) : null;
 }
 
+function saveExpirationTime(expirationTime: string): void {
+  localStorage.setItem(EXPIRATION_TIME_KEY, expirationTime);
+}
+
+function isTokenExpired(): boolean {
+  const expirationTime = localStorage.getItem(EXPIRATION_TIME_KEY);
+  if (!expirationTime) {
+    return true;
+  }
+  const expireDate = new Date(expirationTime).getTime();
+  const now = new Date().getTime();
+  return now > expireDate;
+}
+
 function clearAuth(): void {
   localStorage.removeItem(TOKEN_KEY);
   localStorage.removeItem(USER_INFO_KEY);
+  localStorage.removeItem(EXPIRATION_TIME_KEY);
 }
 
 interface ApiParam {
@@ -134,6 +150,7 @@ function App() {
       setUserInfo(result.userInfo);
       saveUserInfo(result.userInfo);
       saveToken(result.token);
+      saveExpirationTime(result.expirationTime);
       await handleConfigPermission(result.token);
     } catch (error: any) {
       console.error('自动登录失败:', error);
@@ -227,12 +244,18 @@ function App() {
         try {
           const savedToken = getToken();
           const savedUserInfo = getUserInfo();
+          const tokenExpired = isTokenExpired();
           
-          if (savedToken && savedUserInfo) {
+          if (savedToken && savedUserInfo && !tokenExpired) {
+            console.log('Token 有效,使用缓存登录');
             setUserInfo(savedUserInfo);
             await loadApiList();
             loadDocumentInfo();
           } else {
+            console.log('Token 无效或已过期,重新登录');
+            if (tokenExpired) {
+              clearAuth();
+            }
             await handleAutoLogin();
             await loadApiList();
             loadDocumentInfo();

+ 3 - 2
src/script/service.ts

@@ -57,7 +57,7 @@ const apiClient = axios.create({
   },
 });
 
-async function login(authCode: string): Promise<{ token: string; userInfo: UserInfo }> {
+async function login(authCode: string): Promise<{ token: string; userInfo: UserInfo; expirationTime: string }> {
   const response = await axios.post<LoginResponse>(
     `${BASE_URL}/sys_user/ddh5/login`,
     {
@@ -69,6 +69,7 @@ async function login(authCode: string): Promise<{ token: string; userInfo: UserI
   
   if (response.data.code === 200) {
     const token = response.data.result.token;
+    const expirationTime = response.data.result.expiration_time;
     const userInfoStr = response.data.result.user_info;
     let userInfo: UserInfo;
     try {
@@ -87,7 +88,7 @@ async function login(authCode: string): Promise<{ token: string; userInfo: UserI
       console.error('解析用户信息失败:', e);
       userInfo = { userId: '', name: '用户' };
     }
-    return { token, userInfo };
+    return { token, userInfo, expirationTime };
   } else {
     throw new Error(response.data.msg || '登录失败');
   }