274 lines
9.1 KiB
Python
274 lines
9.1 KiB
Python
from flask import Blueprint, render_template, request, redirect, url_for, flash, session
|
|
import requests
|
|
from frontend.config import Config
|
|
|
|
games_bp = Blueprint('games', __name__)
|
|
|
|
|
|
@games_bp.route('/games/')
|
|
def list_games():
|
|
if not session.get('token'):
|
|
return redirect(url_for('auth.login'))
|
|
|
|
# 仅发送必要参数
|
|
resp = requests.post(
|
|
f"{Config.BASE_API_URL}/admin/games",
|
|
json={"token": session['token']} # 匹配后端的请求模型
|
|
)
|
|
|
|
if resp.status_code != 200:
|
|
flash("获取游戏列表失败", "danger")
|
|
return redirect(url_for('dashboard.index'))
|
|
|
|
return render_template('games/list.html', games=resp.json())
|
|
|
|
|
|
|
|
@games_bp.route('/games/add', methods=['GET', 'POST'])
|
|
def add_game():
|
|
if not session.get('token'):
|
|
return redirect(url_for('auth.login'))
|
|
|
|
if request.method == 'POST':
|
|
payload = {
|
|
"game_name": request.form['name'],
|
|
"game_type": 0 if request.form['type'] == 'script' else 1,
|
|
"description": request.form['desc'],
|
|
"min_players": int(request.form['min_players']),
|
|
"max_players": int(request.form['max_players']),
|
|
"duration": request.form['duration'],
|
|
"price": float(request.form['price']),
|
|
"difficulty_level": int(request.form['difficulty']),
|
|
"is_available": int(request.form['is_available']),
|
|
"quantity": int(request.form['quantity']),
|
|
"token": session['token'],
|
|
"long_description": request.form['long_desc'],
|
|
}
|
|
# 添加游戏使用 admin 接口
|
|
resp = requests.post(
|
|
f"{Config.BASE_API_URL}/admin/games/create",
|
|
json=payload,
|
|
)
|
|
print(payload)
|
|
if resp.status_code == 200:
|
|
flash("添加成功", "success")
|
|
return redirect(url_for('games.list_games'))
|
|
else:
|
|
flash("添加失败", "danger")
|
|
return render_template('games/add.html')
|
|
|
|
|
|
@games_bp.route('/games/delete/<int:game_id>', methods=['POST'])
|
|
def delete_game(game_id):
|
|
if not session.get('token'):
|
|
return redirect(url_for('auth.login'))
|
|
|
|
resp = requests.delete(
|
|
f"{Config.BASE_API_URL}/admin/games/{game_id}",
|
|
headers={"Authorization": f"Bearer {session['token']}"}
|
|
)
|
|
if resp.status_code == 200:
|
|
flash("删除成功", "success")
|
|
else:
|
|
flash("删除失败,因为该游戏产生过订单,可以进行下架操作", "danger")
|
|
return redirect(url_for('games.list_games'))
|
|
|
|
@games_bp.route('/games/edit/<int:game_id>', methods=['GET', 'POST'])
|
|
def edit_game(game_id):
|
|
if not session.get('token'):
|
|
return redirect(url_for('auth.login'))
|
|
|
|
if request.method == 'POST':
|
|
# 更新游戏逻辑
|
|
payload = {
|
|
"game_name": request.form['name'],
|
|
"game_type": 0 if request.form['type'] == 'script' else 1,
|
|
"description": request.form['desc'],
|
|
"min_players": int(request.form['min_players']),
|
|
"max_players": int(request.form['max_players']),
|
|
"duration": request.form['duration'],
|
|
"price": float(request.form['price']),
|
|
"difficulty_level": int(request.form['difficulty']),
|
|
"is_available": 1 if 'available' in request.form else 0,
|
|
"quantity": int(request.form['quantity']),
|
|
"long_description": request.form['long_desc'],
|
|
}
|
|
|
|
resp = requests.put(
|
|
f"{Config.BASE_API_URL}/admin/games/{game_id}",
|
|
json=payload,
|
|
headers={"Authorization": f"Bearer {session['token']}"}
|
|
)
|
|
# print(resp.json())
|
|
if resp.status_code == 200:
|
|
flash("游戏更新成功", "success")
|
|
return redirect(url_for('games.list_games'))
|
|
else:
|
|
flash("更新失败,请检查数据", "danger")
|
|
return redirect(url_for('games.edit_game', game_id=game_id))
|
|
|
|
# GET请求处理
|
|
try:
|
|
resp = requests.get(
|
|
f"{Config.BASE_API_URL}/admin/games/{game_id}",
|
|
headers={"Authorization": f"Bearer {session['token']}"}
|
|
)
|
|
resp.raise_for_status()
|
|
return render_template('games/edit.html', game=resp.json())
|
|
except requests.exceptions.HTTPError as e:
|
|
flash("游戏不存在或无权访问", "danger")
|
|
except Exception as e:
|
|
flash("获取游戏信息失败", "danger")
|
|
return redirect(url_for('games.list_games'))
|
|
|
|
@games_bp.route('/games/photos/<int:game_id>', methods=['POST'])
|
|
def manage_photos(game_id):
|
|
if not session.get('token'):
|
|
return redirect(url_for('auth.login'))
|
|
if request.method == 'POST':
|
|
# 处理图片上传逻辑
|
|
photo_url: str = request.form['photo_url']
|
|
resp = requests.post(
|
|
f"{Config.BASE_API_URL}/admin/games/set_photo",
|
|
json={"game_id": game_id, "photo_url": photo_url, "token": session['token']}
|
|
)
|
|
if resp.status_code == 200:
|
|
flash("图片url设置成功", "success")
|
|
else:
|
|
flash("图片设置失败", "danger")
|
|
return redirect(url_for('games.list_games', game_id=game_id))
|
|
|
|
|
|
@games_bp.route('/games/tags', methods=['GET', 'POST'])
|
|
def manage_tags():
|
|
if not session.get('token'):
|
|
return redirect(url_for('auth.login'))
|
|
|
|
# 获取所有游戏数据 (保持原GET请求)
|
|
|
|
|
|
games_resp = requests.post(
|
|
f"{Config.BASE_API_URL}/admin/games",
|
|
json={"token": session['token']} # 匹配后端的请求模型
|
|
)
|
|
all_games = games_resp.json() if games_resp.status_code == 200 else []
|
|
# 处理标签创建请求
|
|
if request.method == 'POST':
|
|
# 创建新标签
|
|
resp = requests.post(
|
|
f"{Config.BASE_API_URL}/admin/tags",
|
|
json={
|
|
"tag_name": request.form['tag_name'],
|
|
"token": session['token']
|
|
}
|
|
)
|
|
if resp.status_code == 200:
|
|
flash("标签创建成功", "success")
|
|
else:
|
|
flash(resp.json().get('detail', '创建失败'), "danger")
|
|
return redirect(url_for('games.manage_tags'))
|
|
|
|
# 获取所有标签 (改为新的POST请求)
|
|
tags_resp = requests.post(
|
|
f"{Config.BASE_API_URL}/admin/get_tags",
|
|
json={"token": session['token']}
|
|
)
|
|
|
|
# 错误处理
|
|
if tags_resp.status_code != 200:
|
|
flash("获取标签列表失败", "danger")
|
|
return redirect(url_for('games.manage_tags'))
|
|
|
|
# 获取游戏标签关联数据 (示例调用)
|
|
game_tags_data = []
|
|
if tags_resp.status_code == 200:
|
|
for tag in tags_resp.json():
|
|
# 调用新的接口获取每个标签关联的游戏
|
|
games_resp = requests.post(
|
|
f"{Config.BASE_API_URL}/admin/get_games_by_tag",
|
|
json={"tag_id": tag['tag_id'], "token": session['token']}
|
|
)
|
|
if games_resp.status_code == 200:
|
|
game_tags_data.append({
|
|
"tag_id": tag['tag_id'],
|
|
"games": games_resp.json()
|
|
})
|
|
|
|
return render_template('games/tags.html',
|
|
tags=tags_resp.json(),
|
|
all_games=all_games,
|
|
game_tags=game_tags_data)
|
|
|
|
@games_bp.route('/games/link_tags', methods=['POST'])
|
|
def link_tags():
|
|
"""批量关联标签"""
|
|
data = {
|
|
"tag_id": request.form['tag_id'],
|
|
"game_ids": list(map(int, request.form.getlist('game_ids'))),
|
|
"token": session['token']
|
|
}
|
|
resp = requests.post(
|
|
f"{Config.BASE_API_URL}/admin/tags/link_games",
|
|
json=data
|
|
)
|
|
if resp.status_code == 200:
|
|
flash(f"成功关联 {resp.json()['linked_count']} 个游戏", "success")
|
|
else:
|
|
flash("关联失败", "danger")
|
|
return redirect(url_for('games.manage_tags'))
|
|
|
|
|
|
@games_bp.route('/games/get_game_tags', methods=['POST'])
|
|
def proxy_get_game_tags():
|
|
"""代理获取标签关联游戏的请求"""
|
|
if not session.get('token'):
|
|
return {'error': 'Unauthorized'}, 401
|
|
|
|
data = {
|
|
"tag_id": request.json.get('tag_id'),
|
|
"token": session['token']
|
|
}
|
|
|
|
resp = requests.post(
|
|
f"{Config.BASE_API_URL}/admin/get_games_by_tag",
|
|
json=data
|
|
)
|
|
return resp.json(), resp.status_code
|
|
|
|
|
|
@games_bp.route('/games/unlink_game', methods=['POST'])
|
|
def proxy_unlink_game():
|
|
"""代理取消关联请求"""
|
|
if not session.get('token'):
|
|
return {'error': 'Unauthorized'}, 401
|
|
|
|
data = {
|
|
"tag_id": request.json.get('tag_id'),
|
|
"game_id": request.json.get('game_id'),
|
|
"token": session['token']
|
|
}
|
|
|
|
print(data)
|
|
resp = requests.post(
|
|
f"{Config.BASE_API_URL}/admin/tags/unlink",
|
|
json=data
|
|
)
|
|
return {'status': 'success'}, 200
|
|
|
|
|
|
@games_bp.route('/games/delete_tag', methods=['POST'])
|
|
def delete_tag():
|
|
"""处理标签删除请求"""
|
|
if not session.get('token'):
|
|
return {'error': 'Unauthorized'}, 401
|
|
|
|
data = {
|
|
"tag_id": request.json.get('tag_id'),
|
|
"token": session['token']
|
|
}
|
|
|
|
resp = requests.post(
|
|
f"{Config.BASE_API_URL}/admin/tags/delete",
|
|
json=data
|
|
)
|
|
return resp.json(), resp.status_code |