fix(douyu): 登录后自动复检 CK 并收紧分配池

This commit is contained in:
yml
2026-11-20 20:46:55 +08:00
parent d42b4528c4
commit 00c30cc24e
9 changed files with 239 additions and 114 deletions
+30 -28
View File
@@ -87,15 +87,13 @@ def _visible_accounts_query(db: Session, current: User):
def _assignable_account_filter():
"""分配池口径:有 CK 且 CK **还没被判失效**,并且没被人工停用/归档。
- `ck_check_status` 为空 = 还没检测过(`derive_status` 同样按可用处理);
- `error` = 本轮查不到,不代表失效,照常留在池子里;
- `invalid` = 巡检已判失效,必须先重登/修复,分配给它也跑不了任何任务。
"""
"""分配池口径:当前 Cookie 必须有一次不早于它的有效 CK 检测。"""
return and_(
cookie_holding_filter(),
or_(Account.ck_check_status.is_(None), Account.ck_check_status != "invalid"),
Account.ck_check_status == "valid",
Account.ck_checked_at.isnot(None),
Account.cookie_updated_at.isnot(None),
Account.ck_checked_at >= Account.cookie_updated_at,
or_(
Account.status.is_(None),
Account.status.notin_([STATUS_DISABLED, STATUS_ARCHIVED]),
@@ -442,7 +440,7 @@ def list_accounts(
assigned_user_id: int | None = Query(None, description="只看某个客服名下的账号"),
assignable_only: bool = Query(
False,
description="只看可分配账号:持 CK 且 CK 未失效(排除已停用/归档)",
description="只看可分配账号:当前 Cookie 已通过新鲜 CK 检测(排除已停用/归档)",
),
tag: str = Query(None),
account_names: str = Query(
@@ -1025,17 +1023,20 @@ def assign_account(
if target.role != "support":
raise HTTPException(status_code=400, detail="只能分配给客服角色")
# 只能分配当前仍持有 CK 的账号(CK 现在挂在账号上)
# 只能分配当前 Cookie 已通过新鲜 CK 检测的账号。
has_success = (
db.query(Account)
.filter(
Account.id == account_id,
cookie_holding_filter(),
_assignable_account_filter(),
)
.first()
)
if not has_success:
raise HTTPException(status_code=400, detail="该账号尚未成功登录,无法分配")
raise HTTPException(
status_code=400,
detail="该账号尚未通过有效 CK 检测,无法分配",
)
if acc.assigned_to != req.assigned_to:
acc.assigned_to = req.assigned_to
@@ -1081,27 +1082,28 @@ def batch_assign_accounts(
if target.role != "support":
raise HTTPException(status_code=400, detail="只能分配给客服角色")
# 只能分配当前仍持有 CK 的账号(CK 现在挂在账号上)
cookie_ids_query = select(Account.id).where(cookie_holding_filter())
invalid_ids = (
db.query(Account.id)
.filter(
Account.id.in_(ids),
Account.id.notin_(cookie_ids_query),
)
.all()
)
if invalid_ids:
names = (
db.query(Account.username)
.filter(Account.id.in_([i[0] for i in invalid_ids]))
# 只能分配当前 Cookie 已通过新鲜 CK 检测的账号。
assignable_ids = {
int(account_id)
for (account_id,) in (
db.query(Account.id)
.filter(
Account.id.in_(ids),
_assignable_account_filter(),
)
.all()
)
}
invalid_ids = [
account_id for account_id in ids if account_id not in assignable_ids
]
if invalid_ids:
names = db.query(Account.username).filter(Account.id.in_(invalid_ids)).all()
name_list = ", ".join([n[0] for n in names[:5]])
suffix = "..." if len(invalid_ids) > 5 else ""
raise HTTPException(
status_code=400,
detail=f"以下账号尚未成功登录,无法分配:{name_list}{suffix}",
detail=f"以下账号尚未通过有效 CK 检测,无法分配:{name_list}{suffix}",
)
# 先取出账号对象(批量时间线要逐个写事件,光有 count 不够)。
@@ -1176,9 +1178,9 @@ def assignments_summary(
db: Session = Depends(get_db),
current: User = Depends(require_permission("account:assign")),
):
"""分配概览:每个客服分到了多少**可分配账号**(持 CK 且 CK 未失效)。
"""分配概览:每个客服分到了多少**可分配账号**(当前 Cookie 已通过新鲜 CK 检测)。
失效 CK 的账号必须先重登/修复,放进分配池只会让客服以为能跑任务,
未检测、检测失败或旧结论的账号必须先重登/复检,放进分配池只会让客服以为能跑任务,
所以列表、全量分配与这里的统计统一用 `_assignable_account_filter()`。
"""
results = (