from flask import request, jsonify, current_app from . import api_bp from .license import require_admin import os # 系统设置配置项映射 CONFIG_MAPPING = { # 基本设置 'site_name': 'SITE_NAME', 'admin_email': 'ADMIN_EMAIL', 'max_failed_attempts': 'MAX_FAILED_ATTEMPTS', 'lockout_minutes': 'LOCKOUT_MINUTES', 'max_unbind_times': 'MAX_UNBIND_TIMES', 'auth_secret_key': 'AUTH_SECRET_KEY', # 卡密设置 'license_key_length': 'LICENSE_KEY_LENGTH', 'license_key_prefix': 'LICENSE_KEY_PREFIX', 'trial_prefix': 'TRIAL_PREFIX', 'offline_cache_days': 'OFFLINE_CACHE_DAYS', # API设置 'api_version': 'API_VERSION', 'items_per_page': 'ITEMS_PER_PAGE', # 文件上传设置 'max_content_length': 'MAX_CONTENT_LENGTH', 'upload_folder': 'UPLOAD_FOLDER', # 会话设置 'session_lifetime_hours': 'SESSION_LIFETIME_HOURS' } @require_admin @api_bp.route('/settings', methods=['POST']) def save_settings(): """保存系统设置""" try: data = request.get_json() if not data: return jsonify({'success': False, 'message': '请求数据为空'}), 400 # 获取当前应用配置 app_config = current_app.config # 更新配置项 updated_configs = {} for key, value in data.items(): if key in CONFIG_MAPPING: config_key = CONFIG_MAPPING[key] # 类型转换 if key in ['max_failed_attempts', 'lockout_minutes', 'max_unbind_times', 'license_key_length', 'offline_cache_days', 'items_per_page']: value = int(value) elif key == 'max_content_length': # 转换为字节 value = int(value) * 1024 * 1024 elif key == 'session_lifetime_hours': # 转换为秒 value = int(value) * 3600 # 更新配置 app_config[config_key] = value updated_configs[config_key] = value # 特殊处理会话生命周期 if 'SESSION_LIFETIME_HOURS' in updated_configs: from datetime import timedelta app_config['PERMANENT_SESSION_LIFETIME'] = timedelta(seconds=updated_configs['SESSION_LIFETIME_HOURS']) return jsonify({ 'success': True, 'message': '设置保存成功', 'updated_configs': updated_configs }) except Exception as e: current_app.logger.error(f"保存设置失败: {str(e)}") return jsonify({'success': False, 'message': '服务器内部错误'}), 500