refactor(binding): 收口斗鱼角色状态判定
This commit is contained in:
@@ -24,6 +24,10 @@ from ...models import (
|
||||
PlatformFulfillmentTask,
|
||||
User,
|
||||
)
|
||||
from ..account_binding import (
|
||||
binding_role_from_result,
|
||||
pending_binding_role_from_result,
|
||||
)
|
||||
from ..douyu_account_state import DouyuLiveAccountState, refresh_douyu_account_state
|
||||
from ..douyu_activity_query import activity_client, query_exchange_eligibility
|
||||
from ..douyu_batch_launcher import start_douyu_batch, stop_douyu_batches
|
||||
@@ -514,39 +518,6 @@ def list_tasks(
|
||||
}
|
||||
|
||||
|
||||
def _pending_binding_role_name(result: dict[str, Any]) -> str:
|
||||
if bool(result.get("bind_confirmed")) or not bool(
|
||||
result.get("bind_ready_for_confirm")
|
||||
):
|
||||
return ""
|
||||
role_name = str(result.get("role_name") or "").strip()
|
||||
if not role_name:
|
||||
return ""
|
||||
baseline = str(
|
||||
result.get("baseline_role_name") or result.get("current_role_name") or ""
|
||||
).strip()
|
||||
if baseline and role_name.casefold() == baseline.casefold():
|
||||
# 同名角色仍可能是本次新绑定:活动状态必须出现从已绑定到
|
||||
# needBindAct 的待确认转换。仅凭 cjm 中残留的同名 bindRole 不算。
|
||||
transitioned_to_pending = bool(result.get("need_bind_act")) and not bool(
|
||||
result.get("is_bound_act")
|
||||
)
|
||||
if not transitioned_to_pending:
|
||||
return ""
|
||||
return role_name
|
||||
|
||||
|
||||
def _binding_role_out(result: dict[str, Any]) -> str:
|
||||
pending_role = _pending_binding_role_name(result)
|
||||
if pending_role:
|
||||
return pending_role
|
||||
if bool(result.get("bind_confirmed")):
|
||||
return str(
|
||||
result.get("role_name") or result.get("current_role_name") or ""
|
||||
).strip()
|
||||
return ""
|
||||
|
||||
|
||||
def _task_out(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
@@ -569,7 +540,8 @@ def _task_out(
|
||||
else None
|
||||
)
|
||||
bind_result = (bind_task.result or {}) if bind_task else {}
|
||||
pending_role = _pending_binding_role_name(bind_result)
|
||||
# 外层只消费共享绑定规则,不再自己比较角色名或 bindAct。
|
||||
pending_role = pending_binding_role_from_result(bind_result)
|
||||
return {
|
||||
"id": task.id,
|
||||
"sourceSystem": task.source_system,
|
||||
@@ -632,7 +604,7 @@ def _task_out(
|
||||
"status": bind_task.status,
|
||||
"message": bind_task.message,
|
||||
"qrUrl": str(bind_result.get("url") or "") if include_qr else "",
|
||||
"roleName": _binding_role_out(bind_result),
|
||||
"roleName": binding_role_from_result(bind_result),
|
||||
"areaName": str(bind_result.get("area_name") or ""),
|
||||
"platName": str(bind_result.get("plat_name") or ""),
|
||||
"bindPhase": str(bind_result.get("bind_phase") or ""),
|
||||
@@ -715,7 +687,7 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
||||
else None
|
||||
)
|
||||
bind_result = (bind_task.result or {}) if bind_task else {}
|
||||
pending_role = _pending_binding_role_name(bind_result)
|
||||
pending_role = pending_binding_role_from_result(bind_result)
|
||||
lease = (
|
||||
db.query(AccountLease).filter(AccountLease.id == task.lease_id).first()
|
||||
if task.lease_id
|
||||
@@ -742,7 +714,7 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
||||
"qrUrl": str(bind_result.get("url") or ""),
|
||||
"qrGeneratedAt": str(bind_result.get("qr_generated_at") or ""),
|
||||
"qrExpiresAt": str(bind_result.get("qr_expires_at") or ""),
|
||||
"roleName": _binding_role_out(bind_result),
|
||||
"roleName": binding_role_from_result(bind_result),
|
||||
"areaName": str(bind_result.get("area_name") or ""),
|
||||
"platName": str(bind_result.get("plat_name") or ""),
|
||||
"bindReadyForConfirm": (bool(pending_role) and not binding_failed),
|
||||
@@ -1245,7 +1217,7 @@ async def start_binding(
|
||||
existing = db.query(DouyuTask).filter(DouyuTask.id == task.bind_task_id).first()
|
||||
if existing:
|
||||
result = existing.result or {}
|
||||
pending_role = _pending_binding_role_name(result)
|
||||
pending_role = pending_binding_role_from_result(result)
|
||||
stale_pending = (
|
||||
bool(result.get("bind_ready_for_confirm")) and not pending_role
|
||||
)
|
||||
@@ -1329,7 +1301,7 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
||||
task.last_error,
|
||||
)
|
||||
result = bind_task.result or {}
|
||||
pending_role = _pending_binding_role_name(result)
|
||||
pending_role = pending_binding_role_from_result(result)
|
||||
if bind_task.status in {"failed", "error", "stopped"}:
|
||||
task.binding_status = "failed"
|
||||
task.execution_status = "manual_required"
|
||||
|
||||
Reference in New Issue
Block a user