113 lines
2.5 KiB
TypeScript
113 lines
2.5 KiB
TypeScript
import { type MaterialDescription } from '@vtj/core';
|
|
|
|
import instance from './instance';
|
|
|
|
// 定义响应类型
|
|
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;
|
|
};
|
|
|
|
/**
|
|
* 根据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;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
/**
|
|
* 更新物料
|
|
* @param data 物料数据
|
|
* @returns 更新操作结果
|
|
*/
|
|
export const updateMaterials = async (data: MaterialData): Promise<any> => {
|
|
const response = await instance.put(
|
|
'/api/v1/materials',
|
|
transformMaterialData(data)
|
|
);
|
|
return response.data;
|
|
};
|
|
|
|
/**
|
|
* 删除指定物料
|
|
* @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;
|
|
};
|