Browse Source

修改执行转圈问题

zyf 1 month ago
parent
commit
9837bf107a
2 changed files with 55 additions and 12 deletions
  1. 12 7
      src/components/App.tsx
  2. 43 5
      src/script/service.ts

+ 12 - 7
src/components/App.tsx

@@ -223,7 +223,7 @@ function App() {
       };
       
       const response: ApiResponse = await Dingdocs.script.runWithTimeout('callApi', 120000, apiItem, token, userInfo?.userId);
-      if (response && (response.code === 200 || response.code === 0)) {
+      if (response && response.code === 200) {
         setResult(JSON.stringify(response, null, 2));
       } else {
         setResult(`调用失败: ${response?.msg || '未知错误'}`);
@@ -231,9 +231,8 @@ function App() {
     } catch (error: any) {
       console.error('调用API失败:', error);
       setResult(`调用失败: ${error?.message || '网络请求超时,请检查网络连接'}`);
-    } finally {
-      setCallLoading(false);
     }
+    setCallLoading(false);
   };
 
   useEffect(() => {
@@ -250,16 +249,22 @@ function App() {
           if (savedToken && savedUserInfo && !tokenExpired) {
             console.log('Token 有效,使用缓存登录');
             setUserInfo(savedUserInfo);
-            await loadApiList();
-            loadDocumentInfo();
+            // 并行加载 API 列表和文档信息
+            await Promise.all([
+              loadApiList(),
+              loadDocumentInfo()
+            ]);
           } else {
             console.log('Token 无效或已过期,重新登录');
             if (tokenExpired) {
               clearAuth();
             }
             await handleAutoLogin();
-            await loadApiList();
-            loadDocumentInfo();
+            // 登录成功后并行加载
+            await Promise.all([
+              loadApiList(),
+              loadDocumentInfo()
+            ]);
           }
         } catch (error: any) {
           console.error('初始化失败:', error);

+ 43 - 5
src/script/service.ts

@@ -51,12 +51,39 @@ interface ApiResponse {
 
 const apiClient = axios.create({
   baseURL: BASE_URL,
-  timeout: 30000,
+  timeout: 60000,
   headers: {
     'Content-Type': 'application/json',
   },
 });
 
+// 请求拦截器:添加 token
+apiClient.interceptors.request.use(
+  (config) => {
+    return config;
+  },
+  (error) => {
+    return Promise.reject(error);
+  }
+);
+
+// 响应拦截器:统一处理错误
+apiClient.interceptors.response.use(
+  (response) => response,
+  (error) => {
+    if (error.code === 'ECONNABORTED') {
+      console.error('请求超时:', error.message);
+      return Promise.reject(new Error('请求超时,请检查网络连接'));
+    }
+    if (!error.response) {
+      console.error('网络错误:', error.message);
+      return Promise.reject(new Error('网络连接失败,请检查网络'));
+    }
+    console.error('API 错误:', error.response?.data || error.message);
+    return Promise.reject(error);
+  }
+);
+
 async function login(authCode: string): Promise<{ token: string; userInfo: UserInfo; expirationTime: string }> {
   const response = await axios.post<LoginResponse>(
     `${BASE_URL}/sys_user/ddh5/login`,
@@ -120,10 +147,21 @@ async function callApi(apiItem: Record<string, any>, token: string, userId: stri
   }));
   params.userId = userId;
 
-  const response = await apiClient.post('/ding/add', params, {
-    headers: { token }
-  });
-  return response.data;
+  try {
+    const response = await apiClient.post('/ding/add', params, {
+      headers: { token },
+      timeout: 90000  // 增加超时时间到90秒
+    });
+    return response.data;
+  } catch (error: any) {
+    console.error('调用API失败:', error);
+    // 返回错误响应而不是抛出异常
+    return {
+      code: -1,
+      msg: error?.message || '调用失败,请重试',
+      data: null
+    };
+  }
 }
 
 async function getDocumentInfo() {