diff --git a/backend/app/routers/collections.py b/backend/app/routers/collections.py index 05f50cc..b2325e1 100644 --- a/backend/app/routers/collections.py +++ b/backend/app/routers/collections.py @@ -284,8 +284,11 @@ def get_stats( """获取藏品统计 - 使用数据库聚合查询优化性能""" from sqlalchemy import func, case - # 基础查询条件 - base_filter = True if current_user.role == "admin" else Collection.f99_91_user_id == current_user.f99_90_id + # 基础查询条件 - 检查用户是否登录 + if current_user is None or not hasattr(current_user, 'role'): + base_filter = False # 未登录用户不能查看任何藏品 + else: + base_filter = True if current_user.role == "admin" else Collection.f99_91_user_id == current_user.f99_90_id # 总数 total_count = db.query(func.count(Collection.f99_90_id)).filter(base_filter).scalar() or 0