from flask import request, jsonify from utils.database import get_db from utils.helpers import error_response from utils.auth import admin_required def register_config_routes(app): @app.route('/api/config', methods=['GET']) def get_configs(): conn = get_db() cursor = conn.cursor() try: cursor.execute('SELECT * FROM system_config') rows = cursor.fetchall() except Exception: rows = [] conn.close() result = [] for row in rows: row_dict = dict(row) if row_dict['config_key'] == 'aliyun_sms_access_key_secret' and row_dict['config_value']: row_dict['config_value'] = '******' result.append(row_dict) return jsonify(result) @app.route('/api/config/', methods=['GET']) def get_config(config_key): conn = get_db() cursor = conn.cursor() try: cursor.execute('SELECT * FROM system_config WHERE config_key = ?', (config_key,)) row = cursor.fetchone() except Exception: row = None conn.close() if not row: return error_response('配置项不存在') config_value = row['config_value'] if config_key == 'aliyun_sms_access_key_secret' and config_value: config_value = '******' return jsonify({ 'config_key': row['config_key'], 'config_value': config_value, 'description': row['description'] if row['description'] else '' }) @app.route('/api/config/', methods=['PUT']) @admin_required def update_config(config_key): data = request.json config_value = data.get('config_value') if config_value is None: return error_response('配置值不能为空') conn = get_db() cursor = conn.cursor() try: cursor.execute(''' UPDATE system_config SET config_value = ?, updated_at = datetime('now') WHERE config_key = ? ''', (config_value, config_key)) conn.commit() success = cursor.rowcount > 0 except Exception: success = False conn.close() if not success: return error_response('配置项不存在') return jsonify({'success': True, 'message': '配置更新成功'})