From 940777a86ea7e3db006ca327d9d7dfc8b30f6e3c Mon Sep 17 00:00:00 2001 From: yuliang_guo Date: Sat, 31 Jan 2026 17:46:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=91=98=E5=B7=A5?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD=E5=BC=80=E5=85=B3=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当 feature_switches 表中没有默认记录时,set_feature_switch 函数 现在会使用预定义的默认值创建记录,而不是静默失败 --- backend/app/api/v1/system_settings.py | 49 +++++++++++++++++++-------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/backend/app/api/v1/system_settings.py b/backend/app/api/v1/system_settings.py index 870771b..c5aad94 100644 --- a/backend/app/api/v1/system_settings.py +++ b/backend/app/api/v1/system_settings.py @@ -168,22 +168,41 @@ async def set_feature_switch(db: AsyncSession, tenant_id: int, feature_code: str ) default_row = result.fetchone() + # 定义功能开关默认值映射 + FEATURE_DEFAULTS = { + 'employee_sync': ('员工同步', 'system', '从钉钉同步员工信息'), + 'dingtalk_login': ('钉钉免密登录', 'system', '允许通过钉钉免密登录'), + 'ai_practice': ('AI 对练', 'feature', 'AI 对练功能'), + 'dual_practice': ('双人对练', 'feature', '双人对练功能'), + } + if default_row: - # 插入租户级配置 - await db.execute( - text(""" - INSERT INTO feature_switches (tenant_id, feature_code, feature_name, feature_group, is_enabled, description) - VALUES (:tenant_id, :feature_code, :feature_name, :feature_group, :is_enabled, :description) - """), - { - "tenant_id": tenant_id, - "feature_code": feature_code, - "feature_name": default_row[0], - "feature_group": default_row[1], - "is_enabled": 1 if is_enabled else 0, - "description": default_row[2] - } - ) + feature_name = default_row[0] + feature_group = default_row[1] + description = default_row[2] + elif feature_code in FEATURE_DEFAULTS: + feature_name, feature_group, description = FEATURE_DEFAULTS[feature_code] + else: + # 未知功能码,使用默认值 + feature_name = feature_code + feature_group = 'system' + description = f'{feature_code} 功能开关' + + # 插入租户级配置 + await db.execute( + text(""" + INSERT INTO feature_switches (tenant_id, feature_code, feature_name, feature_group, is_enabled, description) + VALUES (:tenant_id, :feature_code, :feature_name, :feature_group, :is_enabled, :description) + """), + { + "tenant_id": tenant_id, + "feature_code": feature_code, + "feature_name": feature_name, + "feature_group": feature_group, + "is_enabled": 1 if is_enabled else 0, + "description": description + } + ) # ============================================