feat:物料的 CRUD 功能完成

This commit is contained in:
wangxuefeng
2025-03-01 18:14:09 +08:00
parent e052752694
commit 76ffef36cd
4 changed files with 169 additions and 29 deletions

View File

@@ -1,23 +1,99 @@
import { type MaterialDescription } from '@vtj/core';
import instance from './instance';
export const getMaterialsList = async (data?: Record<string, any>) => {
const response = await instance.get('/api/v1/materials', {
params: data
});
// 定义响应类型
interface MaterialResponse {
code: number;
data: MaterialData | MaterialData[];
message: string;
}
/** 创建物料请求参数 */
interface CreateMaterialRequest {
project_id: number;
value: string;
}
/** 创建物料响应类型 */
interface CreateMaterialResponse {
code: number;
data: { id: string };
message: string;
}
/** 删除物料响应类型 */
interface DeleteMaterialResponse {
code: number;
data: { id: string };
message: string;
}
/**
* 获取物料列表
* @param params 查询参数
* @returns 物料列表
*/
export const getMaterialsList = async (
params?: Record<string, any>
): Promise<MaterialResponse> => {
const response = await instance.get('/api/v1/materials', { params });
return response.data;
};
export const createMaterials = async (data: any) => {
const response = await instance.post('/api/v1/materials', data);
/**
* 根据ID获取单个物料
* @param id 物料ID
* @returns 物料详情
*/
export const getMaterials = async (id: number): Promise<MaterialResponse> => {
const response = await instance.get(`/api/v1/materials/${id}`);
return response.data;
};
export const updateMaterials = async (id: string, data: any) => {
const response = await instance.put(`/api/v1/materials/${id}`, data);
type MaterialData = {
project_id: number;
value: Record<string, MaterialDescription>;
// 从原interface合并的字段
id?: number;
name?: string;
created_at?: string;
updated_at?: string;
};
function transformMaterialData(data: MaterialData) {
return {
...data,
value: JSON.stringify(data.value)
};
}
/**
* 创建新物料
* @param data 物料数据(注意 value 需要是 JSON 字符串)
* @example
* postMaterials({ project_id: 1, value: '{"Authorization": "Bearer token"}' })
*/
export const postMaterials = async (
data: MaterialData
): Promise<CreateMaterialResponse> => {
const response = await instance.post(
'/api/v1/materials',
transformMaterialData(data)
);
return response.data;
};
export const deleteMaterials = async (id: string) => {
const response = await instance.delete(`/api/v1/materials/${id}`);
/**
* 删除指定物料
* @param project_id 要删除的物料所属项目ID
* @returns 删除操作结果
* @example
* deleteMaterial('123').then(() => console.log('删除成功'))
*/
export const deleteMaterials = async (
project_id: number
): Promise<DeleteMaterialResponse> => {
const response = await instance.delete(`/api/v1/materials/${project_id}`);
return response.data;
};