优化 ui
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
"""Cookie 管理路由"""
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.orm import Session
|
||||
import io
|
||||
import csv
|
||||
|
||||
from ..database import get_db
|
||||
from ..models import User, LoginTask, Account
|
||||
from ..deps import get_current_user, require_permission
|
||||
from ..permissions import has_permission
|
||||
|
||||
router = APIRouter(prefix="/api/cookies", tags=["Cookie管理"])
|
||||
|
||||
|
||||
@router.get("")
|
||||
def list_cookies(
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""查看登录成功的 Cookie 列表。"""
|
||||
query = db.query(LoginTask).filter(LoginTask.status == "success")
|
||||
|
||||
# 客服只能看自己账号的
|
||||
if not has_permission(current.role, "login:view_all"):
|
||||
query = query.join(Account, LoginTask.account_id == Account.id).filter(
|
||||
Account.assigned_to == current.id
|
||||
)
|
||||
|
||||
tasks = query.order_by(LoginTask.finished_at.desc()).all()
|
||||
result = []
|
||||
for t in tasks:
|
||||
acc = db.query(Account).filter(Account.id == t.account_id).first()
|
||||
item = {
|
||||
"id": t.id,
|
||||
"batch_id": t.batch_id,
|
||||
"account_id": t.account_id,
|
||||
"account_username": acc.username if acc else "",
|
||||
"created_at": t.finished_at.isoformat() if t.finished_at else None,
|
||||
}
|
||||
# 只有有 cookie:view 权限才返回 cookie 内容
|
||||
if has_permission(current.role, "cookie:view"):
|
||||
cookie = t.cookie or ""
|
||||
item["cookie"] = cookie
|
||||
item["cookie_preview"] = cookie[:50] + "..." if len(cookie) > 50 else cookie
|
||||
else:
|
||||
item["cookie"] = ""
|
||||
item["cookie_preview"] = "***"
|
||||
result.append(item)
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/export")
|
||||
def export_cookies(
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("cookie:export")),
|
||||
):
|
||||
"""导出 Cookie 为 CSV。"""
|
||||
tasks = db.query(LoginTask).filter(
|
||||
LoginTask.status == "success"
|
||||
).order_by(LoginTask.finished_at.desc()).all()
|
||||
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output)
|
||||
writer.writerow(["账号", "Cookie", "时间"])
|
||||
|
||||
for t in tasks:
|
||||
acc = db.query(Account).filter(Account.id == t.account_id).first()
|
||||
username = acc.username if acc else ""
|
||||
writer.writerow([username, t.cookie or "", t.finished_at.isoformat() if t.finished_at else ""])
|
||||
|
||||
output.seek(0)
|
||||
return StreamingResponse(
|
||||
iter([output.getvalue()]),
|
||||
media_type="text/csv",
|
||||
headers={"Content-Disposition": "attachment; filename=cookies.csv"},
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/{task_id}")
|
||||
def delete_cookie(
|
||||
task_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("cookie:export")),
|
||||
):
|
||||
"""删除一条 Cookie 记录。"""
|
||||
task = db.query(LoginTask).filter(LoginTask.id == task_id).first()
|
||||
if not task:
|
||||
raise HTTPException(status_code=404, detail="记录不存在")
|
||||
task.cookie = ""
|
||||
task.status = "failed"
|
||||
task.message = "Cookie已清除"
|
||||
db.commit()
|
||||
return {"message": "已删除", "success": True}
|
||||
Reference in New Issue
Block a user