From 58d9f460b2f8c34430285115e2557d18333c5cab Mon Sep 17 00:00:00 2001
From: Codex Assistant <codex@example.com>
Date: 星期三, 08 十月 2025 14:16:55 +0800
Subject: [PATCH] feat: 修复Player实体phone字段数据冗余问题并优化小程序报名逻辑

---
 web/src/config/api.ts |   75 +++++++++++++++++++++++++------------
 1 files changed, 51 insertions(+), 24 deletions(-)

diff --git a/web/src/config/api.ts b/web/src/config/api.ts
index 62cf341..ecbdb5e 100644
--- a/web/src/config/api.ts
+++ b/web/src/config/api.ts
@@ -17,54 +17,81 @@
 
 // GraphQL璇锋眰宸ュ叿鍑芥暟
 export const graphqlRequest = async (query: string, variables: any = {}) => {
-  console.log('=== GraphQL璇锋眰寮�濮� ===');
-  console.log('璇锋眰绔偣:', API_CONFIG.GRAPHQL_ENDPOINT);
-  console.log('鏌ヨ璇彞:', query);
-  console.log('鍙橀噺:', variables);
-  
-  // 鑾峰彇JWT token
-  const { getToken } = await import('@/utils/auth');
+  // 鑾峰彇JWT token涓庡伐鍏�
+  const { getToken, isTokenExpired, clearAuth } = await import('@/utils/auth');
   const token = getToken();
-  console.log('JWT Token:', token ? '宸茶幏鍙�' : '鏈幏鍙�');
-  
+
+  // 鑻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> = {
     'Content-Type': 'application/json',
   };
+
   if (token) {
     headers['Authorization'] = `Bearer ${token}`;
   }
-  console.log('璇锋眰澶�:', headers);
 
+  // 鏋勫缓璇锋眰浣�
   const requestBody = JSON.stringify({
     query,
     variables,
   });
-  console.log('璇锋眰浣�:', requestBody);
 
   try {
+    // 鍙戦�佽姹�
     const response = await fetch(API_CONFIG.GRAPHQL_ENDPOINT, {
       method: 'POST',
-      headers: headers,
+      headers,
       body: requestBody,
-    })
-
-    console.log('鍝嶅簲鐘舵��:', response.status);
-    console.log('鍝嶅簲鐘舵�佹枃鏈�:', response.statusText);
+    });
 
     if (!response.ok) {
-      throw new Error(`HTTP error! status: ${response.status}`)
+      // 澶勭悊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()
-    console.log('鍝嶅簲缁撴灉:', result);
-    
+    const result = await response.json();
+
     if (result.errors) {
-      console.error('GraphQL閿欒:', result.errors);
-      throw new Error(result.errors[0].message)
+      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}`);
     }
 
-    console.log('杩斿洖鏁版嵁:', result.data);
-    return result.data
+    return result;
   } catch (error) {
     console.error('=== GraphQL璇锋眰澶辫触 ===');
     console.error('閿欒璇︽儏:', error);

--
Gitblit v1.8.0