优化列表分页和数据加载
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
"""Cookie 管理路由"""
|
||||
|
||||
from datetime import timezone
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.orm import Session, defer, joinedload
|
||||
import io
|
||||
import csv
|
||||
|
||||
@@ -38,17 +39,52 @@ def _visible_cookie_tasks_query(db: Session, current: User):
|
||||
|
||||
@router.get("")
|
||||
def list_cookies(
|
||||
search: str = Query(""),
|
||||
page: int | None = Query(None, ge=1),
|
||||
page_size: int = Query(20, ge=1, le=200),
|
||||
include_cookie: bool = Query(True),
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""查看登录成功的 Cookie 列表。"""
|
||||
tasks = _visible_cookie_tasks_query(db, current).order_by(LoginTask.finished_at.desc()).all()
|
||||
if not include_cookie:
|
||||
query = _visible_cookie_tasks_query(db, current).options(defer(LoginTask.cookie))
|
||||
else:
|
||||
query = _visible_cookie_tasks_query(db, current).options(
|
||||
joinedload(LoginTask.account).joinedload(Account.assigned_user),
|
||||
)
|
||||
search_text = (search or "").strip()
|
||||
if search_text:
|
||||
pattern = f"%{search_text}%"
|
||||
if user_has_permission(current, "login:view_all"):
|
||||
query = query.join(Account, LoginTask.account_id == Account.id)
|
||||
query = query.outerjoin(User, Account.assigned_to == User.id).filter(or_(
|
||||
Account.username.ilike(pattern),
|
||||
User.username.ilike(pattern),
|
||||
))
|
||||
|
||||
total = None
|
||||
if page is not None:
|
||||
total = query.order_by(None).count()
|
||||
query = query.order_by(LoginTask.finished_at.desc())
|
||||
if page is not None:
|
||||
query = query.offset((page - 1) * page_size).limit(page_size)
|
||||
|
||||
tasks = query.all()
|
||||
|
||||
# 批量查账号,避免 N+1 查询
|
||||
account_ids = [t.account_id for t in tasks]
|
||||
accounts_map = {}
|
||||
if account_ids:
|
||||
accs = db.query(Account).filter(Account.id.in_(account_ids)).all()
|
||||
account_query = db.query(Account).filter(Account.id.in_(account_ids))
|
||||
if not include_cookie:
|
||||
account_query = account_query.options(
|
||||
joinedload(Account.assigned_user),
|
||||
defer(Account.password),
|
||||
defer(Account.email),
|
||||
defer(Account.email_password),
|
||||
)
|
||||
accs = account_query.all()
|
||||
accounts_map = {a.id: a for a in accs}
|
||||
|
||||
result = []
|
||||
@@ -63,8 +99,8 @@ def list_cookies(
|
||||
"assigned_username": acc.assigned_user.username if acc and acc.assigned_user else None,
|
||||
"created_at": _fmt_dt(t.finished_at),
|
||||
}
|
||||
# 只有有 cookie:view 权限才返回 cookie 和密码(用于自定义格式复制)
|
||||
if user_has_permission(current, "cookie:view"):
|
||||
# 分页列表默认只返回预览,复制/导出时再获取完整敏感字段。
|
||||
if include_cookie and user_has_permission(current, "cookie:view"):
|
||||
cookie = t.cookie or ""
|
||||
item["cookie"] = cookie
|
||||
item["cookie_preview"] = cookie[:50] + "..." if len(cookie) > 50 else cookie
|
||||
@@ -74,9 +110,60 @@ def list_cookies(
|
||||
item["cookie_preview"] = "***"
|
||||
item["account_password"] = ""
|
||||
result.append(item)
|
||||
if page is not None:
|
||||
return {"items": result, "total": total or 0, "page": page, "page_size": page_size}
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/summary")
|
||||
def cookies_summary(
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""Cookie 管理统计,避免前端为了卡片统计拉全量 Cookie。"""
|
||||
query = _visible_cookie_tasks_query(db, current)
|
||||
if user_has_permission(current, "login:view_all"):
|
||||
query = query.join(Account, LoginTask.account_id == Account.id)
|
||||
total = query.count()
|
||||
assigned_count = query.filter(Account.assigned_to.isnot(None)).count()
|
||||
return {
|
||||
"total": total,
|
||||
"assigned_count": assigned_count,
|
||||
"unassigned_count": max(0, total - assigned_count),
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{task_id}")
|
||||
def get_cookie(
|
||||
task_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""获取单条 Cookie 详情,供复制操作按需读取完整敏感字段。"""
|
||||
task = _visible_cookie_tasks_query(db, current).filter(LoginTask.id == task_id).first()
|
||||
if not task:
|
||||
raise HTTPException(status_code=404, detail="记录不存在")
|
||||
acc = db.query(Account).filter(Account.id == task.account_id).first()
|
||||
item = {
|
||||
"id": task.id,
|
||||
"batch_id": task.batch_id,
|
||||
"account_id": task.account_id,
|
||||
"account_username": acc.username if acc else "",
|
||||
"assigned_to": acc.assigned_to if acc else None,
|
||||
"assigned_username": acc.assigned_user.username if acc and acc.assigned_user else None,
|
||||
"created_at": _fmt_dt(task.finished_at),
|
||||
"cookie": "",
|
||||
"cookie_preview": "***",
|
||||
"account_password": "",
|
||||
}
|
||||
if user_has_permission(current, "cookie:view"):
|
||||
cookie = task.cookie or ""
|
||||
item["cookie"] = cookie
|
||||
item["cookie_preview"] = cookie[:50] + "..." if len(cookie) > 50 else cookie
|
||||
item["account_password"] = acc.password if acc else ""
|
||||
return item
|
||||
|
||||
|
||||
@router.get("/export")
|
||||
def export_cookies(
|
||||
format: str = "csv",
|
||||
|
||||
Reference in New Issue
Block a user