diff --git a/frontend/config.py b/frontend/config.py index d88b140..03fff1c 100644 --- a/frontend/config.py +++ b/frontend/config.py @@ -3,3 +3,6 @@ import os class Config: SECRET_KEY = os.getenv('SECRET_KEY', 'jagxor-xokwis-dekqE6') BASE_API_URL = os.getenv('BASE_API_URL', 'http://127.0.0.1:8000') + WX_APP_ID = os.getenv('WX_APP_ID', 'wx2c0e64b724b6dec4') + WX_APP_SECRET = os.getenv('WX_APP_SECRET', 'b5330aece22ce5cf5df3048cb28d1558') + WX_QRCODE_API = 'https://api.weixin.qq.com/wxa/getwxacode' diff --git a/frontend/routes/tables.py b/frontend/routes/tables.py index a3fd979..4400440 100644 --- a/frontend/routes/tables.py +++ b/frontend/routes/tables.py @@ -1,6 +1,14 @@ from flask import Blueprint, render_template, request, redirect, url_for, flash, session import requests from frontend.config import Config +import time +import base64 + +wx_token_cache = { + 'access_token': None, + 'expires_at': 0 # 过期时间戳 +} + tables_bp = Blueprint('tables', __name__) @@ -160,5 +168,52 @@ def list_all_tables(): headers={"Authorization": f"Bearer {session['token']}"} ) return resp.json() + except Exception as e: + return {"detail": str(e)}, 500 + + +@tables_bp.route('/tables/qrcode/') +def get_table_qrcode(table_id): + if not session.get('token'): + return {"detail": "未认证"}, 401 + + # 获取access_token + try: + current_time = time.time() + if current_time > wx_token_cache['expires_at']: + resp = requests.get( + "https://api.weixin.qq.com/cgi-bin/token", + params={ + "grant_type": "client_credential", + "appid": Config.WX_APP_ID, + "secret": Config.WX_APP_SECRET + } + ) + token_data = resp.json() + if 'access_token' not in token_data: + return {"detail": "获取token失败"}, 500 + + wx_token_cache['access_token'] = token_data['access_token'] + wx_token_cache['expires_at'] = current_time + token_data['expires_in'] - 300 # 提前5分钟过期 + + # 生成小程序码 + qr_resp = requests.post( + Config.WX_QRCODE_API, + params={"access_token": wx_token_cache['access_token']}, + json={ + "path": f"/pages/timer/timer?table_id={table_id}", + "env_version": "trial", + "width": 280 + } + ) + + if qr_resp.status_code != 200 or 'image' not in qr_resp.headers.get('Content-Type', ''): + return {"detail": "生成二维码失败"}, 500 + + return { + "image": f"data:image/png;base64,{base64.b64encode(qr_resp.content).decode('utf-8')}", + "table_id": table_id + } + except Exception as e: return {"detail": str(e)}, 500 \ No newline at end of file diff --git a/frontend/templates/tables/list.html b/frontend/templates/tables/list.html index c680117..b7eb94e 100644 --- a/frontend/templates/tables/list.html +++ b/frontend/templates/tables/list.html @@ -4,130 +4,172 @@ {% block content %} -
-

桌台列表

- 添加桌台 - - - - - - - - - - - - {% for table in tables %} - - - - - - - - {% endfor %} - -
ID桌号容量收款策略操作
{{ table.table_id }}{{ table.game_table_number }}{{ table.capacity }}{{ table.strategy_name }} - 编辑 -
- -
-
-
+
+

桌台列表

+ 添加桌台 + + + + + + + + + + + + {% for table in tables %} + + + + + + + + {% endfor %} + +
ID桌号容量收款策略操作
{{ table.table_id }}{{ table.game_table_number }}{{ table.capacity }}{{ table.strategy_name }} + 编辑 + - -
+
+ + - - + + } + + async function selectStrategy(event) { + event.preventDefault(); + const tableId = document.getElementById('selectedTableId').value; + const strategyId = document.getElementById('strategySelect').value; + const token = "{{ session['token'] }}"; + + try { + const response = await fetch(`/admin/tables/${tableId}/strategy`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}` + }, + body: JSON.stringify({ + strategy_id: strategyId + }) + }); + + if (response.ok) { + const myModal = bootstrap.Modal.getInstance(document.getElementById('selectStrategyModal')); + myModal.hide(); + location.reload(); + } else { + const error = await response.json(); + alert(`更新失败: ${error.detail}`); + } + } catch (error) { + console.error('更新策略失败:', error); + } + } + {% endblock %}