修复config.go中的URL错误

This commit is contained in:
Alex Yang
2025-11-24 22:39:23 +08:00
parent a59f655769
commit b4525678e7
14 changed files with 422 additions and 117393 deletions

View File

@@ -115,28 +115,71 @@ function fetchHostsCount() {
// 空实现,保留函数声明以避免引用错误
}
// 通用API请求函数
function apiRequest(endpoint, method = 'GET', data = null) {
// 通用API请求函数 - 添加错误处理和重试机制
function apiRequest(endpoint, method = 'GET', data = null, maxRetries = 3) {
const headers = {
'Content-Type': 'application/json'
};
const config = {
method,
headers
headers,
timeout: 10000, // 设置超时时间为10秒
};
if (data && (method === 'POST' || method === 'PUT' || method === 'DELETE')) {
config.body = JSON.stringify(data);
}
return fetch(`${API_BASE_URL}${endpoint}`, config)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
});
let retries = 0;
function makeRequest() {
return fetch(`${API_BASE_URL}${endpoint}`, config)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// 检查响应是否完整
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
// 使用.text()先获取响应文本处理可能的JSON解析错误
return response.text().then(text => {
try {
return JSON.parse(text);
} catch (e) {
console.error('JSON解析错误:', e, '响应文本:', text);
// 针对ERR_INCOMPLETE_CHUNKED_ENCODING错误进行重试
if (retries < maxRetries) {
retries++;
console.warn(`请求失败,正在进行第${retries}次重试...`);
return new Promise(resolve => setTimeout(() => resolve(makeRequest()), 1000 * retries));
}
throw new Error('JSON解析失败且重试次数已达上限');
}
});
}
return response.json();
})
.catch(error => {
console.error('API请求错误:', error);
// 检查是否为网络错误或ERR_INCOMPLETE_CHUNKED_ENCODING相关错误
if ((error.name === 'TypeError' && error.message.includes('Failed to fetch')) ||
error.message.includes('incomplete chunked encoding')) {
if (retries < maxRetries) {
retries++;
console.warn(`网络错误,正在进行第${retries}次重试...`);
return new Promise(resolve => setTimeout(() => resolve(makeRequest()), 1000 * retries));
}
}
throw error;
});
}
return makeRequest();
}
// 数字格式化函数