暗黑主题修复
This commit is contained in:
@@ -115,7 +115,7 @@ function fetchHostsCount() {
|
||||
// 空实现,保留函数声明以避免引用错误
|
||||
}
|
||||
|
||||
// 通用API请求函数 - 添加错误处理和重试机制
|
||||
// 通用API请求函数 - 添加错误处理、重试机制和缓存
|
||||
function apiRequest(endpoint, method = 'GET', data = null, maxRetries = 3) {
|
||||
const headers = {
|
||||
'Content-Type': 'application/json'
|
||||
@@ -129,6 +129,7 @@ function apiRequest(endpoint, method = 'GET', data = null, maxRetries = 3) {
|
||||
|
||||
// 处理请求URL和参数
|
||||
let url = `${API_BASE_URL}${endpoint}`;
|
||||
let cacheKey = null;
|
||||
|
||||
if (data) {
|
||||
if (method === 'GET') {
|
||||
@@ -138,10 +139,26 @@ function apiRequest(endpoint, method = 'GET', data = null, maxRetries = 3) {
|
||||
params.append(key, data[key]);
|
||||
});
|
||||
url += `?${params.toString()}`;
|
||||
// 生成缓存键
|
||||
cacheKey = `${endpoint}_${params.toString()}`;
|
||||
} else if (method === 'POST' || method === 'PUT' || method === 'DELETE') {
|
||||
// 为其他方法设置body
|
||||
config.body = JSON.stringify(data);
|
||||
}
|
||||
} else {
|
||||
// 无参数的GET请求
|
||||
if (method === 'GET') {
|
||||
cacheKey = endpoint;
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试从缓存获取响应(仅GET请求)
|
||||
if (method === 'GET' && cacheKey && window.memoryManager) {
|
||||
const cachedResponse = memoryManager.getCacheItem('apiResponses', cacheKey);
|
||||
if (cachedResponse) {
|
||||
console.log('从缓存获取API响应:', cacheKey);
|
||||
return Promise.resolve(cachedResponse);
|
||||
}
|
||||
}
|
||||
|
||||
let retries = 0;
|
||||
@@ -159,7 +176,14 @@ function apiRequest(endpoint, method = 'GET', data = null, maxRetries = 3) {
|
||||
// 使用.text()先获取响应文本,处理可能的JSON解析错误
|
||||
return response.text().then(text => {
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
const responseData = JSON.parse(text);
|
||||
|
||||
// 缓存GET请求的响应
|
||||
if (method === 'GET' && cacheKey && window.memoryManager) {
|
||||
memoryManager.addCacheItem('apiResponses', cacheKey, responseData);
|
||||
}
|
||||
|
||||
return responseData;
|
||||
} catch (e) {
|
||||
console.error('JSON解析错误:', e, '响应文本:', text);
|
||||
// 针对ERR_INCOMPLETE_CHUNKED_ENCODING错误进行重试
|
||||
|
||||
Reference in New Issue
Block a user