Quellcode durchsuchen

fix: 401 响应不再受 silent 标志影响,始终跳转登录页

轮询类 API(silent=true)收到 401 时不触发 handleUnauthorized,
导致 token 过期后用户停留在当前页面无法感知。改为 401 一律跳转登录页,
轮询函数检测到认证错误后立即中止不再重试。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
skyline vor 1 Woche
Ursprung
Commit
84ca809cfb
2 geänderte Dateien mit 25 neuen und 7 gelöschten Zeilen
  1. 21 3
      haha-mp/src/api/status.ts
  2. 4 4
      haha-mp/src/utils/request.ts

+ 21 - 3
haha-mp/src/api/status.ts

@@ -149,9 +149,15 @@ export const pollDeviceStatus = (
         }
 
         timerId = setTimeout(poll, interval);
-      } catch (error) {
+      } catch (error: any) {
         if (settled) return;
 
+        // 认证失败立即中止,不重试
+        if (error?.isAuthError) {
+          done(null, new Error('登录已过期,请重新登录'));
+          return;
+        }
+
         failCount++;
 
         if (failCount >= maxConsecutiveFailures) {
@@ -229,9 +235,15 @@ export const pollRecognizeResult = (
         }
 
         timerId = setTimeout(poll, interval);
-      } catch (error) {
+      } catch (error: any) {
         if (settled) return;
 
+        // 认证失败立即中止,不重试
+        if (error?.isAuthError) {
+          done(null, new Error('登录已过期,请重新登录'));
+          return;
+        }
+
         failCount++;
 
         if (failCount >= maxConsecutiveFailures) {
@@ -309,9 +321,15 @@ export const pollOrderInfo = (
         }
 
         timerId = setTimeout(poll, interval);
-      } catch (error) {
+      } catch (error: any) {
         if (settled) return;
 
+        // 认证失败立即中止,不重试
+        if (error?.isAuthError) {
+          done(null, new Error('登录已过期,请重新登录'));
+          return;
+        }
+
         failCount++;
 
         if (failCount >= maxConsecutiveFailures) {

+ 4 - 4
haha-mp/src/utils/request.ts

@@ -101,10 +101,10 @@ export const request = <T = any>(config: RequestConfig): Promise<T> => {
           }
           resolve(responseData.data as T);
         } else if (responseData.code === 401) {
-          if (!silent) {
-            handleUnauthorized();
-          }
-          reject(new Error(responseData.message || '未登录'));
+          handleUnauthorized();
+          const authError = new Error(responseData.message || '未登录');
+          (authError as any).isAuthError = true;
+          reject(authError);
         } else {
           const errorMsg = responseData.message || '操作失败';
           if (!silent) {