import { ImageStoryEntity } from "../domain/Entities"; import { AIGenerateImageStory } from "@/api/movie_start"; /** * 图片故事用例 * 负责管理图片故事模式的业务逻辑,包括图片上传、AI分析和故事生成 */ export class ImageStoryUseCase { /** 当前图片故事数据 */ private imageStory: Partial = { imageUrl: "", imageStory: "", storyType: "auto", }; /** 是否正在分析图片 */ private isAnalyzing: boolean = false; /** 是否正在上传 */ private isUploading: boolean = false; constructor() {} /** * 获取当前图片故事数据 * @returns {Partial} 图片故事数据 */ getImageStory(): Partial { return { ...this.imageStory }; } /** * 获取分析状态 * @returns {boolean} 是否正在分析 */ getAnalyzingStatus(): boolean { return this.isAnalyzing; } /** * 获取上传状态 * @returns {boolean} 是否正在上传 */ getUploadingStatus(): boolean { return this.isUploading; } /** * 设置图片故事数据 * @param {Partial} data - 要设置的图片故事数据 */ setImageStory(data: Partial): void { this.imageStory = { ...this.imageStory, ...data }; } /** * 重置图片故事数据 */ resetImageStory(): void { this.imageStory = { imageUrl: "", imageStory: "", storyType: "auto", }; this.isAnalyzing = false; this.isUploading = false; } /** * 处理图片上传 * @param {string} imageUrl - 已上传的图片URL * @returns {Promise} */ async handleImageUpload(imageUrl: string): Promise { try { this.isUploading = false; // 图片已上传,设置上传状态为false this.isAnalyzing = true; // 设置上传后的图片URL this.setImageStory({ imageUrl }); // 调用AI分析接口 await this.analyzeImageWithAI(); } catch (error) { console.error("图片分析失败:", error); // 分析失败时清空图片URL this.setImageStory({ imageUrl: "" }); throw error; } finally { this.isAnalyzing = false; } } /** * 使用AI分析图片 * @returns {Promise} */ private async analyzeImageWithAI(): Promise { try { // 调用AI分析接口 const response = await AIGenerateImageStory(this.imageStory as ImageStoryEntity); if (response.successful && response.data) { const { imageAnalysis, category } = response.data; // 更新分析结果和分类 this.setImageStory({ imageAnalysis, storyType: category || "auto", imageStory: imageAnalysis, // 将AI分析结果作为默认故事内容 }); } else { throw new Error("AI分析失败"); } } catch (error) { console.error("AI分析失败:", error); throw error; } } /** * 更新故事类型 * @param {string} storyType - 新的故事类型 */ updateStoryType(storyType: string): void { this.setImageStory({ storyType }); } /** * 更新故事内容 * @param {string} storyContent - 新的故事内容 */ updateStoryContent(storyContent: string): void { this.setImageStory({ imageStory: storyContent }); } /** * 获取故事类型选项 * @returns {Array<{key: string, label: string}>} 故事类型选项数组 */ getStoryTypeOptions(): Array<{ key: string; label: string }> { return [ { key: "auto", label: "Auto" }, { key: "adventure", label: "Adventure" }, { key: "romance", label: "Romance" }, { key: "mystery", label: "Mystery" }, { key: "fantasy", label: "Fantasy" }, { key: "comedy", label: "Comedy" }, ]; } }