feat(douyu): unify account probes and lifecycle writes

Centralize Douyu client creation, CK session probing, identity refresh, role alias resolution, capability persistence, cache and batch throttling across collaboration, fulfillment, tasks and cookie management. Add the account-name copy action in the collaboration table.
This commit is contained in:
yml
2026-11-20 01:55:25 +08:00
parent e9937419cb
commit 1a49bcd481
24 changed files with 2162 additions and 639 deletions
+73 -30
View File
@@ -33,16 +33,27 @@ from sqlalchemy.orm import Session
from core.douyu import DouyuActivityClient
from ..models import Account, DouyuTask
from .account_binding import binding_snapshot, set_bind_status
from .account_capability_service import sync_account_goods_states
from .business_events import record_official_exchange
from .douyu_account_probe import (
ROLE_BOUND,
ROLE_ERROR,
ROLE_PENDING,
ROLE_UNBOUND,
ROLE_UNKNOWN,
persist_probe_result,
query_elite_bind_candidates,
resolve_elite_role,
resolve_esports_role,
resolve_peace_role,
)
from .douyu_activity_query import (
query_activity_goods,
query_bind_info,
query_csrf,
query_esports_bind_info,
query_exchange_records,
query_xpd_bind_info,
query_xpd_embed,
)
from .douyu_runner_donate import _parse_expire_at, _points_expire_at
from .high_value_service import (
@@ -199,7 +210,10 @@ class OverviewMixin:
) -> dict[str, Any]:
"""按作用域查绑定信息(三个接口不同,收敛在一处)。"""
if scope == "elite":
return query_bind_info(client, act_alias=alias)
rows, errors = query_elite_bind_candidates(client, [alias])
if errors:
raise RuntimeError(";".join(errors))
return rows[0] if rows else {}
if scope == "esports":
return query_esports_bind_info(client, act_alias=alias)
return query_xpd_bind_info(client, act_alias=alias)
@@ -238,7 +252,8 @@ class OverviewMixin:
esports_manual_id: str,
) -> list[dict[str, Any]]:
"""和平小店:先要一次性短时授权,再查购买记录。"""
embed = client.xpd_embed_query(
embed = query_xpd_embed(
client,
act_alias=str(config["xpd_act_alias"]),
rid=str(config["xpd_rid"]),
)
@@ -383,6 +398,11 @@ class OverviewMixin:
)
overview["bind"] = {}
role_resolvers = {
"elite": resolve_elite_role,
"esports": resolve_esports_role,
"peace": resolve_peace_role,
}
for scope, label, alias_key in (
("elite", "精英宝典", "bind_act_alias"),
("esports", "电竞手册", "esports_act_alias"),
@@ -394,32 +414,55 @@ class OverviewMixin:
if not alias:
errors[f"bind_{scope}"] = f"未配置{label}活动别名"
continue
try:
info = self._fetch_bind_info(client, scope, alias)
snapshot = binding_snapshot(info)
derived = snapshot["bind_status"]
# 把**明细**也带上:运营要看的是"绑的哪个角色、哪个区服、
# 什么时候能换绑",不是一个"已绑定"就完事。
overview["bind"][scope] = {
"derived": derived,
"phase": snapshot["bind_phase"],
"role_name": snapshot["role_name"],
"area_name": snapshot["area_name"],
"plat_name": snapshot["plat_name"],
"nickname": snapshot["nickname"],
"game_id": snapshot["game_id"],
"game_name": snapshot["game_name"],
"is_change_cooling": snapshot["is_change_cooling"],
"can_change_role": snapshot["can_change_role"],
"change_role_wait_time": _ts(snapshot["change_role_wait_time"]),
"can_change_time": _ts(snapshot["can_change_time"]),
}
if derived is not None:
# 唯一写入者;推不出来(None)时**不动原值** —— 宁可留 unknown,
# 也不要把"没查到"写成"没绑定"
set_bind_status(account, scope, derived)
except Exception as exc: # noqa: BLE001 外部接口边界需要宽泛兜底
errors[f"bind_{scope}"] = _err(exc)
role = role_resolvers[scope](
client,
config,
bind_fetcher=lambda selected_alias, selected_scope=scope: (
self._fetch_bind_info(client, selected_scope, selected_alias)
),
)
status = str(role.get("status") or "")
snapshot = (
role.get("snapshot") if isinstance(role.get("snapshot"), dict) else {}
)
overview["bind"][scope] = {
"status": status,
"derived": snapshot.get("bind_status"),
"phase": snapshot.get("bind_phase"),
"role_name": snapshot.get("role_name") or "",
"area_name": snapshot.get("area_name") or "",
"plat_name": snapshot.get("plat_name") or "",
"nickname": snapshot.get("nickname") or "",
"game_id": snapshot.get("game_id") or "",
"game_name": snapshot.get("game_name") or "",
"source_alias": role.get("source_alias") or "",
"is_change_cooling": snapshot.get("is_change_cooling"),
"can_change_role": snapshot.get("can_change_role"),
"change_role_wait_time": _ts(snapshot.get("change_role_wait_time")),
"can_change_time": _ts(snapshot.get("can_change_time")),
}
if status in {ROLE_ERROR, ROLE_UNKNOWN} and role.get("errors"):
errors[f"bind_{scope}"] = ";".join(role.get("errors") or [])
if status == ROLE_ERROR:
continue
if status in {ROLE_BOUND, ROLE_PENDING, ROLE_UNBOUND}:
persist_probe_result(
db,
account,
{
"probe_id": f"task:{task.id}:role:{scope}",
"identity": {"uid": account.uid, "username": account.username},
"session": {},
"elite": {},
"eligibility": {},
"roles": {scope: role},
"gold": {},
"goods": {},
"errors": {},
},
source=f"douyu_task.query_account_overview.{scope}",
record_event=False,
)
# ---- 官方兑换记录(**三个作用域都拉**)----
#