166 lines
5.6 KiB
Python
166 lines
5.6 KiB
Python
"""斗鱼绑定任务的唯一读取模型。
|
|
|
|
``DouyuTask.result`` 是任务级绑定事实的来源:
|
|
|
|
- 本次扫码识别出的待确认角色只属于这个任务;
|
|
- ``bind_confirmed`` 表示这个任务是否已经把角色确认生效;
|
|
- ``accounts.game_name`` / ``role_bind_status`` 只表示账号当前状态,
|
|
不能拿来判断某个履约订单是否已经确认。
|
|
|
|
精英宝典、履约详情、客户页和履约刷新都必须消费这里的结果。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import asdict, dataclass
|
|
from typing import Any
|
|
|
|
from ..models import Account, DouyuTask
|
|
from .account_binding import (
|
|
binding_channel,
|
|
get_bind_status,
|
|
pending_binding_role_from_result,
|
|
)
|
|
|
|
BINDING_VIEW_NONE = "none"
|
|
BINDING_VIEW_WAITING_SCAN = "waiting_scan"
|
|
BINDING_VIEW_PENDING = "pending"
|
|
BINDING_VIEW_CONFIRMED = "confirmed"
|
|
BINDING_VIEW_FAILED = "failed"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class DouyuBindingView:
|
|
"""供所有绑定入口共享的稳定视图。"""
|
|
|
|
source_task_id: int | None
|
|
source_task_type: str
|
|
source_task_status: str
|
|
status: str
|
|
bind_phase: str
|
|
pending_role_name: str
|
|
pending_area_name: str
|
|
pending_plat_name: str
|
|
pending_channel: str
|
|
confirmed_role_name: str
|
|
confirmed_area_name: str
|
|
confirmed_plat_name: str
|
|
confirmed_channel: str
|
|
effective_role_name: str
|
|
effective_area_name: str
|
|
effective_plat_name: str
|
|
effective_channel: str
|
|
current_role_name: str
|
|
current_area_name: str
|
|
current_plat_name: str
|
|
current_channel: str
|
|
qr_url: str
|
|
reported_ready_for_confirm: bool
|
|
bind_confirmed: bool
|
|
bind_ready_for_confirm: bool
|
|
account_bind_status: str
|
|
|
|
def as_dict(self) -> dict[str, Any]:
|
|
return asdict(self)
|
|
|
|
|
|
def resolve_douyu_binding(
|
|
task: DouyuTask | None,
|
|
*,
|
|
account: Account | None = None,
|
|
) -> DouyuBindingView:
|
|
"""从任务结果和账号当前状态生成唯一绑定视图。"""
|
|
result = task.result if task is not None and isinstance(task.result, dict) else {}
|
|
source_status = str(task.status or "") if task is not None else ""
|
|
successful_task = source_status == "success"
|
|
pending_role = pending_binding_role_from_result(result)
|
|
bind_confirmed = successful_task and bool(result.get("bind_confirmed"))
|
|
if pending_role:
|
|
bind_confirmed = False
|
|
|
|
pending_area = str(result.get("area_name") or "").strip() if pending_role else ""
|
|
pending_plat = str(result.get("plat_name") or "").strip() if pending_role else ""
|
|
pending_channel = binding_channel(
|
|
{
|
|
"area_name": pending_area,
|
|
"plat_name": pending_plat,
|
|
}
|
|
)
|
|
|
|
confirmed_role = (
|
|
str(result.get("role_name") or result.get("current_role_name") or "").strip()
|
|
if bind_confirmed
|
|
else ""
|
|
)
|
|
confirmed_area = str(result.get("area_name") or "").strip() if bind_confirmed else ""
|
|
confirmed_plat = str(result.get("plat_name") or "").strip() if bind_confirmed else ""
|
|
confirmed_channel = binding_channel(
|
|
{
|
|
"area_name": confirmed_area,
|
|
"plat_name": confirmed_plat,
|
|
}
|
|
)
|
|
|
|
account_bind_status = get_bind_status(account, "elite") if account else ""
|
|
current_role = str(account.game_name or "").strip() if account else ""
|
|
current_area = str(result.get("current_area_name") or "").strip()
|
|
current_plat = str(result.get("current_plat_name") or "").strip()
|
|
current_channel = str(account.game_channel or "").strip() if account else ""
|
|
if not current_role:
|
|
current_role = str(
|
|
result.get("current_role_name") or result.get("baseline_role_name") or ""
|
|
).strip()
|
|
if not current_channel:
|
|
current_channel = binding_channel(
|
|
{
|
|
"area_name": result.get("current_area_name"),
|
|
"plat_name": result.get("current_plat_name"),
|
|
}
|
|
)
|
|
|
|
effective_role = pending_role or confirmed_role
|
|
effective_area = pending_area or confirmed_area
|
|
effective_plat = pending_plat or confirmed_plat
|
|
effective_channel = pending_channel or confirmed_channel
|
|
qr_url = str(result.get("url") or "")
|
|
|
|
if source_status in {"failed", "error", "stopped"}:
|
|
status = BINDING_VIEW_FAILED
|
|
elif bind_confirmed:
|
|
status = BINDING_VIEW_CONFIRMED
|
|
elif pending_role:
|
|
status = BINDING_VIEW_PENDING
|
|
elif qr_url:
|
|
status = BINDING_VIEW_WAITING_SCAN
|
|
else:
|
|
status = BINDING_VIEW_NONE
|
|
|
|
return DouyuBindingView(
|
|
source_task_id=int(task.id) if task is not None else None,
|
|
source_task_type=str(task.task_type or "") if task is not None else "",
|
|
source_task_status=source_status,
|
|
status=status,
|
|
bind_phase=str(result.get("bind_phase") or ""),
|
|
pending_role_name=pending_role,
|
|
pending_area_name=pending_area,
|
|
pending_plat_name=pending_plat,
|
|
pending_channel=pending_channel,
|
|
confirmed_role_name=confirmed_role,
|
|
confirmed_area_name=confirmed_area,
|
|
confirmed_plat_name=confirmed_plat,
|
|
confirmed_channel=confirmed_channel,
|
|
effective_role_name=effective_role,
|
|
effective_area_name=effective_area,
|
|
effective_plat_name=effective_plat,
|
|
effective_channel=effective_channel,
|
|
current_role_name=current_role,
|
|
current_area_name=current_area,
|
|
current_plat_name=current_plat,
|
|
current_channel=current_channel,
|
|
qr_url=qr_url,
|
|
reported_ready_for_confirm=bool(result.get("bind_ready_for_confirm")),
|
|
bind_confirmed=status == BINDING_VIEW_CONFIRMED,
|
|
bind_ready_for_confirm=status == BINDING_VIEW_PENDING,
|
|
account_bind_status=account_bind_status,
|
|
)
|