修正openlist过期重新获取token

This commit is contained in:
mtvpls
2025-12-22 21:41:11 +08:00
parent 84fa95fc57
commit 2104abb84b

View File

@@ -134,13 +134,31 @@ export class OpenListClient {
const response = await fetch(url, requestOptions);
// 如果是401且未重试过清除缓存并重新登录后重试
// 检查 HTTP status 401
if (response.status === 401 && !retried) {
console.log('[OpenListClient] 收到 401清除 Token 缓存并重试');
console.log('[OpenListClient] 收到 HTTP 401清除 Token 缓存并重试');
this.clearTokenCache();
return this.fetchWithRetry(url, options, true);
}
// 检查响应体中的 code 字段OpenList 的 Token 过期时 HTTP status 是 200但 code 是 401
if (response.ok && !retried) {
try {
// 克隆响应以便读取 JSON
const clonedResponse = response.clone();
const data = await clonedResponse.json();
if (data.code === 401) {
console.log('[OpenListClient] 响应体 code 为 401Token 已过期,清除缓存并重试');
this.clearTokenCache();
return this.fetchWithRetry(url, options, true);
}
} catch (error) {
// 如果解析 JSON 失败,忽略错误,返回原始响应
console.warn('[OpenListClient] 解析响应 JSON 失败:', error);
}
}
return response;
}