fix(fulfillment): 拆分绑定事实并复用同账号角色

- 详情页增加 QYOOB / cjm / 7 天锁绑定判定面板
- 订单目标只认动作 alias pending,cjm-only 不再推进订单
- 同账号同角色直接复用已有绑定,不再等待二维码或调 actBind
- 补账号侧 7 天锁本地检查
- 补充绑定证据、同对复用和 7 天锁回归测试
This commit is contained in:
yml
2026-11-24 13:14:50 +08:00
parent ad6c31729d
commit 5b6d146f93
10 changed files with 1221 additions and 27 deletions
@@ -51,7 +51,14 @@ from .repository import (
_load_task,
_load_task_for_update,
)
from .role_lock import ROLE_LOCK_ISSUE, RoleLock, find_role_lock
from .role_lock import (
ACCOUNT_LOCK_ISSUE,
ROLE_LOCK_ISSUE,
AccountLock,
RoleLock,
find_account_lock,
find_role_lock,
)
from .runtime import (
_append_event,
_apply_task_lease_policy,
@@ -239,10 +246,37 @@ def _lease_for_task(
return fulfillment_lease_for_task(db, task)
def _actionable_pending_role(
result: dict[str, Any] | None,
role_name: str,
) -> str:
"""只有动作 alias 上的 pending 才能作为订单目标。
cjm/legacy 上先出现的角色只能提示,不能直接确认;否则 actBind 会对
QYOOB 调一个未生效的 pending(11.22 抓包 -200001 的根因)。
"""
role = str(role_name or "").strip()
if not role or not isinstance(result, dict):
return ""
raw_aliases = result.get("action_act_aliases")
action_aliases = (
[str(value).strip() for value in raw_aliases if str(value).strip()]
if isinstance(raw_aliases, (list, tuple))
else []
)
selected_alias = str(result.get("act_alias") or "").strip()
if action_aliases and selected_alias and selected_alias not in action_aliases:
return ""
return role
def _pending_role_of(task: DouyuTask | None) -> str:
if task is None or not isinstance(task.result, dict):
return ""
return pending_binding_role_from_result(task.result)
return _actionable_pending_role(
task.result,
pending_binding_role_from_result(task.result),
)
def _owned_binding_tasks(
@@ -376,6 +410,55 @@ def _mark_role_locked(
return True
def _mark_account_locked(
db: Session,
task: PlatformFulfillmentTask,
source: DouyuTask | None,
lock: AccountLock,
) -> bool:
"""当前分配的账号 7 天内绑过别的角色:转人工,不调 actBind。"""
if str(task.issue_code or "") == ACCOUNT_LOCK_ISSUE:
return False
message = lock.message()
lease = _lease_for_task(db, task)
if source is not None and int(source.id) > int(task.bind_task_id or 0):
task.bind_task_id = int(source.id)
task.binding_status = "failed"
task.execution_status = "manual_required"
task.issue_code = ACCOUNT_LOCK_ISSUE
task.last_error = message
task.updated_at = _now()
detail = {
"accountRoleName": lock.role_name,
"accountBoundAt": lock.bound_at.isoformat(),
"accountReusableAt": lock.reusable_at.isoformat(),
"allocatedAccountId": task.account_id,
"allocatedAccountName": lock.account_name,
"message": message,
}
if source is not None:
detail["sourceTaskId"] = int(source.id)
detail["sourceTaskType"] = str(source.task_type or "")
_append_event(db, task.id, "binding_account_locked_elsewhere", detail=detail)
_apply_task_lease_policy(db, task, lease=lease)
_emit_order_site_binding_state(db, task.id)
return True
def _block_on_account_lock(
db: Session,
task: PlatformFulfillmentTask,
role_name: str,
source: DouyuTask | None = None,
) -> bool:
if str(task.issue_code or "") == ACCOUNT_LOCK_ISSUE:
return False
lock = find_account_lock(db, task.account_id, role_name)
if lock is None:
return False
return _mark_account_locked(db, task, source, lock)
def _block_on_role_lock(
db: Session,
task: PlatformFulfillmentTask,
@@ -427,6 +510,121 @@ def _apply_binding_qr_display(
return True
def _same_pair_reuse_role(
result: dict[str, Any] | None,
account: Account | None,
) -> str:
"""本次扫码角色已经绑在当前账号上:同一对,直接复用。
只认 **cjm/legacy 上出现的扫码角色**,并且要求动作 alias 上也是同一个
角色且 bindAct=1。动作 alias 上只要出现 bindAct=0 的 pending,就说明客户
扫的是新角色,必须走正常确认,不能复用旧角色。
"""
if not isinstance(result, dict) or account is None:
return ""
if str(account.role_bind_status or "") != "bound":
return ""
current = str(account.game_name or "").strip()
if not current:
return ""
raw_aliases = result.get("action_act_aliases")
action_aliases = (
[str(value).strip() for value in raw_aliases if str(value).strip()]
if isinstance(raw_aliases, (list, tuple))
else []
)
candidates = [
raw
for raw in (result.get("bind_candidates") or [])
if isinstance(raw, dict)
]
action_candidates = [
raw
for raw in candidates
if str(raw.get("act_alias") or raw.get("alias") or "").strip()
in action_aliases
]
if any(
str(raw.get("role_name") or "").strip()
and not bool(raw.get("is_bound_act"))
for raw in action_candidates
):
# 动作 alias 上已有新的 pending:客户扫了新角色,交给正常确认。
return ""
selected_alias = str(result.get("act_alias") or "").strip()
legacy_role = next(
(
str(raw.get("role_name") or "").strip()
for raw in candidates
if str(raw.get("act_alias") or raw.get("alias") or "").strip()
not in action_aliases
and str(raw.get("role_name") or "").strip()
),
"",
)
if selected_alias and selected_alias in action_aliases:
scanned_role = legacy_role
else:
scanned_role = str(
result.get("pending_role_name")
or result.get("role_name")
or legacy_role
or ""
).strip()
if not scanned_role or scanned_role != current:
return ""
if action_candidates:
action_bound = any(
str(raw.get("role_name") or "").strip() == current
and bool(raw.get("is_bound_act"))
for raw in action_candidates
)
return current if action_bound else ""
if (
(not action_aliases or selected_alias not in action_aliases)
and str(result.get("current_role_name") or "").strip() == current
):
return current
return ""
def _mark_reused_pair(
db: Session,
task: PlatformFulfillmentTask,
source: DouyuTask,
*,
role_name: str,
account: Account | None,
) -> bool:
lease = _lease_for_task(db, task)
task.bind_task_id = int(source.id)
task.binding_status = "bound"
task.execution_status = "bound"
task.issue_code = ""
task.last_error = ""
task.updated_at = _now()
_append_event(
db,
task.id,
"binding_reused_existing_pair",
detail={
"roleName": role_name[:128],
"accountId": task.account_id,
"accountName": (
(account.nickname or account.username)
if account is not None
else ""
),
"sourceTaskId": int(source.id),
"sourceTaskType": str(source.task_type or ""),
"reason": "same_account_same_role",
},
)
_apply_task_lease_policy(db, task, lease=lease)
_emit_order_site_binding_state(db, task.id)
return True
def _apply_binding_task(
db: Session,
task: PlatformFulfillmentTask,
@@ -441,8 +639,8 @@ def _apply_binding_task(
"""
if _frozen(task):
return False
if str(task.issue_code or "") == ROLE_LOCK_ISSUE:
# 角色 7 天锁只能由人工重置后重新进入流程。
if str(task.issue_code or "") in {ROLE_LOCK_ISSUE, ACCOUNT_LOCK_ISSUE}:
# 7 天锁只能由人工重置后重新进入流程。
return False
if not task.account_id or not source.account_id:
return False
@@ -471,6 +669,16 @@ def _apply_binding_task(
return False
account = db.get(Account, int(task.account_id))
if str(source.task_type or "") == "get_bind_qr" and phase != "confirming":
reused_role = _same_pair_reuse_role(source.result, account)
if reused_role:
return _mark_reused_pair(
db,
task,
source,
role_name=reused_role,
account=account,
)
binding = resolve_fulfillment_binding(
source,
account=account,
@@ -601,6 +809,9 @@ def _apply_binding_task(
role_name = binding.pending_role_name
if not role_name:
return False
if not _actionable_pending_role(source.result, role_name):
# cjm/legacy 先出现的新角色:订单继续保持等待动作通道。
return False
if phase == "confirming":
# 确认任务执行中,后到的扫码结果不能把订单打回待确认、丢掉确认任务指针。
return False
@@ -615,6 +826,8 @@ def _apply_binding_task(
return False
if _block_on_role_lock(db, task, role_name, source):
return True
if _block_on_account_lock(db, task, role_name, source):
return True
task.bind_task_id = int(source.id)
task.binding_status = "role_ready"
task.execution_status = "role_ready"
@@ -891,11 +1104,19 @@ def reconcile_binding_observations(
if not changed and task.binding_status == "role_ready":
# 部署前已经 role_ready 的订单:启动时补一次角色 7 天锁检查。
target_role_name = _order_target_role(db, task)
if target_role_name and _block_on_role_lock(
db,
task,
target_role_name,
current,
if target_role_name and (
_block_on_role_lock(
db,
task,
target_role_name,
current,
)
or _block_on_account_lock(
db,
task,
target_role_name,
current,
)
):
changed = True
if changed:
@@ -938,7 +1159,10 @@ def _latest_pending_binding_task(
for row in rows:
if not source_created_within_lease(lease, row):
continue
if resolve_douyu_binding(row, account=account).status == BINDING_VIEW_PENDING:
binding = resolve_douyu_binding(row, account=account)
if binding.status != BINDING_VIEW_PENDING:
continue
if _actionable_pending_role(row.result, binding.pending_role_name):
return row
return None
@@ -961,6 +1185,10 @@ def _attach_pending_binding_task(
or int(bind_task.account_id or 0) != int(task.account_id)
or bind_task.task_type not in PENDING_BINDING_TASK_TYPES
or binding.status != BINDING_VIEW_PENDING
or not _actionable_pending_role(
bind_task.result if isinstance(bind_task.result, dict) else {},
binding.pending_role_name,
)
or not lease_is_active(lease)
or not source_created_within_lease(lease, bind_task)
or (
@@ -978,6 +1206,8 @@ def _attach_pending_binding_task(
return False
if _block_on_role_lock(db, task, binding.pending_role_name, bind_task):
return True
if _block_on_account_lock(db, task, binding.pending_role_name, bind_task):
return True
previous_bind_task_id = task.bind_task_id
task.bind_task_id = int(bind_task.id)
task.binding_status = "role_ready"
@@ -1187,7 +1417,23 @@ def confirm_binding_sync(db: Session, *, task_id: int, current: User) -> dict[st
):
db.commit()
return get_task_detail(db, task.id)
if target_role_name and _block_on_account_lock(
db,
task,
target_role_name,
current_bind_task,
):
db.commit()
return get_task_detail(db, task.id)
expected_role_name = str(current_bind_result.get("role_name") or "").strip()
if expected_role_name and not _actionable_pending_role(
current_bind_result,
expected_role_name,
):
raise HTTPException(
status_code=409,
detail="当前动作通道没有待确认角色,请客户重新扫码后再确认",
)
baseline_role_name = str(current_bind_result.get("current_role_name") or "").strip()
batch_id = f"pfconfirm-{task.id}-{uuid.uuid4().hex[:8]}"
confirm_task = DouyuTask(