560856cf22
Deploy / Build Production Runtime Images (push) Has been skipped
Deploy / Deploy Production (push) Has been skipped
Deploy / Production Browser E2E (push) Has been skipped
Deploy / Deploy Staging (push) Failing after 210h26m26s
CI/CD Pipeline / Frontend Lint (push) Failing after 210h27m0s
CI/CD Pipeline / Validate Code Quality And Tests (push) Failing after 210h27m7s
62 lines
1.6 KiB
TypeScript
62 lines
1.6 KiB
TypeScript
/**
|
||
* 项目相关 API
|
||
* 素材库需要 project_id,前端自动管理默认项目
|
||
*/
|
||
import apiClient from "./client";
|
||
|
||
export interface ProjectItem {
|
||
id: string;
|
||
name: string;
|
||
description: string;
|
||
}
|
||
|
||
/** 后端 ProjectResponse 只返回 id, name, description */
|
||
interface BackendProjectResponse {
|
||
id: string;
|
||
name: string;
|
||
description: string;
|
||
}
|
||
|
||
/** 后端 ListProjectsResponse 返回 { items: [...] } */
|
||
interface BackendListProjectsResponse {
|
||
items: BackendProjectResponse[];
|
||
}
|
||
|
||
const toProjectItem = (item: BackendProjectResponse): ProjectItem => ({
|
||
id: item.id,
|
||
name: item.name,
|
||
description: item.description,
|
||
});
|
||
|
||
/** 获取当前用户的项目列表 */
|
||
export const getProjects = async (): Promise<ProjectItem[]> => {
|
||
const response =
|
||
await apiClient.get<BackendListProjectsResponse>("/projects");
|
||
return (response.data.items || []).map(toProjectItem);
|
||
};
|
||
|
||
/** 创建项目 */
|
||
export const createProject = async (data: {
|
||
name: string;
|
||
description?: string;
|
||
}): Promise<ProjectItem> => {
|
||
const response = await apiClient.post<BackendProjectResponse>("/projects", {
|
||
name: data.name,
|
||
description: data.description || "",
|
||
});
|
||
return toProjectItem(response.data);
|
||
};
|
||
|
||
/** 获取或创建默认项目(素材库需要 project_id) */
|
||
export const getOrCreateDefaultProject = async (): Promise<ProjectItem> => {
|
||
const projects = await getProjects();
|
||
if (projects.length > 0) {
|
||
return projects[0];
|
||
}
|
||
// 没有项目时自动创建默认项目
|
||
return createProject({
|
||
name: "默认项目",
|
||
description: "系统自动创建的默认项目",
|
||
});
|
||
};
|