2025-03-10 08:35:19 +08:00

125 lines
4.5 KiB
Python

from flask import Blueprint, render_template, request, redirect, url_for, flash, session
import requests
from frontend.config import Config
tables_bp = Blueprint('tables', __name__)
@tables_bp.route('/tables/')
def list_tables():
if not session.get('token'):
flash("请先登录", "warning")
return redirect(url_for('auth.login'))
try:
resp = requests.get(
f"{Config.BASE_API_URL}/admin/tables",
headers={"Authorization": f"Bearer {session['token']}"}
)
if resp.status_code != 200:
flash("获取桌台列表失败", "danger")
return redirect(url_for('dashboard.index'))
return render_template('tables/list.html', tables=resp.json())
except Exception as e:
flash(f"网络错误: {str(e)}", "danger")
return redirect(url_for('dashboard.index'))
@tables_bp.route('/tables/add', methods=['GET', 'POST'])
def add_table():
if not session.get('token'):
return redirect(url_for('auth.login'))
if request.method == 'POST':
try:
payload = {
"token": session['token'],
"game_table_number": str(request.form['game_table_number']),
"capacity": int(request.form['capacity']),
"price": float(request.form['price'])
}
resp = requests.post(
f"{Config.BASE_API_URL}/admin/tables/create",
json=payload,
headers={"Authorization": f"Bearer {session['token']}"}
)
if resp.status_code == 200:
flash("添加成功", "success")
return redirect(url_for('tables.list_tables'))
else:
error_msg = resp.json().get('detail', '未知错误')
flash(f"添加失败: {error_msg}", "danger")
except ValueError:
flash("输入格式不正确", "danger")
except Exception as e:
flash(f"网络错误: {str(e)}", "danger")
return render_template('tables/add.html')
@tables_bp.route('/tables/edit/<int:table_id>', methods=['GET', 'POST'])
def edit_table(table_id):
if not session.get('token'):
return redirect(url_for('auth.login'))
if request.method == 'POST':
try:
payload = {
"token": session['token'],
"game_table_number": str(request.form['game_table_number']),
"capacity": int(request.form['capacity']),
"price": float(request.form['price'])
}
resp = requests.put(
f"{Config.BASE_API_URL}/admin/tables/{table_id}",
json=payload,
headers={"Authorization": f"Bearer {session['token']}"}
)
if resp.status_code == 200:
flash("更新成功", "success")
return redirect(url_for('tables.list_tables'))
else:
error_msg = resp.json().get('detail', '未知错误')
flash(f"更新失败: {error_msg}", "danger")
except ValueError:
flash("输入格式不正确", "danger")
except Exception as e:
flash(f"网络错误: {str(e)}", "danger")
# 获取当前桌台信息
try:
resp = requests.get(
f"{Config.BASE_API_URL}/admin/tables",
headers={"Authorization": f"Bearer {session['token']}"}
)
if resp.status_code == 200:
tables = resp.json()
table = next((t for t in tables if t['table_id'] == table_id), None)
if table:
return render_template('tables/edit.html', table=table)
flash("桌台不存在", "danger")
except Exception as e:
flash(f"获取数据失败: {str(e)}", "danger")
return redirect(url_for('tables.list_tables'))
@tables_bp.route('/tables/delete/<int:table_id>', methods=['POST'])
def delete_table(table_id):
if not session.get('token'):
return redirect(url_for('auth.login'))
try:
resp = requests.delete(
f"{Config.BASE_API_URL}/admin/tables/{table_id}",
headers={"Authorization": f"Bearer {session['token']}"}
)
if resp.status_code == 200:
flash("删除成功", "success")
else:
error_msg = resp.json().get('detail', '未知错误')
flash(f"删除失败: {error_msg}", "danger")
except Exception as e:
flash(f"网络错误: {str(e)}", "danger")
return redirect(url_for('tables.list_tables'))