59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
from flask import Blueprint, render_template, session, redirect, url_for, flash, request
|
|
import requests
|
|
from frontend.config import Config
|
|
|
|
config_bp = Blueprint('config', __name__, url_prefix='/admin/config')
|
|
|
|
@config_bp.route('/manage', methods=['GET', 'POST'])
|
|
def manage_config():
|
|
if not session.get('token'):
|
|
return redirect(url_for('auth.login'))
|
|
|
|
# 获取所有配置
|
|
price_resp = requests.get(f"{Config.BASE_API_URL}/admin/config/price")
|
|
night_resp = requests.get(f"{Config.BASE_API_URL}/admin/config/night")
|
|
|
|
config = {
|
|
'price': price_resp.json() if price_resp.status_code == 200 else {},
|
|
'night': night_resp.json() if night_resp.status_code == 200 else {}
|
|
}
|
|
print(config)
|
|
|
|
if request.method == 'POST':
|
|
field = request.form.get('field')
|
|
value = request.form.get('value')
|
|
|
|
# 定义所有配置项的API端点映射
|
|
endpoints = {
|
|
'points_rate': '/points-rate',
|
|
'get_points_rate': '/get-points-rate',
|
|
'start_time': '/night-start',
|
|
'end_time': '/night-end',
|
|
'price_minutes': '/price-minutes'
|
|
}
|
|
|
|
# 构建请求参数
|
|
json_data = {"token": session['token']}
|
|
if field in ['points_rate', 'get_points_rate']:
|
|
json_data[field] = float(value)
|
|
elif field in ['start_time', 'end_time']:
|
|
json_data['time' if field in ['start_time', 'end_time'] else field] = value
|
|
else:
|
|
json_data[field] = int(value)
|
|
|
|
resp = requests.post(
|
|
f"{Config.BASE_API_URL}/admin/config{endpoints[field]}",
|
|
json=json_data
|
|
)
|
|
|
|
if resp.status_code == 200:
|
|
flash("配置更新成功", "success")
|
|
else:
|
|
error_detail = resp.json().get('detail', '未知错误')
|
|
flash(f"更新失败: {error_detail}", "danger")
|
|
|
|
return redirect(url_for('config.manage_config'))
|
|
|
|
return render_template('config/manage.html', config=config)
|
|
|