fix(douyu): 优化和平小店绑定逻辑

This commit is contained in:
yml2213
2026-08-06 18:48:29 +08:00
parent 86459a1bd0
commit c05c0e044e
4 changed files with 2308 additions and 71 deletions
+64 -29
View File
@@ -1086,7 +1086,10 @@ class DouyuBatchRunner:
cookie: str,
config: dict,
):
"""生成和平小店绑定二维码并轮询等待微信扫码绑定/换绑完成。"""
"""生成和平小店绑定二维码并轮询等待微信扫码绑定/换绑完成。
识别到新角色后仅标记"待确认",不自动回写账号,由用户手动确认绑定。
"""
client = self._client(cookie)
act_alias = str(config.get("xpd_act_alias") or "").strip()
if not act_alias:
@@ -1114,19 +1117,15 @@ class DouyuBatchRunner:
"二维码已生成,请微信扫码在小程序中绑定角色",
result,
)
bound = self._wait_xpd_bind(db, task, account, client, act_alias, result)
if self._stop.is_set():
result["bind_polling"] = False
state = self._wait_xpd_bind(db, task, client, act_alias, result)
result["bind_polling"] = False
if state == "stopped":
self._mark_task(db, task, "stopped", "任务已停止", result)
return
if bound:
account.xpd_bind_status = "xpd_bound"
account.updated_at = datetime.now(timezone.utc)
db.commit()
role_text = str(result.get("role_name") or account.xpd_game_name or "-")
self._mark_task(db, task, "success", f"小店绑定成功: {role_text}", result)
if state == "pending":
role_text = str(result.get("role_name") or "-")
self._mark_task(db, task, "success", f"已识别角色: {role_text},待确认绑定", result)
return
result["bind_polling"] = False
self._mark_task(
db,
task,
@@ -1139,15 +1138,15 @@ class DouyuBatchRunner:
self,
db: Session,
task: DouyuTask,
account: Account,
client: DouyuActivityClient,
act_alias: str,
result: dict,
) -> bool:
"""轮询 bindInfo 等待绑定/换绑完成
) -> str:
"""轮询 bindInfo 检测绑定/换绑角色,识别到后停在"待确认",不自动回写账号
- 绑定前未绑定:检测到 bind_role=1 即绑定成功
- 绑定前已绑定(换绑):检测到角色名变化才算换绑成,角色不变继续等
- 绑定前未绑定:检测到 bind_role=1 即识别到待确认角色
- 绑定前已绑定(换绑):检测到角色名变化才算换绑成,角色不变继续等
返回 "pending"=已识别待确认角色, "stopped"=任务停止, "timeout"=超时未识别。
"""
before_bound = bool(result.get("before_bound"))
before_role_name = str(result.get("before_role_name") or "")
@@ -1165,11 +1164,8 @@ class DouyuBatchRunner:
if (not before_bound and bound_now and role_name) or changed:
result.update({key: value for key, value in info.items() if key != "raw"})
result["bind_polling"] = False
result["xpd_bound"] = True
account.xpd_game_name = role_name
account.updated_at = datetime.now(timezone.utc)
db.commit()
return True
result["xpd_pending_confirm"] = True
return "pending"
self._update_task_progress(
db,
task,
@@ -1191,7 +1187,48 @@ class DouyuBatchRunner:
if self._stop.wait(DOUYU_XPD_BIND_POLL_INTERVAL):
break
result["bind_polling"] = False
return False
return "stopped" if self._stop.is_set() else "timeout"
def _execute_confirm_xpd_bind(
self,
db: Session,
task: DouyuTask,
account: Account,
cookie: str,
config: dict,
):
"""确认和平小店绑定:回查 bindInfo,确认绑定角色后将账号回写为已绑定。"""
client = self._client(cookie)
act_alias = str(config.get("xpd_act_alias") or "").strip()
if not act_alias:
self._mark_task(db, task, "failed", "请先配置小店活动代号 actAlias")
return
result = client.xpd_bind_info(act_alias=act_alias)
role_name = str(result.get("role_name") or "")
if not result.get("bind_role") or not role_name:
account.xpd_bind_status = "xpd_not_bound"
account.updated_at = datetime.now(timezone.utc)
db.commit()
self._mark_task(db, task, "failed", "尚未检测到小店绑定角色,请先扫码绑定", result)
return
# 优先用完整角色信息回写(与查询角色一致),失败时回退 bindInfo 角色名
try:
ctx = self._xpd_role_context(client, config)
role = ctx["role"]
if role.get("role_id"):
self._apply_xpd_role_to_account(account, role, self._xpd_area_id(role, account))
else:
account.xpd_game_name = role_name
account.updated_at = datetime.now(timezone.utc)
except Exception:
account.xpd_game_name = role_name
account.updated_at = datetime.now(timezone.utc)
account.xpd_bind_status = "xpd_bound"
account.updated_at = datetime.now(timezone.utc)
db.commit()
result["xpd_pending_confirm"] = False
result["xpd_bound"] = True
self._mark_task(db, task, "success", f"小店绑定成功: {role_name}", result)
def _execute_query_xpd_bind_info(
self,
@@ -1201,19 +1238,16 @@ class DouyuBatchRunner:
cookie: str,
config: dict,
):
"""查询和平小店绑定信息(bindInfo)。"""
"""查询和平小店绑定信息(bindInfo)。
仅查询展示,不回写账号;确认绑定由 confirm_xpd_bind 任务完成。
"""
client = self._client(cookie)
act_alias = str(config.get("xpd_act_alias") or "").strip()
if not act_alias:
self._mark_task(db, task, "failed", "请先配置小店活动代号 actAlias")
return
result = client.xpd_bind_info(act_alias=act_alias)
role_name = str(result.get("role_name") or "")
if role_name:
account.xpd_game_name = role_name
account.xpd_bind_status = "xpd_bound" if result.get("bind_role") else "xpd_not_bound"
account.updated_at = datetime.now(timezone.utc)
db.commit()
status = "已绑定" if result.get("bind_role") else "未绑定"
text = str(result.get("role_name") or result.get("nick") or "-")
self._mark_task(
@@ -2593,6 +2627,7 @@ class DouyuBatchRunner:
"prefetch_csrf_token": self._execute_prefetch_csrf_token,
"get_xpd_bind_qr": self._execute_get_xpd_bind_qr,
"query_xpd_bind_info": self._execute_query_xpd_bind_info,
"confirm_xpd_bind": self._execute_confirm_xpd_bind,
"query_xpd_role": self._execute_query_xpd_role,
"refresh_xpd_goods": self._execute_refresh_xpd_goods,
"query_xpd_balance": self._execute_query_xpd_balance,