- 回滚 QYOOB-only 门禁,扫到新角色即进入 role_ready - 客户页 readiness 与平台任务 role_ready 对齐,避免确认接口 409 - 绑定证据 baseline 不再冒充本次扫码 - 新增人工确认绑定接口与按钮(角色、备注、操作人事件) - 补充客户确认、证据口径和人工确认回归测试
296 lines
9.7 KiB
Python
296 lines
9.7 KiB
Python
"""履约绑定判定的只读证据。
|
||
|
||
把一个 DouyuTask 的结果拆成互不混淆的几层事实:
|
||
|
||
- ``actionCurrent``:动作 alias(QYOOB)上已经 bindAct=1 的角色;
|
||
- ``actionPending``:动作 alias 上 bindAct=0、可确认的本次待确认角色;
|
||
- ``legacyLatest``:legacy(cjm)上的最新角色,只能提示,不能单独确认;
|
||
- ``accountCurrentRole``:账号表当前生效角色(覆盖式,不代表本次);
|
||
- ``roleLock`` / ``accountLock``:7 天锁,是“能不能绑”的资格,不是角色识别。
|
||
|
||
这个模块只读,不改变订单状态。页面用它把“新扫的 / 旧的 / cjm 的 / 7 天锁的”
|
||
分开显示,避免再把它们混成一个“待确认角色”。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import datetime
|
||
from typing import Any
|
||
|
||
from sqlalchemy.orm import Session
|
||
|
||
from ...models import Account, DouyuTask, PlatformFulfillmentTask
|
||
from ..account_binding import pending_binding_role_from_result
|
||
from .role_lock import AccountLock, RoleLock, find_account_lock, find_role_lock
|
||
|
||
|
||
def _text(value: Any) -> str:
|
||
return str(value or "").strip()
|
||
|
||
|
||
def _bool(value: Any) -> bool:
|
||
return bool(value)
|
||
|
||
|
||
def _channel(area: str, plat: str) -> str:
|
||
return " / ".join(value for value in (area, plat) if value)
|
||
|
||
|
||
def _candidate(raw: dict[str, Any]) -> dict[str, Any]:
|
||
alias = _text(raw.get("act_alias") or raw.get("alias"))
|
||
area = _text(raw.get("area_name"))
|
||
plat = _text(raw.get("plat_name"))
|
||
return {
|
||
"alias": alias,
|
||
"roleName": _text(raw.get("role_name")),
|
||
"areaName": area,
|
||
"platName": plat,
|
||
"channel": _channel(area, plat),
|
||
"bindAct": _bool(raw.get("is_bound_act") or raw.get("bind_act")),
|
||
"bindRole": _bool(raw.get("is_bound_role") or raw.get("bind_role")),
|
||
"needBindAct": _bool(raw.get("need_bind_act")),
|
||
"actionableAlias": _bool(
|
||
raw.get("actionable_alias") or raw.get("actionableAlias")
|
||
),
|
||
"confirmable": _bool(raw.get("confirmable")),
|
||
"reason": _text(raw.get("reason")),
|
||
}
|
||
|
||
|
||
def _candidate_from_info(
|
||
info: Any,
|
||
action_aliases: list[str],
|
||
) -> dict[str, Any] | None:
|
||
if not isinstance(info, dict):
|
||
return None
|
||
raw = {
|
||
**info,
|
||
"act_alias": info.get("act_alias") or info.get("alias"),
|
||
}
|
||
candidate = _candidate(raw)
|
||
candidate["actionableAlias"] = candidate["alias"] in action_aliases
|
||
return candidate if candidate["roleName"] else None
|
||
|
||
|
||
def _lock_out(lock: RoleLock | AccountLock | None) -> dict[str, Any] | None:
|
||
if lock is None:
|
||
return None
|
||
data = {
|
||
"roleName": lock.role_name,
|
||
"accountId": lock.account_id,
|
||
"accountName": lock.account_name,
|
||
"boundAt": lock.bound_at.isoformat(),
|
||
"reusableAt": lock.reusable_at.isoformat(),
|
||
"message": lock.message(),
|
||
}
|
||
if isinstance(lock, RoleLock):
|
||
data["leasePurpose"] = lock.lease_purpose
|
||
data["leaseBatchId"] = lock.lease_batch_id
|
||
return data
|
||
|
||
|
||
def _account_current_candidate(
|
||
account: Account | None,
|
||
action_alias: str,
|
||
) -> dict[str, Any] | None:
|
||
if account is None or not _text(account.game_name):
|
||
return None
|
||
if str(account.role_bind_status or "") != "bound":
|
||
return None
|
||
channel = _text(account.game_channel)
|
||
area, _, plat = channel.partition(" · ")
|
||
return {
|
||
"alias": action_alias,
|
||
"roleName": _text(account.game_name),
|
||
"areaName": area,
|
||
"platName": plat,
|
||
"channel": channel,
|
||
"bindAct": True,
|
||
"bindRole": True,
|
||
"needBindAct": False,
|
||
"actionableAlias": bool(action_alias),
|
||
"confirmable": False,
|
||
"reason": "account_current_bound",
|
||
}
|
||
|
||
|
||
def build_binding_evidence(
|
||
db: Session,
|
||
task: PlatformFulfillmentTask,
|
||
bind_task: DouyuTask | None,
|
||
account: Account | None,
|
||
*,
|
||
now: datetime | None = None,
|
||
) -> dict[str, Any] | None:
|
||
"""构建页面用的绑定判定快照;无绑定任务时返回 None。"""
|
||
if bind_task is None:
|
||
return None
|
||
result = bind_task.result if isinstance(bind_task.result, dict) else {}
|
||
action_aliases = [
|
||
_text(value)
|
||
for value in (result.get("action_act_aliases") or [])
|
||
if _text(value)
|
||
]
|
||
candidates = [
|
||
_candidate(raw)
|
||
for raw in (result.get("bind_candidates") or [])
|
||
if isinstance(raw, dict)
|
||
]
|
||
candidates = [item for item in candidates if item["alias"]]
|
||
if not action_aliases:
|
||
action_aliases = [
|
||
item["alias"] for item in candidates if item["actionableAlias"]
|
||
]
|
||
selected_info = result.get("bind_info") or result.get("before_bind_info")
|
||
selected_info_candidate = _candidate_from_info(selected_info, action_aliases)
|
||
if not candidates and selected_info_candidate is not None:
|
||
candidates = [selected_info_candidate]
|
||
|
||
selected_alias = _text(
|
||
result.get("act_alias")
|
||
or (selected_info or {}).get("act_alias")
|
||
or (candidates[0]["alias"] if candidates else "")
|
||
)
|
||
decision = result.get("bind_decision")
|
||
decision = decision if isinstance(decision, dict) else {}
|
||
selected_actionable = bool(
|
||
decision.get("actionable_alias")
|
||
or (selected_alias and selected_alias in action_aliases)
|
||
)
|
||
|
||
action_current = next(
|
||
(
|
||
item
|
||
for item in candidates
|
||
if item["alias"] in action_aliases
|
||
and item["bindAct"]
|
||
and item["roleName"]
|
||
),
|
||
None,
|
||
)
|
||
if action_current is None:
|
||
action_current = _account_current_candidate(
|
||
account,
|
||
action_aliases[0] if action_aliases else "",
|
||
)
|
||
action_pending = next(
|
||
(
|
||
item
|
||
for item in candidates
|
||
if item["alias"] in action_aliases
|
||
and item["roleName"]
|
||
and not item["bindAct"]
|
||
),
|
||
None,
|
||
)
|
||
if (
|
||
action_pending is None
|
||
and selected_info_candidate is not None
|
||
and selected_info_candidate["alias"] in action_aliases
|
||
and selected_info_candidate["roleName"]
|
||
and not selected_info_candidate["bindAct"]
|
||
):
|
||
action_pending = selected_info_candidate
|
||
legacy_latest = next(
|
||
(
|
||
item
|
||
for item in candidates
|
||
if item["alias"] not in action_aliases and item["roleName"]
|
||
),
|
||
None,
|
||
)
|
||
if (
|
||
legacy_latest is None
|
||
and selected_info_candidate is not None
|
||
and selected_info_candidate["alias"] not in action_aliases
|
||
and selected_info_candidate["roleName"]
|
||
):
|
||
legacy_latest = selected_info_candidate
|
||
|
||
pending_role = pending_binding_role_from_result(result)
|
||
action_pending_role = (
|
||
_text((action_pending or {}).get("roleName")) if action_pending else ""
|
||
)
|
||
# scannedRole 只认“本次新扫码”:
|
||
# - 新 pending(QYOOB bindAct=0 / 可确认角色);
|
||
# - legacy(cjm) 上出现的扫码角色;
|
||
# - decision 明确可确认时的角色。
|
||
# 不能用 result.role_name / before_bind_info 的 baseline 兜底,否则会把
|
||
# 账号上一单的旧角色误报成本次扫码,详情就会和列表的“待扫码”冲突。
|
||
scanned_role = _text(
|
||
pending_role
|
||
or (legacy_latest or {}).get("roleName")
|
||
or action_pending_role
|
||
or (decision.get("role_name") if decision.get("confirmable") else "")
|
||
)
|
||
# 取消 QYOOB-only 门禁后,cjm 的新角色同样可以成为客户确认目标;
|
||
# 这里展示真实本次扫码角色,避免详情说“不可确认”但客户页可以确认。
|
||
target_role = scanned_role
|
||
baseline_role = _text(
|
||
result.get("baseline_role_name")
|
||
or (selected_info or {}).get("role_name")
|
||
or (result.get("before_bind_info") or {}).get("role_name")
|
||
)
|
||
account_current_role = _text(account.game_name) if account is not None else ""
|
||
same_pair = bool(
|
||
scanned_role
|
||
and account_current_role
|
||
and scanned_role == account_current_role
|
||
and action_current is not None
|
||
and action_current.get("bindAct")
|
||
and action_current.get("roleName") == scanned_role
|
||
)
|
||
bound = str(bind_task.status or "") == "success" and bool(
|
||
result.get("bind_confirmed")
|
||
)
|
||
confirmable = bool(
|
||
target_role
|
||
and str(bind_task.status or "") == "success"
|
||
and task.binding_status == "role_ready"
|
||
and not bound
|
||
)
|
||
role_lock = (
|
||
find_role_lock(
|
||
db,
|
||
scanned_role,
|
||
current_account_id=task.account_id,
|
||
now=now,
|
||
)
|
||
if scanned_role
|
||
else None
|
||
)
|
||
account_lock = find_account_lock(
|
||
db,
|
||
task.account_id,
|
||
scanned_role,
|
||
now=now,
|
||
)
|
||
return {
|
||
"sourceTaskId": int(bind_task.id),
|
||
"sourceTaskType": str(bind_task.task_type or ""),
|
||
"sourceTaskStatus": str(bind_task.status or ""),
|
||
"actionAliases": action_aliases,
|
||
"selectedAlias": selected_alias,
|
||
"selectedActionable": selected_actionable,
|
||
"decisionReason": _text(
|
||
decision.get("reason") or result.get("bind_phase")
|
||
),
|
||
"confirmable": confirmable,
|
||
"targetRole": target_role,
|
||
"scannedRole": scanned_role,
|
||
"baselineRole": baseline_role,
|
||
"samePair": same_pair,
|
||
"actionCurrent": action_current,
|
||
"actionPending": action_pending,
|
||
"legacyLatest": legacy_latest,
|
||
"accountCurrentRole": account_current_role,
|
||
"accountBindStatus": (
|
||
str(account.role_bind_status or "") if account is not None else ""
|
||
),
|
||
"roleLock": _lock_out(role_lock),
|
||
"accountLock": _lock_out(account_lock),
|
||
}
|
||
|
||
|
||
__all__ = ["build_binding_evidence"]
|