fix(douyu): 修复查询工作台冷却筛选与 CSV 导出安全 (v0.4.27)

This commit is contained in:
yml
2026-11-30 17:23:49 +08:00
parent fdc01826a3
commit a3529c54a2
6 changed files with 111 additions and 58 deletions
+58 -35
View File
@@ -25,7 +25,7 @@ from fastapi import (
WebSocketDisconnect,
)
from loguru import logger
from sqlalchemy import case, func, or_, select
from sqlalchemy import and_, case, func, or_, select
from sqlalchemy.orm import Session, joinedload
from sqlalchemy.sql import Select
from starlette.concurrency import run_in_threadpool
@@ -672,6 +672,7 @@ def _apply_task_account_filters(
query = query.filter(
Account.cookie.isnot(None),
Account.cookie != "",
or_(Account.status.is_(None), Account.status != "invalid"),
or_(
Account.ck_check_status.is_(None),
Account.ck_check_status.notin_(("valid", "invalid")),
@@ -682,15 +683,27 @@ def _apply_task_account_filters(
query = query.filter(
Account.role_bind_status == BIND_BOUND,
or_(
Account.role_change_available_at.is_(None),
Account.role_change_available_at <= now,
and_(
Account.role_change_available_at.is_(None),
or_(
Account.change_role_wait_time.is_(None),
Account.change_role_wait_time <= 0,
),
),
),
)
elif ck_filter == "cooldown_waiting":
now = datetime.now(UTC)
query = query.filter(
Account.role_bind_status == BIND_BOUND,
Account.role_change_available_at > now,
or_(
Account.role_change_available_at > now,
and_(
Account.role_change_available_at.is_(None),
Account.change_role_wait_time > 0,
),
),
)
search_text = (search or "").strip()
if search_text:
@@ -1211,7 +1224,7 @@ _ACCOUNT_STATUS_LABELS = {
def _manual_budget_summary(db: Session) -> dict[str, Any]:
overview = get_login_quota_usage_summary(db)
overview = cast(dict[str, Any], get_login_quota_usage_summary(db))
manual_cat = next(
(
item
@@ -1281,6 +1294,15 @@ def _format_cooldown_duration(seconds: int) -> str:
return f"{minutes}分"
def _csv_safe_cell(value: Any) -> Any:
"""Prevent spreadsheet apps from evaluating exported text as a formula."""
if isinstance(value, str) and value.lstrip(" \t\r\n").startswith(
("=", "+", "-", "@")
):
return f"'{value}"
return value
@router.post("/query-workbench/lookup")
def lookup_query_workbench_accounts(
req: DouyuQueryWorkbenchLookupRequest,
@@ -1382,7 +1404,9 @@ def relogin_query_workbench_invalid_accounts(
current: User = Depends(require_permission("douyu:task")),
):
"""从查询工作台发起失效/无 CK 账号重新登录,强制走「人工登录」预算(manual_login)。"""
if current.role != "super_admin" and not user_has_permission(current, "login:batch"):
if current.role != "super_admin" and not user_has_permission(
current, "login:batch"
):
raise HTTPException(
status_code=403, detail="无权限发起重登(需超级管理员或 login:batch 权限)"
)
@@ -1533,9 +1557,7 @@ def export_query_workbench_csv(
)
rows_by_id = {int(acc.id): acc for acc in visible_accounts}
ordered_accounts = [
rows_by_id[aid] for aid in requested_ids if aid in rows_by_id
]
ordered_accounts = [rows_by_id[aid] for aid in requested_ids if aid in rows_by_id]
now = datetime.now(UTC)
output = io.StringIO()
writer = csv.writer(output)
@@ -1574,7 +1596,9 @@ def export_query_workbench_csv(
ck_label = "未检测"
bind_info = bind_payload(acc)
bind_status = str(bind_info.get("role_bind_status") or acc.role_bind_status or "")
bind_status = str(
bind_info.get("role_bind_status") or acc.role_bind_status or ""
)
bind_label = str(bind_info.get("role_bind_label") or "")
change_status = "未知"
@@ -1593,43 +1617,42 @@ def export_query_workbench_csv(
)
if aware > now:
change_status = "冷却中"
cooldown_note = (
f"剩余 {_format_cooldown_duration(int((aware - now).total_seconds()))}"
)
cooldown_note = f"剩余 {_format_cooldown_duration(int((aware - now).total_seconds()))}"
else:
change_status = "可换绑"
cooldown_note = "已过冷却"
elif (acc.change_role_wait_time or 0) > 0:
change_status = "冷却中"
cooldown_note = (
f"剩余 {_format_cooldown_duration(int(acc.change_role_wait_time or 0))}"
)
cooldown_note = f"剩余 {_format_cooldown_duration(int(acc.change_role_wait_time or 0))}"
else:
change_status = "可换绑"
cooldown_note = "可直接换绑"
writer.writerow(
[
acc.username or "",
acc.uid or "",
acc.nickname or "",
acc.tag or "",
acc.assigned_user.username if acc.assigned_user else "未分配",
_ACCOUNT_STATUS_LABELS.get(acc.status or "", acc.status or ""),
ck_label,
_format_shanghai_dt(acc.ck_checked_at),
acc.gold_balance if acc.gold_balance is not None else "",
acc.exchange_balance if acc.exchange_balance is not None else "",
acc.points if acc.points is not None else "",
_format_shanghai_dt(acc.points_expire_at),
bind_label,
acc.game_name or "",
acc.game_channel or "",
change_status,
_format_shanghai_dt(acc.role_change_available_at),
_format_shanghai_dt(acc.role_change_official_at),
cooldown_note,
_format_shanghai_dt(acc.updated_at or acc.overview_queried_at),
_csv_safe_cell(value)
for value in [
acc.username or "",
acc.uid or "",
acc.nickname or "",
acc.tag or "",
acc.assigned_user.username if acc.assigned_user else "未分配",
_ACCOUNT_STATUS_LABELS.get(acc.status or "", acc.status or ""),
ck_label,
_format_shanghai_dt(acc.ck_checked_at),
acc.gold_balance if acc.gold_balance is not None else "",
acc.exchange_balance if acc.exchange_balance is not None else "",
acc.points if acc.points is not None else "",
_format_shanghai_dt(acc.points_expire_at),
bind_label,
acc.game_name or "",
acc.game_channel or "",
change_status,
_format_shanghai_dt(acc.role_change_available_at),
_format_shanghai_dt(acc.role_change_official_at),
cooldown_note,
_format_shanghai_dt(acc.updated_at or acc.overview_queried_at),
]
]
)
filename = f"douyu_query_workbench_{datetime.now(_SHANGHAI_TZ).strftime('%Y%m%d_%H%M%S')}.csv"