fix(douyu): 修正绑定状态与换绑冷却判定

This commit is contained in:
yml
2026-11-22 02:10:29 +08:00
parent 21956d952c
commit fa75ce66ee
9 changed files with 636 additions and 104 deletions
+82 -50
View File
@@ -285,6 +285,25 @@ def role_change_available_at(
return None
def is_platform_account_bind_cooldown(
error_code: Any = None,
message: str = "",
) -> bool:
"""是否为斗鱼平台账号自身的换绑冷却。
2026-11-22 换绑失败 HAR 中 ``actBind`` 返回 ``-200001``:
“该平台账号未到可换绑时间,请使用其他平台账号”。它限制的是当前斗鱼号,
不是用户扫码选择的腾讯游戏角色;提示和人工处理都必须区分两者。
"""
try:
normalized_code = int(error_code)
except (TypeError, ValueError):
normalized_code = None
return normalized_code == -200001 or "该平台账号未到可换绑时间" in (
message or ""
)
def binding_pending_decision(
info: dict | None,
*,
@@ -295,9 +314,15 @@ def binding_pending_decision(
) -> dict[str, Any]:
"""解释一个绑定候选是否可进入确认阶段。
角色名不是唯一事实,也不能要求每次都必须换新角色。用户重新绑定同名角色
时,必须观察到二维码生成后的状态变化。``needBindAct=1`` 在本活动里是
持续存在的“仍允许绑定”标志,不能单独证明存在新的扫码结果。
2026-11-22 两份 HAR 证明必须把两类状态分开:
- ``bindAct=0 + roleName``:角色已选中但未最终生效,是可确认 pending。
它可以在生成新二维码前就存在,官网仍允许直接 ``actBind``;
- ``bindAct=1 + needBindAct=1``:最终绑定已经生效。``needBindAct`` 是长期
存在的“仍允许以后换绑”标志,不能当成新的扫码结果。
因此 ``bindAct=1`` 永远不是 pending;``bindAct=0`` 的动作 alias 即使没有
观察到本轮状态迁移,也仍然可以确认。角色名相同不等于已经绑定。
"""
result: dict[str, Any] = {
"alias": str((info or {}).get("act_alias") or ""),
@@ -339,60 +364,67 @@ def binding_pending_decision(
if not role_name:
result["reason"] = "no_role"
return result
baseline = baseline_role_name.strip()
if not baseline:
# 2026-11-22:没有 baseline 无法证明这是本轮扫码产生的新角色。
# 历史库中 bound + needBindAct 会长期并存,不能再次冒充 pending。
result["reason"] = (
"already_bound"
if snapshot["is_bound_act"]
else "pending_without_baseline"
)
if snapshot["is_bound_act"]:
# 2026-11-22 换绑失败 HAR:历史角色会长期返回 bindAct=1 + needBindAct=1。
# 这表示当前绑定仍生效,不是待确认;若官方在扫码后直接完成绑定,轮询层
# 会通过 directly_confirmed 收敛,不需要把它伪装成 pending。
result["reason"] = "already_bound"
return result
if role_name.casefold() != baseline.casefold():
baseline = baseline_role_name.strip()
if baseline and role_name.casefold() != baseline.casefold():
result["role_changed"] = True
result.update({"confirmable": True, "reason": "role_changed"})
return result
if not isinstance(baseline_info, dict):
if snapshot["is_bound_act"]:
result["reason"] = "already_bound"
baseline_snapshot: dict[str, Any] | None = None
if isinstance(baseline_info, dict):
baseline_snapshot = binding_snapshot(baseline_info)
result["baseline_need_bind_act"] = bool(
baseline_snapshot["need_bind_act"]
)
result["baseline_is_pending"] = bool(
baseline_snapshot["role_name"]
and not baseline_snapshot["is_bound_act"]
and baseline_snapshot["need_bind_act"]
)
if result["same_alias"] and baseline_snapshot["is_bound_act"]:
result.update(
{
"confirmable": True,
"reason": "same_alias_after_bound_transition",
}
)
return result
result["reason"] = "same_role_without_baseline_info"
if (
result["same_alias"]
and not baseline_snapshot["need_bind_act"]
and snapshot["need_bind_act"]
):
result.update(
{
"confirmable": True,
"reason": "same_alias_after_pending_transition",
}
)
return result
# 2026-11-22 绑定成功 HAR:官网允许确认二维码生成前就已经存在的
# bindAct=0 角色。这里不能再要求“本轮必须发生状态变化”,否则合法 pending
# 会一直轮询到超时。关键安全边界是上面的 bindAct=1 已提前返回。
if result["actionable_alias"] and snapshot["need_bind_act"]:
result.update(
{
"confirmable": True,
"reason": "existing_actionable_pending",
}
)
return result
baseline_snapshot = binding_snapshot(baseline_info)
result["baseline_need_bind_act"] = bool(
baseline_snapshot["need_bind_act"]
)
result["baseline_is_pending"] = bool(
baseline_snapshot["role_name"]
and not baseline_snapshot["is_bound_act"]
and baseline_snapshot["need_bind_act"]
)
if result["same_alias"] and (
baseline_snapshot["is_bound_act"] and not snapshot["is_bound_act"]
):
result.update(
{
"confirmable": True,
"reason": "same_alias_after_bound_transition",
}
)
if not baseline:
result["reason"] = "pending_without_baseline"
return result
if (
result["same_alias"]
and not snapshot["is_bound_act"]
and snapshot["need_bind_act"]
and not baseline_snapshot["need_bind_act"]
):
result.update(
{
"confirmable": True,
"reason": "same_alias_after_pending_transition",
}
)
return result
if snapshot["is_bound_act"]:
result["reason"] = "already_bound"
if baseline_snapshot is None:
result["reason"] = "same_role_without_baseline_info"
return result
result["reason"] = (
"same_role_stale_pending"
+97 -8
View File
@@ -23,6 +23,7 @@ from .account_binding import (
binding_snapshot,
is_bound_act,
is_pending_binding_role,
is_platform_account_bind_cooldown,
is_role_change_cooling,
role_change_available_at,
set_bind_status,
@@ -1323,6 +1324,9 @@ class BindMixin:
),
"bind_phase": "change_waiting",
"bind_polling": False,
# 2026-11-22 换绑失败 HAR:canChangeTime 限制的是当前斗鱼账号,
# 不是客户扫码后出现的腾讯游戏角色。
"issue_code": "account_role_cooldown",
}
self._apply_bind_info_to_account(
account,
@@ -1333,7 +1337,7 @@ class BindMixin:
db,
task,
"failed",
f"{role_label} 暂不能换绑,剩余 {wait_text}",
f"斗鱼账号当前角色({role_label})未到可换绑时间,剩余 {wait_text}",
result,
)
return
@@ -1386,9 +1390,52 @@ class BindMixin:
)
),
}
# 2026-11-22:二维码生成前的 pending 只能作为 baseline,不能直接
# 推进 role_ready。官方活动 alias 会长期保留 needBindAct=1,必须在
# 二维码生成后观察到绑定状态变化,才能证明这是本轮扫码结果。
# 2026-11-22 绑定成功 HAR:二维码生成前已经存在的 bindAct=0 角色
# 是官网认可的 pending,可以直接确认,不要求本轮再次出现状态迁移。
# 历史 bindAct=1 角色已被上面的 pending 判定排除,不会再误报。
if pending_decision["confirmable"]:
pending_snapshot = self._bind_snapshot(pending_before)
pending_role = str(pending_snapshot["role_name"] or "")
pending_alias = str(pending_before.get("act_alias") or "")
pending_summary = (
f"act={pending_alias or '-'} "
f"{self._format_bind_summary(pending_before, pending=True)}"
)
result.update(
{
**pending_snapshot,
"act_alias": pending_alias,
"query_act_alias": pending_alias,
"pending_role_name": pending_role,
"bind_ready_for_confirm": True,
"bind_confirmed": False,
"bind_phase": "role_ready",
"bind_polling": False,
"bind_summary": pending_summary,
"bind_decision": pending_decision,
"bind_candidate_decisions": candidate_decisions,
"action_act_aliases": action_aliases,
"poll_count": 0,
}
)
account.updated_at = qr_generated_at
self._push_log(
"success",
"生成二维码时发现已有可确认角色 "
f"act={pending_alias or '-'} role={pending_role or '-'} "
f"reason={pending_decision['reason']}",
)
self._mark_task(
db,
task,
"success",
f"已识别现有待确认角色: {pending_role},待确认绑定",
result,
)
return
# 没有可确认 pending 时只发布二维码并交给持久化轮询。尤其不能在
# bindAct=1 + needBindAct=1 时提前进入 role_ready。
account.updated_at = datetime.now(UTC)
task.queue_lane = "binding_poll"
task.next_run_at = qr_generated_at + timedelta(
@@ -1558,25 +1605,67 @@ class BindMixin:
confirm_result = client.confirm_bind(use_confirm_alias)
except DouyuActivityError as exc:
confirm_msg = str(exc)
confirm_code = getattr(exc, "code", None)
confirm_payload = getattr(exc, "payload", None)
self._push_log("warning", f"确认绑定接口失败: {confirm_msg}")
# 待绑定游戏账号侧换绑限制(未到换绑时间等)导致确认失败:保留原绑定并给出明确提示
if isinstance(confirm_payload, dict) and confirm_payload:
record_role_api_response(
db,
account_id=account.id,
task_id=task.id,
response=confirm_payload,
scope=SCOPE_ELITE,
stage="confirm_bind.error",
source="douyu_task.confirm_bind",
act_alias=use_confirm_alias,
)
account_cooldown = is_platform_account_bind_cooldown(
confirm_code,
confirm_msg,
)
before_bound_snapshot = self._bind_snapshot(before_bound)
before_bound_role = str(before_bound_snapshot["role_name"] or "")
before_bound_wait = before_bound_snapshot["change_role_wait_time"]
before_bound_wait_text = self._format_wait_time(before_bound_wait)
if account_cooldown:
issue_code = "account_role_cooldown"
role_label = before_bound_role or role_name or "当前角色"
suffix = f",剩余 {before_bound_wait_text}" if before_bound_wait_text else ""
failure_message = (
f"斗鱼账号当前绑定角色({role_label})未到可换绑时间"
f"{suffix},请更换斗鱼账号或稍后重试"
)
else:
issue_code = "customer_role_cooldown"
failure_message = (
f"待绑定游戏账号({role_name or '-'})未到换绑时间"
"(不是斗鱼/虎牙账号),请重新换账号扫码绑定"
)
# 2026-11-22 换绑失败 HAR:actBind 已拒绝时新角色只存在于 cjm 候选态,
# 活动 alias 仍是原绑定。必须保留旧角色,不能让失败角色污染账号库。
if before_bound is not None:
self._apply_bind_info_to_account(account, before_bound)
self._apply_bind_info_to_account(account, before_bound, BIND_BOUND)
else:
account.updated_at = datetime.now(UTC)
self._mark_task(
db,
task,
"failed",
f"待绑定游戏账号({role_name or '-'})未到换绑时间(不是斗鱼/虎牙账号),请重新换账号扫码绑定",
failure_message,
{
"act_alias": use_confirm_alias,
"query_act_alias": query_alias,
"query_act_aliases": query_aliases,
"before_bind_info": before,
"before_bound_bind_info": before_bound,
"before_bound_snapshot": (
before_bound_snapshot if before_bound is not None else None
),
**before_snapshot,
"confirm_error": confirm_msg,
"bind_ready_for_confirm": True,
"confirm_error_code": confirm_code,
"issue_code": issue_code,
"bind_ready_for_confirm": False,
"bind_confirmed": False,
"bind_phase": "confirm_failed",
},
@@ -31,6 +31,7 @@ from ...models import (
from ...permissions import ROLE_LABELS
from ..account_binding import (
binding_role_from_result,
is_platform_account_bind_cooldown,
pending_binding_role_from_result,
)
from ..account_capability_service import (
@@ -1410,7 +1411,7 @@ def _customer_task_message(task: PlatformFulfillmentTask) -> str:
"points_short": "账号正在补充积分,请稍后重试",
"commodity_limit_reached": "当前商品暂时无法兑换,请联系客服",
"binding_failed": "暂未识别到绑定角色,请继续使用当前二维码完成扫码",
"account_role_cooldown": "当前绑定账号暂不可换绑,请截图本页面并联系客服处理",
"account_role_cooldown": "当前斗鱼账号未到换绑时间,请截图本页面并联系客服处理",
"customer_role_cooldown": "该游戏角色仍在换绑冷却期,请截图本页面并联系客服处理",
"eligibility_check_failed": "状态查询异常,请稍后刷新",
"manual_required": "当前需要人工处理,请联系客服",
@@ -1419,9 +1420,20 @@ def _customer_task_message(task: PlatformFulfillmentTask) -> str:
def _binding_failure_issue(bind_task: DouyuTask, result: dict[str, Any]) -> str:
explicit_issue = str(result.get("issue_code") or "")
if explicit_issue in {"account_role_cooldown", "customer_role_cooldown"}:
return explicit_issue
phase = str(result.get("bind_phase") or "")
message = str(bind_task.message or "")
if phase == "change_waiting" or "暂不能换绑" in message:
confirm_error = str(result.get("confirm_error") or "")
if (
phase == "change_waiting"
or is_platform_account_bind_cooldown(
result.get("confirm_error_code"),
f"{message} {confirm_error}",
)
or "暂不能换绑" in message
):
task_issue = "account_role_cooldown"
elif (
phase == "confirm_failed"
@@ -1432,7 +1444,7 @@ def _binding_failure_issue(bind_task: DouyuTask, result: dict[str, Any]) -> str:
else:
return "binding_failed"
messages = {
"account_role_cooldown": "当前绑定账号暂不可换绑,请截图本页面并联系客服处理",
"account_role_cooldown": "当前斗鱼账号未到换绑时间,请截图本页面并联系客服处理",
"customer_role_cooldown": "该游戏角色仍在换绑冷却期,请截图本页面并联系客服处理",
}
return task_issue if messages.get(task_issue) else "binding_failed"
@@ -1454,6 +1466,8 @@ def _customer_binding_queue_state(
return "allocation_queued", 2000
if not lease_ready:
return "lease_inactive", 0
if task.binding_status == "failed":
return "failed", 0
if bind_task is None:
return "ready_to_start", 0
result = bind_task.result or {}