From afeeed281e60466b576fbe74d339634cc5d07b82 Mon Sep 17 00:00:00 2001
From: Codex Assistant <codex@example.com>
Date: 星期三, 08 十月 2025 08:56:42 +0800
Subject: [PATCH] 修复评审功能和用户认证问题

---
 web/src/config/api.ts |   43 ++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/web/src/config/api.ts b/web/src/config/api.ts
index c1430be..ecbdb5e 100644
--- a/web/src/config/api.ts
+++ b/web/src/config/api.ts
@@ -17,9 +17,20 @@
 
 // GraphQL璇锋眰宸ュ叿鍑芥暟
 export const graphqlRequest = async (query: string, variables: any = {}) => {
-  // 鑾峰彇JWT token
-  const { getToken } = await import('@/utils/auth');
+  // 鑾峰彇JWT token涓庡伐鍏�
+  const { getToken, isTokenExpired, clearAuth } = await import('@/utils/auth');
   const token = getToken();
+
+  // 鑻oken杩囨湡锛岀洿鎺ユ竻鐞嗗苟璺崇櫥褰�
+  if (!token || isTokenExpired(token)) {
+    clearAuth();
+    // 閬垮厤鍦ㄧ櫥褰曢〉閲嶅璺宠浆閫犳垚鐧藉睆/寰幆
+    const atLogin = typeof window !== 'undefined' && window.location && window.location.hash?.startsWith('#/login');
+    if (!atLogin) {
+      window.location.href = '/#/login';
+    }
+    throw new Error('Token expired or missing')
+  }
 
   // 鏋勫缓璇锋眰澶�
   const headers: Record<string, string> = {
@@ -45,13 +56,39 @@
     });
 
     if (!response.ok) {
+      // 澶勭悊401鏈巿鏉�
+      if (response.status === 401) {
+        const { clearAuth } = await import('@/utils/auth');
+        clearAuth();
+        const atLogin = typeof window !== 'undefined' && window.location && window.location.hash?.startsWith('#/login');
+        if (!atLogin) {
+          window.location.href = '/#/login';
+        }
+      }
       throw new Error(`HTTP error! status: ${response.status}`);
     }
 
     const result = await response.json();
 
     if (result.errors) {
-      throw new Error(`GraphQL errors: ${JSON.stringify(result.errors)}`);
+      const msg = JSON.stringify(result.errors) || ''
+      // 璇嗗埆璁よ瘉绫婚敊璇叧閿瓧
+      const isAuthError =
+        msg.includes('Unauthorized') ||
+        msg.includes('璁よ瘉') ||
+        msg.includes('unauthorized') ||
+        msg.includes('invalid token') ||
+        msg.includes('expired')
+
+      if (isAuthError) {
+        const { clearAuth } = await import('@/utils/auth');
+        clearAuth();
+        const atLogin = typeof window !== 'undefined' && window.location && window.location.hash?.startsWith('#/login');
+        if (!atLogin) {
+          window.location.href = '/#/login';
+        }
+      }
+      throw new Error(`GraphQL errors: ${msg}`);
     }
 
     return result;

--
Gitblit v1.8.0