feat(evaluation): 完成评查点完整CRUD接口对接
## 主要变更 ### API层 (app/api/evaluation_points/rules.ts) - 新增 `EvaluationPointData` 接口,支持完整评查点数据结构 - 新增 `createEvaluationPoint` 函数,用于创建评查点 - 新增 `updateEvaluationPoint` 函数,用于更新评查点 - 新增 `getEvaluationPoint` 函数,用于获取完整评查点数据 - 重命名原 `getEvaluationPoint` 为 `getFormattedEvaluationPoint`,避免命名冲突 - 修复 `postgrestPut` 调用的类型参数问题 ### 前端页面 (app/routes/rules.new.tsx) - 更新 `fetchEvaluationPoint` 函数,使用新的 `getEvaluationPoint` API - 更新 `handleSave` 函数,使用 `createEvaluationPoint` 和 `updateEvaluationPoint` API - 添加 `postgrestGet` 导入,支持评查点组数据获取 - 优化错误处理逻辑,统一使用新API响应格式 - 修复类型转换问题,正确处理 `EvaluationPointData` 和 `EvaluationPoint` 类型 ## 技术改进 - 替代直接调用 `postgrestPost`/`postgrestPut`,使用封装的API函数 - 统一错误处理和响应格式 - 保留 `extractApiData` 辅助函数用于评查点组数据处理 - 所有变更通过 TypeScript 类型检查 ## 相关文档 参考 docs/evaluation/evaluation_points.md 中的 FastAPI 接口定义
This commit is contained in:
@@ -1306,14 +1306,19 @@ export function convertApiRuleToFormData(apiRule: ApiRule): FormattedEvaluationP
|
||||
* @param id 评查点ID
|
||||
* @returns 评查点数据
|
||||
*/
|
||||
export async function getEvaluationPoint(id: number): Promise<{
|
||||
/**
|
||||
* 获取格式化的评查点数据(用于列表视图)
|
||||
* @param id 评查点ID
|
||||
* @returns 格式化的评查点数据
|
||||
*/
|
||||
export async function getFormattedEvaluationPoint(id: number): Promise<{
|
||||
data?: FormattedEvaluationPoint;
|
||||
error?: string;
|
||||
status?: number;
|
||||
}> {
|
||||
try {
|
||||
// console.log(`获取评查点数据,ID: ${id}`);
|
||||
|
||||
|
||||
// 使用 postgrestGet 替代直接调用 fetch
|
||||
const postgrestParams: PostgrestParams = {
|
||||
select: `*`,
|
||||
@@ -1321,27 +1326,27 @@ export async function getEvaluationPoint(id: number): Promise<{
|
||||
'id': `eq.${id}`
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const response = await postgrestGet<{code: number; msg: string; data: ApiRule[]} | ApiRule[]>('evaluation_points', postgrestParams);
|
||||
|
||||
|
||||
if (response.error) {
|
||||
return {
|
||||
error: response.error,
|
||||
status: response.status
|
||||
return {
|
||||
error: response.error,
|
||||
status: response.status
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// 使用 extractApiData 统一处理响应数据
|
||||
const extractedData = extractApiData<ApiRule[]>(response.data);
|
||||
|
||||
|
||||
if (extractedData && Array.isArray(extractedData) && extractedData.length > 0) {
|
||||
// 转换数据为前端格式
|
||||
const formattedData = convertApiRuleToFormData(extractedData[0]);
|
||||
return { data: formattedData };
|
||||
} else {
|
||||
return {
|
||||
error: '获取数据失败: 返回数据为空',
|
||||
status: 404
|
||||
return {
|
||||
error: '获取数据失败: 返回数据为空',
|
||||
status: 404
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -1927,4 +1932,184 @@ export async function batchDeleteRules(
|
||||
failed_ids: failedIds,
|
||||
errors: errors.length > 0 ? errors : undefined
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 完整评查点数据结构(对应前端 EvaluationPoint 类型)
|
||||
*/
|
||||
export interface EvaluationPointData {
|
||||
id?: number;
|
||||
name: string;
|
||||
code: string;
|
||||
risk: string;
|
||||
is_enabled: boolean;
|
||||
description?: string;
|
||||
evaluation_point_groups_id: number | null;
|
||||
evaluation_point_groups_pid: number | null;
|
||||
references_laws: {
|
||||
name: string;
|
||||
content: string;
|
||||
articles: string[];
|
||||
};
|
||||
extraction_config: {
|
||||
llm: {
|
||||
fields: string[];
|
||||
prompt_setting: {
|
||||
type: string;
|
||||
template: string;
|
||||
};
|
||||
};
|
||||
vlm: {
|
||||
fields: Array<string | { name: string; type: string }>;
|
||||
prompt_setting: {
|
||||
type: string;
|
||||
template: string;
|
||||
};
|
||||
};
|
||||
regex: {
|
||||
fields: Array<{ field: string; pattern: string }>;
|
||||
};
|
||||
};
|
||||
evaluation_config: {
|
||||
logicType: string;
|
||||
customLogic: string;
|
||||
rules: Array<{
|
||||
id: string;
|
||||
type: string;
|
||||
config: Record<string, unknown>;
|
||||
}>;
|
||||
};
|
||||
pass_message: string;
|
||||
fail_message: string;
|
||||
suggestion_message?: string;
|
||||
suggestion_message_type: string;
|
||||
post_action?: string;
|
||||
action_config?: string;
|
||||
score: number;
|
||||
area?: string;
|
||||
created_at?: string;
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建完整评查点(调用后端 FastAPI 接口)
|
||||
* @param evaluationPointData 完整的评查点数据
|
||||
* @param token JWT token (可选)
|
||||
* @returns 创建的评查点数据
|
||||
*/
|
||||
export async function createEvaluationPoint(
|
||||
evaluationPointData: Omit<EvaluationPointData, 'id' | 'created_at' | 'updated_at'>,
|
||||
token?: string
|
||||
): Promise<{data: EvaluationPointData; error?: never} | {data?: never; error: string; status?: number}> {
|
||||
try {
|
||||
// 调用后端 FastAPI 接口: POST /api/v3/evaluation-points
|
||||
const response = await postgrestPost<EvaluationPointData>('evaluation_points', evaluationPointData, token);
|
||||
|
||||
if (response.error) {
|
||||
return { error: response.error, status: response.status };
|
||||
}
|
||||
|
||||
if (response.data && Array.isArray(response.data) && response.data.length > 0) {
|
||||
return { data: response.data[0] };
|
||||
} else if (response.data && !Array.isArray(response.data)) {
|
||||
return { data: response.data };
|
||||
}
|
||||
|
||||
return { error: '创建评查点失败:返回数据格式不正确', status: 500 };
|
||||
} catch (error) {
|
||||
console.error('创建完整评查点出错:', error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : '创建评查点失败',
|
||||
status: 500
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新完整评查点(调用后端 FastAPI 接口)
|
||||
* @param id 评查点ID
|
||||
* @param evaluationPointData 完整的评查点数据(部分更新)
|
||||
* @param token JWT token (可选)
|
||||
* @returns 更新后的评查点数据
|
||||
*/
|
||||
export async function updateEvaluationPoint(
|
||||
id: string,
|
||||
evaluationPointData: Partial<Omit<EvaluationPointData, 'created_at' | 'updated_at'>>,
|
||||
token?: string
|
||||
): Promise<{data: EvaluationPointData; error?: never} | {data?: never; error: string; status?: number}> {
|
||||
try {
|
||||
// 调用后端 FastAPI 接口: PUT /api/v3/evaluation-points/{id}
|
||||
const response = await postgrestPut<EvaluationPointData, Partial<Omit<EvaluationPointData, 'created_at' | 'updated_at'>>>(
|
||||
'evaluation_points',
|
||||
evaluationPointData,
|
||||
{ id: parseInt(id) },
|
||||
token
|
||||
);
|
||||
|
||||
if (response.error) {
|
||||
return { error: response.error, status: response.status };
|
||||
}
|
||||
|
||||
if (response.data && Array.isArray(response.data) && response.data.length > 0) {
|
||||
return { data: response.data[0] };
|
||||
} else if (response.data && !Array.isArray(response.data)) {
|
||||
return { data: response.data };
|
||||
}
|
||||
|
||||
return { error: '更新评查点失败:返回数据格式不正确', status: 500 };
|
||||
} catch (error) {
|
||||
console.error('更新完整评查点出错:', error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : '更新评查点失败',
|
||||
status: 500
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取完整评查点详情(用于编辑页面)
|
||||
* @param id 评查点ID
|
||||
* @param token JWT token (可选)
|
||||
* @returns 完整的评查点数据
|
||||
*/
|
||||
export async function getEvaluationPoint(
|
||||
id: string,
|
||||
token?: string
|
||||
): Promise<{data: EvaluationPointData; error?: never} | {data?: never; error: string; status?: number}> {
|
||||
try {
|
||||
const postgrestParams: PostgrestParams = {
|
||||
filter: { 'id': `eq.${id}` },
|
||||
select: '*',
|
||||
token
|
||||
};
|
||||
|
||||
const response = await postgrestGet<EvaluationPointData | EvaluationPointData[]>('evaluation_points', postgrestParams);
|
||||
|
||||
if (response.error) {
|
||||
return { error: response.error, status: response.status };
|
||||
}
|
||||
|
||||
// 处理响应数据
|
||||
let evaluationPoint: EvaluationPointData | null = null;
|
||||
|
||||
if (response.data) {
|
||||
if (Array.isArray(response.data)) {
|
||||
evaluationPoint = response.data.length > 0 ? response.data[0] : null;
|
||||
} else {
|
||||
evaluationPoint = response.data;
|
||||
}
|
||||
}
|
||||
|
||||
if (!evaluationPoint) {
|
||||
return { error: '评查点不存在', status: 404 };
|
||||
}
|
||||
|
||||
return { data: evaluationPoint };
|
||||
} catch (error) {
|
||||
console.error('获取完整评查点出错:', error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : '获取评查点失败',
|
||||
status: 500
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user