diff --git a/app/api/evaluation_points/rules.ts b/app/api/evaluation_points/rules.ts index 19c21e0..a137b45 100644 --- a/app/api/evaluation_points/rules.ts +++ b/app/api/evaluation_points/rules.ts @@ -417,6 +417,8 @@ export async function getRule(id: string, token?: string): Promise<{data: Rule; try { // 使用postgrestGet获取单个评查点数据 const postgrestParams: PostgrestParams = { + // 使用PostgREST查询参数语法 + filter: { 'id': `eq.${id}` }, // 使用PostgREST资源嵌入语法获取关联数据 select: ` id, @@ -440,21 +442,32 @@ export async function getRule(id: string, token?: string): Promise<{data: Rule; `, token }; - - // 获取评查点详情 - const response = await postgrestGet<{code: number; msg: string; data: ApiRule}>(`evaluation_points/${id}`, postgrestParams); + + // 获取评查点详情 - 使用正确的PostgREST格式 + const response = await postgrestGet<{code: number; msg: string; data: ApiRule} | ApiRule[]>('evaluation_points', postgrestParams); // 检查是否有错误响应 if (response.error) { return { error: response.error, status: response.status }; } - - // 确保响应数据存在且符合预期格式 - if (!response.data || !response.data.data) { - return { error: '接口返回数据格式不正确', status: 500 }; + + // 处理响应数据(PostgREST可能返回数组或包装对象) + let apiRule: ApiRule | null = null; + + if (response.data) { + // 如果是数组格式(PostgREST标准响应) + if (Array.isArray(response.data)) { + apiRule = response.data.length > 0 ? response.data[0] : null; + } + // 如果是包装对象格式 + else if ('data' in response.data && response.data.data) { + apiRule = response.data.data as ApiRule; + } + } + + if (!apiRule) { + return { error: '评查点不存在', status: 404 }; } - - const apiRule = response.data.data; // 获取分组信息 try { @@ -711,21 +724,34 @@ export async function updateRule(id: string, ruleData: Partial(`evaluation_points/${id}`, apiRuleData, undefined, token); + // 使用postgrestPut更新评查点 - 使用正确的PostgREST格式 + const response = await postgrestPut<{code: number; msg: string; data: ApiRule} | ApiRule[], typeof apiRuleData>('evaluation_points', apiRuleData, { id: parseInt(id) }, token); // 检查是否有错误响应 if (response.error) { return { error: response.error, status: response.status }; } - // 确保响应数据存在且符合预期格式 - if (!response.data || !response.data.data) { - return { error: '接口返回数据格式不正确', status: 500 }; + // 处理响应数据(PostgREST可能返回数组或包装对象) + let updatedRule: ApiRule | null = null; + + if (response.data) { + // 如果是数组格式(PostgREST标准响应) + if (Array.isArray(response.data)) { + updatedRule = response.data.length > 0 ? response.data[0] : null; + } + // 如果是包装对象格式 + else if ('data' in response.data && response.data.data) { + updatedRule = response.data.data as ApiRule; + } + } + + if (!updatedRule) { + return { error: '更新成功但无法获取更新后的数据', status: 500 }; } // 将API返回的数据映射到前端模型 - const rule = mapApiRuleToFrontendModel(response.data.data); + const rule = mapApiRuleToFrontendModel(updatedRule); return { data: rule }; } catch (error) {