from fastapi import APIRouter, Depends from app.core.auth import get_current_admin_user, get_current_user from app.core.config import LLM_CONFIG, DEFAULT_RESET_PASSWORD, MAX_FILE_SIZE, MAX_IMAGE_SIZE from app.core.response import create_api_response from pydantic import BaseModel import json from pathlib import Path router = APIRouter() # 配置文件路径 CONFIG_FILE = Path(__file__).parent.parent.parent.parent / "config" / "system_config.json" class SystemConfigModel(BaseModel): model_name: str system_prompt: str DEFAULT_RESET_PASSWORD: str MAX_FILE_SIZE: int # 字节为单位 MAX_IMAGE_SIZE: int # 字节为单位 def load_config_from_file(): """从文件加载配置,如果文件不存在则返回默认配置""" try: if CONFIG_FILE.exists(): with open(CONFIG_FILE, 'r', encoding='utf-8') as f: return json.load(f) except Exception: pass # 返回默认配置 return { 'model_name': LLM_CONFIG['model_name'], 'system_prompt': LLM_CONFIG['system_prompt'], 'DEFAULT_RESET_PASSWORD': DEFAULT_RESET_PASSWORD, 'MAX_FILE_SIZE': MAX_FILE_SIZE, 'MAX_IMAGE_SIZE': MAX_IMAGE_SIZE } def save_config_to_file(config_data): """将配置保存到文件""" try: CONFIG_FILE.parent.mkdir(parents=True, exist_ok=True) with open(CONFIG_FILE, 'w', encoding='utf-8') as f: json.dump(config_data, f, ensure_ascii=False, indent=2) return True except Exception as e: print(f"保存配置文件失败: {e}") return False @router.get("/admin/system-config") async def get_system_config(current_user=Depends(get_current_user)): """ 获取系统配置 普通用户也可以获取 """ try: config = load_config_from_file() response_data = { 'model_name': config.get('model_name', LLM_CONFIG['model_name']), 'system_prompt': config.get('system_prompt', LLM_CONFIG['system_prompt']), 'DEFAULT_RESET_PASSWORD': config.get('DEFAULT_RESET_PASSWORD', DEFAULT_RESET_PASSWORD), 'MAX_FILE_SIZE': config.get('MAX_FILE_SIZE', MAX_FILE_SIZE), 'MAX_IMAGE_SIZE': config.get('MAX_IMAGE_SIZE', MAX_IMAGE_SIZE), } return create_api_response(code="200", message="配置获取成功", data=response_data) except Exception as e: return create_api_response(code="500", message=f"获取配置失败: {str(e)}") @router.put("/admin/system-config") async def update_system_config( config: SystemConfigModel, current_user=Depends(get_current_admin_user) ): """ 更新系统配置 只有管理员才能访问 """ try: config_data = { 'model_name': config.model_name, 'system_prompt': config.system_prompt, 'DEFAULT_RESET_PASSWORD': config.DEFAULT_RESET_PASSWORD, 'MAX_FILE_SIZE': config.MAX_FILE_SIZE, 'MAX_IMAGE_SIZE': config.MAX_IMAGE_SIZE } if not save_config_to_file(config_data): return create_api_response(code="500", message="配置保存到文件失败") # 更新运行时配置 LLM_CONFIG['model_name'] = config.model_name LLM_CONFIG['system_prompt'] = config.system_prompt import app.core.config as config_module config_module.DEFAULT_RESET_PASSWORD = config.DEFAULT_RESET_PASSWORD config_module.MAX_FILE_SIZE = config.MAX_FILE_SIZE config_module.MAX_IMAGE_SIZE = config.MAX_IMAGE_SIZE return create_api_response( code="200", message="配置更新成功,重启服务后完全生效", data=config_data ) except Exception as e: return create_api_response(code="500", message=f"更新配置失败: {str(e)}") # 在应用启动时加载配置 def load_system_config(): """在应用启动时调用,加载保存的配置""" try: config = load_config_from_file() LLM_CONFIG['model_name'] = config.get('model_name', LLM_CONFIG['model_name']) LLM_CONFIG['system_prompt'] = config.get('system_prompt', LLM_CONFIG['system_prompt']) import app.core.config as config_module config_module.DEFAULT_RESET_PASSWORD = config.get('DEFAULT_RESET_PASSWORD', DEFAULT_RESET_PASSWORD) config_module.MAX_FILE_SIZE = config.get('MAX_FILE_SIZE', MAX_FILE_SIZE) config_module.MAX_IMAGE_SIZE = config.get('MAX_IMAGE_SIZE', MAX_IMAGE_SIZE) print(f"系统配置加载成功: model={config.get('model_name')}") except Exception as e: print(f"加载系统配置失败,使用默认配置: {e}")