From 45152a5694edbfa46cb08545a28bd68623fac21a Mon Sep 17 00:00:00 2001 From: xiamuceer Date: Fri, 28 Nov 2025 20:24:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:1.=E4=BF=AE=E5=A4=8D=E7=94=9F=E6=88=90?= =?UTF-8?q?=E7=AB=A0=E8=8A=82=E5=86=85=E5=AE=B9=E7=9A=84=E9=A3=8E=E6=A0=BC?= =?UTF-8?q?=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/chapters.py | 15 ++++---- backend/app/api/wizard_stream.py | 2 +- backend/app/database.py | 4 +-- backend/app/services/import_export_service.py | 36 ++++++++++++++++--- frontend/package.json | 2 +- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/backend/app/api/chapters.py b/backend/app/api/chapters.py index 159d0bb..7a48afe 100644 --- a/backend/app/api/chapters.py +++ b/backend/app/api/chapters.py @@ -444,6 +444,7 @@ async def build_smart_chapter_context( .where(Chapter.content != "") .order_by(Chapter.chapter_number) ) + all_chapters_info = all_chapters_result.all() total_previous = len(all_chapters_info) @@ -1054,10 +1055,10 @@ async def generate_chapter_content_stream( ) style = style_result.scalar_one_or_none() if style: - # 验证风格是否可用:全局预设风格(project_id为NULL)或者当前项目的自定义风格 - if style.project_id is None or style.project_id == current_chapter.project_id: + # 验证风格是否可用:全局预设风格(user_id为NULL)或者当前用户的自定义风格 + if style.user_id is None or style.user_id == current_user_id: style_content = style.prompt_content or "" - style_type = "全局预设" if style.project_id is None else "项目自定义" + style_type = "全局预设" if style.user_id is None else "用户自定义" logger.info(f"使用指定风格: {style.name} ({style_type})") else: logger.warning(f"风格 {style_id} 不属于当前项目,无法使用") @@ -2338,7 +2339,7 @@ async def generate_single_chapter_for_batch( ) style = style_result.scalar_one_or_none() if style: - if style.project_id is None or style.project_id == chapter.project_id: + if style.user_id is None or style.user_id == user_id: style_content = style.prompt_content or "" # 构建智能上下文 @@ -2589,10 +2590,10 @@ async def regenerate_chapter_stream( ) style = style_result.scalar_one_or_none() if style: - # 验证风格是否可用:全局预设风格(project_id为NULL)或者当前项目的自定义风格 - if style.project_id is None or style.project_id == chapter.project_id: + # 验证风格是否可用:全局预设风格(user_id为NULL)或者当前用户的自定义风格 + if style.user_id is None or style.user_id == user_id: style_content = style.prompt_content or "" - style_type = "全局预设" if style.project_id is None else "项目自定义" + style_type = "全局预设" if style.user_id is None else "用户自定义" logger.info(f"✅ 使用写作风格: {style.name} ({style_type})") else: logger.warning(f"⚠️ 风格 {style_id} 不属于当前项目,跳过") diff --git a/backend/app/api/wizard_stream.py b/backend/app/api/wizard_stream.py index 8e91a03..3ff28ed 100644 --- a/backend/app/api/wizard_stream.py +++ b/backend/app/api/wizard_stream.py @@ -229,7 +229,7 @@ async def world_building_generator( try: result = await db.execute( select(WritingStyle).where( - WritingStyle.project_id.is_(None), + WritingStyle.user_id.is_(None), WritingStyle.order_index == 1 ).limit(1) ) diff --git a/backend/app/database.py b/backend/app/database.py index fd06871..e574f05 100644 --- a/backend/app/database.py +++ b/backend/app/database.py @@ -311,7 +311,7 @@ async def _init_global_writing_styles(user_id: str): async with AsyncSessionLocal() as session: # 检查是否已存在全局预设风格 result = await session.execute( - select(WritingStyle).where(WritingStyle.project_id.is_(None)) + select(WritingStyle).where(WritingStyle.user_id.is_(None)) ) existing = result.scalars().first() @@ -326,7 +326,7 @@ async def _init_global_writing_styles(user_id: str): for index, (preset_id, preset_data) in enumerate(presets.items(), start=1): style = WritingStyle( - project_id=None, # NULL 表示全局预设 + user_id=None, # NULL 表示全局预设 name=preset_data["name"], style_type="preset", preset_id=preset_id, diff --git a/backend/app/services/import_export_service.py b/backend/app/services/import_export_service.py index d4ed554..36716c0 100644 --- a/backend/app/services/import_export_service.py +++ b/backend/app/services/import_export_service.py @@ -340,10 +340,19 @@ class ImportExportService: @staticmethod async def _export_writing_styles(project_id: str, db: AsyncSession) -> List[WritingStyleExportData]: - """导出写作风格""" + """导出写作风格(用户自定义风格)""" + # 获取项目所属用户 + project_result = await db.execute( + select(Project).where(Project.id == project_id) + ) + project = project_result.scalar_one_or_none() + if not project: + return [] + + # 导出该用户的自定义风格(不包括全局预设) result = await db.execute( select(WritingStyle) - .where(WritingStyle.project_id == project_id) + .where(WritingStyle.user_id == project.user_id) .order_by(WritingStyle.order_index) ) styles = result.scalars().all() @@ -797,11 +806,30 @@ class ImportExportService: styles_data: List[Dict], db: AsyncSession ) -> int: - """导入写作风格""" + """导入写作风格(用户自定义风格)""" + # 获取项目所属用户 + project_result = await db.execute( + select(Project).where(Project.id == project_id) + ) + project = project_result.scalar_one_or_none() + if not project: + return 0 + count = 0 for style_data in styles_data: + # 检查是否已存在同名风格(避免重复导入) + existing = await db.execute( + select(WritingStyle).where( + WritingStyle.user_id == project.user_id, + WritingStyle.name == style_data.get("name") + ) + ) + if existing.scalar_one_or_none(): + logger.debug(f"风格 {style_data.get('name')} 已存在,跳过导入") + continue + style = WritingStyle( - project_id=project_id, + user_id=project.user_id, # 使用 user_id 而不是 project_id name=style_data.get("name"), style_type=style_data.get("style_type"), preset_id=style_data.get("preset_id"), diff --git a/frontend/package.json b/frontend/package.json index 37e1f44..b13acf2 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,7 +1,7 @@ { "name": "frontend", "private": true, - "version": "1.0.6", + "version": "1.0.7", "type": "module", "scripts": { "dev": "vite",