新增上传图片到七牛云功能,更新角色实体结构以使用 tags 替代 tagIds,重构获取角色列表逻辑以支持新接口,增加根据图片获取角色实体数据的方法。

This commit is contained in:
海龙 2025-08-10 14:52:06 +08:00
parent 7d5bc5115c
commit c48b706e9a
3 changed files with 82 additions and 6 deletions

View File

@ -2,6 +2,7 @@ import { useState, useCallback, useMemo } from 'react';
import { RoleEntity, AITextEntity } from '../domain/Entities';
import { RoleEditUseCase } from '../usecase/RoleEditUseCase';
import { getRoleData, getRoleList, getUserRoleLibrary, replaceRole } from '@/api/video_flow';
import { getUploadToken, uploadToQiniu } from '@/api/common';
/**
* Hook返回值接口
@ -33,6 +34,8 @@ interface UseRoleService {
fetchUserRoleLibrary: () => Promise<void>;
/** 替换角色 */
replaceRoleWithLibrary: (replaceRoleId: string) => Promise<void>;
/** 上传图片到七牛云 */
uploadImageToQiniu: (file: File) => Promise<void>;
}
/**
@ -292,6 +295,23 @@ export const useRoleServiceHook = (): UseRoleService => {
}
}, [selectedRole, roleEditUseCase, userRoleLibrary, initializeRoleData]);
/**
*
* @description
* @param file
* @returns {Promise<string>} URL
*/
const uploadImageToQiniu = useCallback(async (file: File) => {
try {
const { token } = await getUploadToken();
const url = await uploadToQiniu(file, token);
const roleEntity = await roleEditUseCase?.getRoleByImage(url);
} catch (error) {
console.error('上传图片到七牛云失败:', error);
throw error;
}
}, [roleEditUseCase]);
return {
// 响应式数据
roleList,
@ -308,5 +328,6 @@ export const useRoleServiceHook = (): UseRoleService => {
regenerateRole,
fetchUserRoleLibrary,
replaceRoleWithLibrary,
uploadImageToQiniu
};
};

View File

@ -37,7 +37,7 @@ export interface RoleEntity extends BaseEntity {
/** 角色提示词 */
generateText: string;
/**角色标签 */
tagIds: TagValueObject[];
tags: TagValueObject[];
/** 角色图片URL */
imageUrl: string;
}

View File

@ -1,3 +1,4 @@
import { VideoFlowProjectResponse } from '@/api/allMovieType';
import { RoleEntity } from '../domain/Entities';
import {
applyRoleToShots,
@ -7,7 +8,8 @@ import {
regenerateRole,
getRoleShots,
replaceRole,
optimizeRoleDescription
optimizeRoleDescription,
detailScriptEpisodeNew
} from '@/api/video_flow';
/**
@ -30,11 +32,13 @@ export class RoleEditUseCase {
*/
async getRoleList(projectId: string): Promise<RoleEntity[]> {
try {
const response = await getRoleList({ projectId });
if (response.successful) {
return response.data;
// const response = await getRoleList({ projectId });
const detail = await detailScriptEpisodeNew({ project_id: projectId });
const roleList = this.parseRoleList(detail.data);
if (detail.successful) {
return roleList;
} else {
throw new Error(response.message || '获取角色列表失败');
throw new Error(detail.message || '获取角色列表失败');
}
} catch (error) {
console.error('获取角色列表失败:', error);
@ -42,6 +46,25 @@ export class RoleEditUseCase {
}
}
parseRoleList(detail: VideoFlowProjectResponse): RoleEntity[] {
const characters = detail.data?.character || [];
return characters.map((char, index) => {
const roleEntity: RoleEntity = {
id: `role_${index + 1}`,
name: char.name || '',
generateText: char.description || '',
tags: [], // 默认为空标签数组
imageUrl: '', // 默认为空图片URL
loadingProgress: 100, // 默认加载完成
disableEdit: false, // 默认允许编辑
updatedAt: Date.now()
};
return roleEntity;
});
}
/**
*
* @returns Promise<RoleEntity[]>
@ -224,4 +247,36 @@ export class RoleEditUseCase {
// 例如:解析文本中的关键词、特征描述等作为标签
return [];
}
/**
* @description
* @param imageUrl
* @returns Promise<RoleEntity>
*/
async getRoleByImage(imageUrl: string): Promise<RoleEntity> {
try {
// TODO: 调用后端API根据图片地址获取角色数据
// 这里需要根据实际的后端API接口来实现
// const response = await getRoleByImage({ imageUrl });
// 临时实现:返回一个模拟的角色实体
// 实际使用时需要替换为真实的API调用
const mockRole: RoleEntity = {
id: `role_${Date.now()}`,
name: '从图片识别的角色',
generateText: '通过图片识别生成的角色描述',
tagIds: [], // 空标签数组
imageUrl: imageUrl, // 使用传入的图片地址
loadingProgress: 100, // 加载完成
disableEdit: false, // 允许编辑
updatedAt: Date.now()
};
return mockRole;
} catch (error) {
console.error('根据图片获取角色失败:', error);
throw new Error('根据图片获取角色失败');
}
}
}