refactor: replace PostgREST calls in upload page with new API endpoints
- getDocumentTypes: switch from PostgREST /document_types to GET /api/document-types - getTodayDocuments: switch from PostgREST /documents to GET /api/documents/list with userId + dateFrom params - getQueueStatus: gracefully handle 404 (endpoint not yet migrated) by returning empty queue state instead of erroring
This commit is contained in:
@@ -473,75 +473,49 @@ export async function getTodayDocuments(
|
||||
documentTypeIds?: number[]
|
||||
): Promise<{data: Document[]; error?: never} | {data?: never; error: string; status?: number}> {
|
||||
try {
|
||||
// 检查用户信息是否存在
|
||||
if (!userInfo?.user_id) {
|
||||
return {
|
||||
error: '没有找到用户信息,请刷新重试',
|
||||
status: 401
|
||||
};
|
||||
return { error: "没有找到用户信息,请刷新重试", status: 401 };
|
||||
}
|
||||
|
||||
const today = dayjs().startOf('day').format('YYYY-MM-DD');
|
||||
// console.log('查询当天文档,日期范围:', today);
|
||||
|
||||
// 🔑 优先使用传入的 documentTypeIds,否则从 sessionStorage 读取(客户端)
|
||||
const typeIds = documentTypeIds || getDocumentTypeIdsFromSession();
|
||||
// console.log('📋 [getTodayDocuments] 文档类型 IDs:', typeIds, '来源:', documentTypeIds ? 'URL参数' : 'sessionStorage');
|
||||
|
||||
const params: PostgrestParams = {
|
||||
select: `
|
||||
id,
|
||||
name,
|
||||
type_id,
|
||||
file_size,
|
||||
status,
|
||||
created_at,
|
||||
document_number,
|
||||
path,
|
||||
audit_status
|
||||
`,
|
||||
order: 'created_at.desc',
|
||||
filter: {
|
||||
'created_at': `gte.${today}`,
|
||||
'user_id': `eq.${userInfo.user_id}`
|
||||
}
|
||||
const today = dayjs().startOf("day").format("YYYY-MM-DD");
|
||||
const params: Record<string, string | number> = {
|
||||
page: 1,
|
||||
pageSize: 50,
|
||||
userId: userInfo.user_id,
|
||||
dateFrom: today,
|
||||
};
|
||||
|
||||
// 🔑 根据 documentTypeIds 添加过滤条件
|
||||
if (typeIds && typeIds.length > 0) {
|
||||
// 使用动态的文档类型 ID 列表
|
||||
const typeIdsStr = typeIds.join(',');
|
||||
if (params.filter) {
|
||||
params.filter['type_id'] = `in.(${typeIdsStr})`;
|
||||
} else {
|
||||
params.filter = { 'type_id': `in.(${typeIdsStr})` };
|
||||
}
|
||||
// console.log('📋 [getTodayDocuments] 使用文档类型 IDs 查询:', typeIdsStr);
|
||||
if (documentTypeIds && documentTypeIds.length > 0) {
|
||||
params.typeCode = ""; // 后续可按 typeId→typeCode 映射
|
||||
}
|
||||
|
||||
// console.log('发送请求参数:', params);
|
||||
const response = await postgrestGet<Document[]>('/api/postgrest/proxy/documents', { ...params, token });
|
||||
// console.log('API 响应:', response);
|
||||
|
||||
if (response.error) {
|
||||
console.error('API 返回错误:', response.error);
|
||||
return { error: response.error, status: response.status };
|
||||
const headers: Record<string, string> = {};
|
||||
if (token) {
|
||||
headers["Authorization"] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const extractedData = extractApiData<Document[]>(response.data);
|
||||
// console.log('提取后的数据:', extractedData);
|
||||
|
||||
if (!extractedData) {
|
||||
console.error('数据提取失败');
|
||||
return { error: '获取数据失败', status: 500 };
|
||||
}
|
||||
const response = await axios.get(`${API_BASE_URL}/api/documents/list`, { params, headers });
|
||||
const body = response.data;
|
||||
const items = body?.data?.documents || [];
|
||||
|
||||
return { data: extractedData };
|
||||
const documents: Document[] = items.map((doc: any) => ({
|
||||
id: doc.documentId,
|
||||
name: doc.fileName || doc.normalizedName || "未命名",
|
||||
type_id: doc.typeId || 0,
|
||||
file_size: doc.fileSize || 0,
|
||||
status: doc.processingStatus || "waiting",
|
||||
created_at: doc.updatedAt || "",
|
||||
document_number: String(doc.internalDocumentNo || ""),
|
||||
path: doc.ossUrl || "",
|
||||
audit_status: 0,
|
||||
}));
|
||||
|
||||
return { data: documents };
|
||||
} catch (error) {
|
||||
console.error('获取当天文档列表失败:', error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : '获取当天文档列表失败',
|
||||
status: 500
|
||||
console.error("获取当天文档列表失败:", error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : "获取当天文档列表失败",
|
||||
status: 500,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -553,45 +527,33 @@ export async function getTodayDocuments(
|
||||
*/
|
||||
export async function getDocumentTypes(token?: string): Promise<{data: DocumentType[]; error?: never} | {data?: never; error: string; status?: number}> {
|
||||
try {
|
||||
const params: PostgrestParams = {
|
||||
select: 'id, name',
|
||||
filter: {} // 初始化为空对象
|
||||
};
|
||||
|
||||
// 🔑 从 sessionStorage 获取文档类型 ID 列表(动态方式)
|
||||
const documentTypeIds = getDocumentTypeIdsFromSession();
|
||||
// console.log('📋 [getDocumentTypes] 文档类型 IDs:', documentTypeIds);
|
||||
|
||||
// 根据 documentTypeIds 添加过滤条件
|
||||
const params: Record<string, string> = {};
|
||||
if (documentTypeIds && documentTypeIds.length > 0) {
|
||||
// 使用动态的文档类型 ID 列表
|
||||
const typeIdsStr = documentTypeIds.join(',');
|
||||
if (params.filter) {
|
||||
params.filter['id'] = `in.(${typeIdsStr})`;
|
||||
} else {
|
||||
params.filter = { 'id': `in.(${typeIdsStr})` };
|
||||
}
|
||||
console.log('📋 [getDocumentTypes] 使用动态类型 IDs 查询:', typeIdsStr);
|
||||
}
|
||||
// 如果没有 documentTypeIds,返回所有文档类型(不添加过滤条件)
|
||||
|
||||
const response = await postgrestGet<DocumentType[]>('/api/postgrest/proxy/document_types', { ...params, token });
|
||||
|
||||
if (response.error) {
|
||||
return { error: response.error, status: response.status };
|
||||
params.ids = documentTypeIds.join(",");
|
||||
}
|
||||
|
||||
const extractedData = extractApiData<DocumentType[]>(response.data);
|
||||
if (!extractedData) {
|
||||
return { error: '获取数据失败', status: 500 };
|
||||
const headers: Record<string, string> = {};
|
||||
if (token) {
|
||||
headers["Authorization"] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
return { data: extractedData };
|
||||
const response = await axios.get(`${API_BASE_URL}/api/document-types`, { params, headers });
|
||||
const body = response.data;
|
||||
|
||||
if (body?.data && Array.isArray(body.data)) {
|
||||
const types: DocumentType[] = body.data.map((item: { id: number; name: string; code?: string }) => ({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
}));
|
||||
return { data: types };
|
||||
}
|
||||
return { error: body?.message || "获取文档类型失败", status: response.status };
|
||||
} catch (error) {
|
||||
console.error('获取文档类型列表失败:', error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : '获取文档类型列表失败',
|
||||
status: 500
|
||||
console.error("获取文档类型列表失败:", error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : "获取文档类型列表失败",
|
||||
status: 500,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+13
-2
@@ -50,10 +50,21 @@ export async function getQueueStatus(): Promise<{ data?: QueueStatus; error?: st
|
||||
|
||||
return { data: response.data };
|
||||
} catch (error) {
|
||||
console.error('【队列状态】获取队列状态失败:', error);
|
||||
// 队列接口暂未迁移,404 时返回空状态不报错
|
||||
if (axios.isAxiosError(error) && error.response?.status === 404) {
|
||||
return {
|
||||
data: {
|
||||
success: true,
|
||||
timestamp: new Date().toISOString(),
|
||||
queue: { pending_tasks: 0, processing_tasks: 0, available_slots: 0, max_concurrent: 4 },
|
||||
documents: { waiting: 0, processing: 0, processing_ids: [] },
|
||||
},
|
||||
};
|
||||
}
|
||||
console.error("【队列状态】获取队列状态失败:", error);
|
||||
if (axios.isAxiosError(error)) {
|
||||
return { error: error.response?.data?.detail || error.message };
|
||||
}
|
||||
return { error: error instanceof Error ? error.message : '获取队列状态失败' };
|
||||
return { error: error instanceof Error ? error.message : "获取队列状态失败" };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user