完成评查点分组的增删改
This commit is contained in:
+82
-13
@@ -156,7 +156,26 @@ export async function apiRequest<T>(
|
||||
headers.set('Accept', 'application/json');
|
||||
}
|
||||
|
||||
// 发送请求,5秒超时
|
||||
// 针对 PostgREST 的额外处理
|
||||
if (endpoint.includes('evaluation_point_groups') && (options.method === 'POST' || options.method === 'PATCH')) {
|
||||
console.log('使用 PostgREST 特定配置处理请求');
|
||||
// 确保请求体是有效的 JSON 对象
|
||||
if (options.body && typeof options.body === 'string') {
|
||||
try {
|
||||
JSON.parse(options.body); // 验证 JSON 是否有效
|
||||
} catch (e) {
|
||||
console.error('请求体不是有效的 JSON:', options.body);
|
||||
throw new Error('请求体必须是有效的 JSON');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`发送 ${options.method || 'GET'} 请求到: ${url}`);
|
||||
if (options.body) {
|
||||
console.log(`请求体: ${options.body}`);
|
||||
}
|
||||
|
||||
// 发送请求,10秒超时
|
||||
const response = await fetchWithTimeout(url, {
|
||||
...options,
|
||||
headers
|
||||
@@ -164,9 +183,23 @@ export async function apiRequest<T>(
|
||||
|
||||
// 解析响应
|
||||
let data = null;
|
||||
const contentType = response.headers.get('content-type');
|
||||
if (contentType && contentType.includes('application/json') && response.status !== 204) {
|
||||
data = await response.json();
|
||||
let responseText = '';
|
||||
|
||||
try {
|
||||
const contentType = response.headers.get('content-type');
|
||||
responseText = await response.text();
|
||||
|
||||
if (contentType && contentType.includes('application/json') && response.status !== 204 && responseText) {
|
||||
try {
|
||||
data = JSON.parse(responseText);
|
||||
} catch (e) {
|
||||
console.error('响应不是有效的 JSON:', responseText);
|
||||
throw new Error('服务器返回无效的 JSON 响应');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('解析响应失败:', e);
|
||||
console.log('原始响应:', responseText);
|
||||
}
|
||||
|
||||
// 收集响应头信息
|
||||
@@ -175,6 +208,51 @@ export async function apiRequest<T>(
|
||||
responseHeaders[key] = value;
|
||||
});
|
||||
|
||||
// 打印响应信息
|
||||
// console.log(`响应状态: ${response.status} ${response.statusText}`);
|
||||
// console.log(`响应头:`, responseHeaders);
|
||||
// console.log(`响应体:`, data || responseText);
|
||||
|
||||
// 检查 PostgREST 特定错误
|
||||
if (!response.ok) {
|
||||
if (response.status === 400) {
|
||||
console.error('PostgREST 错误 - 无效请求:', data || responseText);
|
||||
return {
|
||||
error: '无效的请求格式,请检查数据格式是否正确',
|
||||
status: response.status,
|
||||
headers: responseHeaders
|
||||
};
|
||||
} else if (response.status === 401) {
|
||||
console.error('PostgREST 错误 - 未授权:', data || responseText);
|
||||
return {
|
||||
error: '未授权,请检查认证信息',
|
||||
status: response.status,
|
||||
headers: responseHeaders
|
||||
};
|
||||
} else if (response.status === 403) {
|
||||
console.error('PostgREST 错误 - 禁止访问:', data || responseText);
|
||||
return {
|
||||
error: '没有权限执行此操作',
|
||||
status: response.status,
|
||||
headers: responseHeaders
|
||||
};
|
||||
} else if (response.status === 404) {
|
||||
console.error('PostgREST 错误 - 资源不存在:', data || responseText);
|
||||
return {
|
||||
error: '请求的资源不存在',
|
||||
status: response.status,
|
||||
headers: responseHeaders
|
||||
};
|
||||
} else {
|
||||
console.error(`HTTP请求失败: ${response.status} - ${url}`, data || responseText);
|
||||
return {
|
||||
error: data?.msg || `请求失败: ${response.status}`,
|
||||
status: response.status,
|
||||
headers: responseHeaders
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 检查API返回的状态码
|
||||
if (data && 'code' in data && data.code !== 0) {
|
||||
console.error(`API请求失败: ${data.msg || '未知错误'} - ${url}`);
|
||||
@@ -185,15 +263,6 @@ export async function apiRequest<T>(
|
||||
};
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
console.error(`HTTP请求失败: ${response.status} - ${url}`);
|
||||
return {
|
||||
error: data?.msg || `请求失败: ${response.status}`,
|
||||
status: response.status,
|
||||
headers: responseHeaders
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
data,
|
||||
status: response.status,
|
||||
|
||||
Reference in New Issue
Block a user