feat: 完善AI改写草稿箱功能 - 修复重头游玩、评分、数据刷新等问题

This commit is contained in:
wangwuww111
2026-03-09 14:15:00 +08:00
parent bbdccfa843
commit 18db6a8cc6
17 changed files with 1385 additions and 99 deletions

View File

@@ -1,7 +1,7 @@
/**
* 故事数据管理器
*/
import { get, post } from '../utils/http';
import { get, post, request } from '../utils/http';
export default class StoryManager {
constructor() {
@@ -128,6 +128,7 @@ export default class StoryManager {
*/
resetStory() {
this.currentNodeKey = 'start';
this.pathHistory = []; // 清空路径历史
}
/**
@@ -156,38 +157,117 @@ export default class StoryManager {
}
/**
* AI改写中间章节生成新的剧情分支
* @returns {Object|null} 成功返回新节点,失败返回 null(不改变当前状态)
* AI改写中间章节异步提交到草稿箱
* @returns {Object|null} 成功返回草稿ID,失败返回 null
*/
async rewriteBranch(storyId, prompt, userId) {
async rewriteBranchAsync(storyId, prompt, userId) {
try {
// 先标记之前的未读草稿为已读,避免轮询弹出之前的通知
await this.markAllDraftsRead(userId);
const currentNode = this.getCurrentNode();
const result = await post(`/stories/${storyId}/rewrite-branch`, {
const result = await post(`/drafts`, {
userId: userId,
storyId: storyId,
currentNodeKey: this.currentNodeKey,
pathHistory: this.pathHistory,
currentContent: currentNode?.content || '',
prompt: prompt
}, { timeout: 300000 }); // 5分钟超时AI生成需要较长时间
}, { timeout: 30000 });
// 检查是否有有效的 nodes
if (result && result.nodes) {
// AI 成功,将新分支合并到当前故事中
Object.assign(this.currentStory.nodes, result.nodes);
// 跳转到新分支的入口节点
this.currentNodeKey = result.entryNodeKey || 'branch_1';
return this.getCurrentNode();
if (result && result.draftId) {
return result;
}
// AI 失败,返回 null
console.log('AI服务不可用:', result?.error || '未知错误');
return null;
} catch (error) {
console.error('AI改写分支失败:', error?.errMsg || error?.message || JSON.stringify(error));
console.error('AI改写提交失败:', error?.errMsg || error?.message || JSON.stringify(error));
return null;
}
}
/**
* 获取用户草稿列表
*/
async getDrafts(userId) {
try {
const result = await get(`/drafts?userId=${userId}`);
return result || [];
} catch (error) {
console.error('获取草稿列表失败:', error);
return [];
}
}
/**
* 检查是否有新完成的草稿
*/
async checkNewDrafts(userId) {
try {
const result = await get(`/drafts/check-new?userId=${userId}`);
return result || { hasNew: false, count: 0, drafts: [] };
} catch (error) {
console.error('检查新草稿失败:', error);
return { hasNew: false, count: 0, drafts: [] };
}
}
/**
* 批量标记所有未读草稿为已读
*/
async markAllDraftsRead(userId) {
try {
await request({ url: `/drafts/batch-read?userId=${userId}`, method: 'PUT' });
return true;
} catch (error) {
console.error('批量标记已读失败:', error);
return false;
}
}
/**
* 获取草稿详情
*/
async getDraftDetail(draftId) {
try {
const result = await get(`/drafts/${draftId}`);
return result;
} catch (error) {
console.error('获取草稿详情失败:', error);
return null;
}
}
/**
* 删除草稿
*/
async deleteDraft(draftId, userId) {
try {
const result = await request({
url: `/drafts/${draftId}?userId=${userId}`,
method: 'DELETE'
});
return true;
} catch (error) {
console.error('删除草稿失败:', error);
return false;
}
}
/**
* 从草稿加载并播放 AI 生成的内容
*/
loadDraftContent(draft) {
if (!draft || !draft.aiNodes) return null;
// 将 AI 生成的节点合并到当前故事
if (this.currentStory) {
Object.assign(this.currentStory.nodes, draft.aiNodes);
this.currentNodeKey = draft.entryNodeKey || 'branch_1';
return this.getCurrentNode();
}
return null;
}
/**
* AI续写故事
*/