Files
AI-Website/backend/app/routers/history.py
2026-01-09 09:48:57 +08:00

115 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app import models, database
router = APIRouter()
def get_db():
db = database.SessionLocal()
try:
yield db
finally:
db.close()
from app.utils_jwt import get_current_user
from typing import List
from app import schemas
# 根路径返回所有充值记录(管理员)
@router.get("/")
def root_history(db: Session = Depends(get_db), user=Depends(get_current_user)):
if not getattr(user, "is_admin", False):
raise HTTPException(status_code=403, detail="无权限")
try:
# 只获取充值记录,不显示消费记录
records = db.query(models.History).filter(models.History.type == "recharge").all()
result = []
for r in records:
# 获取用户名(如果存在)
username = ""
user_record = db.query(models.User).filter(models.User.id == r.user_id).first()
if user_record:
username = user_record.username
# 安全处理时间格式
timestamp_str = ""
try:
if hasattr(r, 'created_at') and r.created_at:
timestamp_str = r.created_at.strftime('%Y-%m-%d %H:%M:%S')
elif hasattr(r, 'timestamp') and r.timestamp:
timestamp_str = r.timestamp.strftime('%Y-%m-%d %H:%M:%S')
except:
timestamp_str = str(r.created_at or r.timestamp or "")
result.append({
"id": r.id,
"user_id": r.user_id,
"username": username,
"type": r.type,
"amount": r.amount,
"desc": r.desc,
"time": timestamp_str
})
return {"history": result}
except Exception as e:
print(f"Error in root_history: {str(e)}")
return {"history": [], "error": str(e)}
@router.get("/list")
def list_history(db: Session = Depends(get_db), user=Depends(get_current_user)):
records = db.query(models.History).filter(models.History.user_id == user.id).order_by(models.History.created_at.desc()).all()
return {"history": [
{
"type": r.type,
"amount": r.amount,
"desc": r.desc,
"time": r.created_at.strftime('%Y-%m-%d %H:%M:%S')
} for r in records
]}
# 管理员获取全部充值记录
@router.get("/all")
def list_all_history(db: Session = Depends(get_db), user=Depends(get_current_user)):
if not getattr(user, "is_admin", False):
raise HTTPException(status_code=403, detail="无权限")
try:
# 只获取充值记录,不显示消费记录
records = db.query(models.History).filter(models.History.type == "recharge").all()
result = []
for r in records:
# 获取用户名(如果存在)
username = ""
user_record = db.query(models.User).filter(models.User.id == r.user_id).first()
if user_record:
username = user_record.username
# 安全处理时间格式
timestamp_str = ""
try:
if hasattr(r, 'created_at') and r.created_at:
timestamp_str = r.created_at.strftime('%Y-%m-%d %H:%M:%S')
elif hasattr(r, 'timestamp') and r.timestamp:
timestamp_str = r.timestamp.strftime('%Y-%m-%d %H:%M:%S')
except:
timestamp_str = str(r.created_at or r.timestamp or "")
result.append({
"id": r.id,
"user_id": r.user_id,
"username": username,
"type": r.type,
"amount": r.amount,
"desc": r.desc,
"time": timestamp_str
})
return {"history": result}
except Exception as e:
# 记录错误但返回空列表避免500错误
print(f"Error in list_all_history: {str(e)}")
return {"history": [], "error": str(e)}