1
0

feat: 前端适配后端新接口

适配后端统一模型 ID、协议字段、UUID 自动生成和结构化错误响应:

- 类型定义:Provider 新增 protocol 字段,Model 新增 unifiedId,CreateModelInput 移除 id
- API 客户端:提取结构化错误响应中的错误码
- 供应商管理:添加协议选择下拉框和表格列
- 模型管理:移除 ID 输入,显示统一模型 ID(只读)
- Hooks:错误码映射为友好中文消息
- 测试:所有组件测试通过,mock 数据适配新字段
- 文档:更新 README 说明协议字段和统一模型 ID
This commit is contained in:
2026-04-21 20:49:37 +08:00
parent 24f03595a7
commit feff97acbd
28 changed files with 547 additions and 78 deletions

View File

@@ -1,8 +1,19 @@
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { MessagePlugin } from 'tdesign-react';
import type { CreateModelInput, UpdateModelInput } from '@/types';
import type { CreateModelInput, UpdateModelInput, ApiError } from '@/types';
import * as api from '@/api/models';
const ERROR_MESSAGES: Record<string, string> = {
duplicate_model: '同一供应商下模型名称已存在',
invalid_provider_id: '供应商 ID 仅允许字母、数字、下划线,长度 1-64',
immutable_field: '供应商 ID 不允许修改',
provider_not_found: '供应商不存在',
};
function getErrorMessage(error: ApiError): string {
return error.code ? ERROR_MESSAGES[error.code] || error.message : error.message;
}
export const modelKeys = {
all: ['models'] as const,
filtered: (providerId?: string) => ['models', providerId] as const,
@@ -24,8 +35,8 @@ export function useCreateModel() {
queryClient.invalidateQueries({ queryKey: modelKeys.all });
MessagePlugin.success('模型创建成功');
},
onError: (error: Error) => {
MessagePlugin.error(error.message);
onError: (error: ApiError) => {
MessagePlugin.error(getErrorMessage(error));
},
});
}
@@ -40,8 +51,8 @@ export function useUpdateModel() {
queryClient.invalidateQueries({ queryKey: modelKeys.all });
MessagePlugin.success('模型更新成功');
},
onError: (error: Error) => {
MessagePlugin.error(error.message);
onError: (error: ApiError) => {
MessagePlugin.error(getErrorMessage(error));
},
});
}

View File

@@ -1,8 +1,19 @@
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { MessagePlugin } from 'tdesign-react';
import type { CreateProviderInput, UpdateProviderInput } from '@/types';
import type { CreateProviderInput, UpdateProviderInput, ApiError } from '@/types';
import * as api from '@/api/providers';
const ERROR_MESSAGES: Record<string, string> = {
duplicate_model: '同一供应商下模型名称已存在',
invalid_provider_id: '供应商 ID 仅允许字母、数字、下划线,长度 1-64',
immutable_field: '供应商 ID 不允许修改',
provider_not_found: '供应商不存在',
};
function getErrorMessage(error: ApiError): string {
return error.code ? ERROR_MESSAGES[error.code] || error.message : error.message;
}
export const providerKeys = {
all: ['providers'] as const,
};
@@ -23,8 +34,8 @@ export function useCreateProvider() {
queryClient.invalidateQueries({ queryKey: providerKeys.all });
MessagePlugin.success('供应商创建成功');
},
onError: (error: Error) => {
MessagePlugin.error(error.message);
onError: (error: ApiError) => {
MessagePlugin.error(getErrorMessage(error));
},
});
}
@@ -39,8 +50,8 @@ export function useUpdateProvider() {
queryClient.invalidateQueries({ queryKey: providerKeys.all });
MessagePlugin.success('供应商更新成功');
},
onError: (error: Error) => {
MessagePlugin.error(error.message);
onError: (error: ApiError) => {
MessagePlugin.error(getErrorMessage(error));
},
});
}