feat: align frontend document and rule management flows
This commit is contained in:
+52
-40
@@ -58,7 +58,7 @@ import {
|
||||
type EvaluationPointData
|
||||
} from '~/api/evaluation_points/rules';
|
||||
import { getRulesList } from '~/api/evaluation_points/rules';
|
||||
import { postgrestGet } from '~/api/postgrest-client';
|
||||
import { getEvaluationPointGroupsByDocumentTypes, type RuleGroup as ApiRuleGroupTree } from '~/api/evaluation_points/rule-groups';
|
||||
|
||||
export const meta: MetaFunction = () => {
|
||||
return [
|
||||
@@ -280,28 +280,6 @@ export default function RuleNew() {
|
||||
setInstanceKey(`new_${Date.now()}`);
|
||||
}, []);
|
||||
|
||||
|
||||
/**
|
||||
* 从API响应中提取数据
|
||||
* @param responseData - API响应数据
|
||||
* @returns 提取的数据或null
|
||||
*/
|
||||
function extractApiData<T>(responseData: unknown): T | null {
|
||||
if (!responseData) return null;
|
||||
|
||||
// 格式1: { code: number, msg: string, data: T }
|
||||
if (typeof responseData === 'object' && responseData !== null &&
|
||||
'code' in responseData &&
|
||||
'data' in responseData &&
|
||||
(responseData as { data: unknown }).data) {
|
||||
return (responseData as { data: T }).data;
|
||||
}
|
||||
|
||||
// 格式2: 直接是数据对象
|
||||
return responseData as T;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取评查点数据
|
||||
* 编辑模式下从API获取指定ID的评查点数据
|
||||
@@ -386,32 +364,66 @@ export default function RuleNew() {
|
||||
*/
|
||||
const fetchEvaluationPointGroups = useCallback(async () => {
|
||||
try {
|
||||
// console.log("🔍 [fetchEvaluationPointGroups] 开始获取评查点组数据");
|
||||
const response = await postgrestGet('/api/postgrest/proxy/evaluation_point_groups', { token: frontendJWT });
|
||||
const storedIds = typeof window !== 'undefined' ? sessionStorage.getItem('documentTypeIds') : null;
|
||||
if (!storedIds) {
|
||||
setEvaluationPointGroups([]);
|
||||
return;
|
||||
}
|
||||
|
||||
// console.log("🔍 [fetchEvaluationPointGroups] API响应:", response);
|
||||
const parsedIds = JSON.parse(storedIds);
|
||||
if (!Array.isArray(parsedIds) || parsedIds.length === 0) {
|
||||
setEvaluationPointGroups([]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.data) {
|
||||
// 使用 extractApiData 提取数据(处理可能的包装格式)
|
||||
const extractedData = extractApiData<EvaluationPointGroup[]>(response.data);
|
||||
// console.log("🔍 [fetchEvaluationPointGroups] 提取后的数据:", extractedData);
|
||||
const documentTypeIds = parsedIds
|
||||
.map(item => Number(item))
|
||||
.filter(item => Number.isFinite(item) && item > 0);
|
||||
|
||||
if (extractedData && Array.isArray(extractedData) && extractedData.length > 0) {
|
||||
setEvaluationPointGroups(extractedData);
|
||||
// console.log(`✅ [fetchEvaluationPointGroups] 成功加载 ${extractedData.length} 个评查点组`);
|
||||
} else {
|
||||
console.warn("⚠️ [fetchEvaluationPointGroups] 提取的数据为空或格式不正确");
|
||||
setEvaluationPointGroups([]);
|
||||
}
|
||||
} else if (response.error) {
|
||||
const response = await getEvaluationPointGroupsByDocumentTypes(documentTypeIds, frontendJWT);
|
||||
if (response.error) {
|
||||
console.error('❌ [fetchEvaluationPointGroups] API返回错误:', response.error);
|
||||
setEvaluationPointGroups([]);
|
||||
return;
|
||||
}
|
||||
|
||||
const flattenGroups = (groups: ApiRuleGroupTree[]): EvaluationPointGroup[] => {
|
||||
const items: EvaluationPointGroup[] = [];
|
||||
groups.forEach(group => {
|
||||
items.push({
|
||||
id: Number(group.id),
|
||||
pid: group.pid === '0' ? 0 : Number(group.pid),
|
||||
code: group.code || '',
|
||||
name: group.name,
|
||||
description: group.description,
|
||||
is_enabled: group.is_enabled,
|
||||
created_at: group.createdAt || '',
|
||||
updated_at: group.createdAt || ''
|
||||
});
|
||||
|
||||
if (group.children && group.children.length > 0) {
|
||||
group.children.forEach(child => {
|
||||
items.push({
|
||||
id: Number(child.id),
|
||||
pid: child.pid === '0' ? 0 : Number(child.pid),
|
||||
code: child.code || '',
|
||||
name: child.name,
|
||||
description: child.description,
|
||||
is_enabled: child.is_enabled,
|
||||
created_at: child.createdAt || '',
|
||||
updated_at: child.createdAt || ''
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
return items;
|
||||
};
|
||||
|
||||
setEvaluationPointGroups(flattenGroups(response.data || []));
|
||||
} catch (error) {
|
||||
console.error('❌ [fetchEvaluationPointGroups] 获取评查点组数据失败:', error);
|
||||
setEvaluationPointGroups([]);
|
||||
// 显示错误提示但不影响应用继续使用
|
||||
toastService.error(`获取评查点组数据失败: ${error instanceof Error ? error.message : '未知错误'}\n将使用默认数据`);
|
||||
toastService.error(`获取评查点组数据失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
||||
}
|
||||
}, [frontendJWT]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user