fix: 素材库新建自动获取/创建默认项目以提供 project_id
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Has been cancelled
CI/CD Pipeline / Frontend Lint (pull_request) Has been cancelled

- 新增 api/projects.ts:项目 API 模块,含 getOrCreateDefaultProject
- 修改 createAssetLibrary:自动解析 project_id 再发送请求
- 后端 CreateAssetLibraryRequest 要求 project_id,前端之前未发送导致 422
- 修复 P0:新建素材库点击确定无响应

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Audit Bot
2026-06-28 22:01:38 +08:00
parent 2ee3992a94
commit 841b501c30
2 changed files with 68 additions and 2 deletions
+8 -2
View File
@@ -3,6 +3,7 @@
* Phase 1 重构:去掉 project_id,素材直接归属用户
*/
import apiClient from './client';
import { getOrCreateDefaultProject } from './projects';
/** 素材条目 */
export interface AssetItem {
@@ -93,12 +94,17 @@ export const getAssetLibraries = async (): Promise<AssetLibraryItem[]> => {
return response.data.items || [];
};
/** 创建素材库 */
/** 创建素材库(自动获取或创建默认项目以提供 project_id) */
export const createAssetLibrary = async (data: {
name: string;
kind: 'video' | 'voice' | 'image';
}): Promise<AssetLibraryItem> => {
const response = await apiClient.post('/asset-libraries', data);
// 后端要求 project_id,前端自动管理默认项目
const project = await getOrCreateDefaultProject();
const response = await apiClient.post('/asset-libraries', {
project_id: project.id,
...data,
});
return response.data;
};
+60
View File
@@ -0,0 +1,60 @@
/**
* 项目相关 API
* 素材库需要 project_id,前端自动管理默认项目
*/
import apiClient from './client';
export interface ProjectItem {
id: string;
owner_user_id: string;
name: string;
description: string;
shared_users: string[];
}
interface BackendProjectResponse {
id: string;
owner_user_id: string;
name: string;
description: string;
shared_users: string[];
}
const toProjectItem = (item: BackendProjectResponse): ProjectItem => ({
id: item.id,
owner_user_id: item.owner_user_id,
name: item.name,
description: item.description,
shared_users: item.shared_users || [],
});
/** 获取当前用户的项目列表 */
export const getProjects = async (): Promise<ProjectItem[]> => {
const response = await apiClient.get<BackendProjectResponse[]>('/projects');
return (response.data || []).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: '系统自动创建的默认项目',
});
};