type: 收窄虎牙绑定与充值执行器类型

This commit is contained in:
yml2213
2026-08-30 19:52:49 +08:00
parent 6a16d561d2
commit 0efd84a281
2 changed files with 277 additions and 121 deletions
+130 -52
View File
@@ -3,6 +3,7 @@
from __future__ import annotations
import time
from datetime import datetime, timezone
from typing import Any
from sqlalchemy.orm import Session
@@ -18,10 +19,27 @@ HUYA_BIND_ROLE_POLL_SECONDS = 180
HUYA_BIND_ROLE_POLL_INTERVAL = 3
HUYA_BIND_ZT_UUID = "b02faae1"
HUYA_BIND_ROOM_ID = "30596253"
from .huya_runner_core import HUYA_RECHARGE_SOURCE_ID
from .huya_runner_core import HUYA_RECHARGE_SOURCE_ID, HuyaBatchRunnerCore
class BindMixin:
"""游戏角色绑定域:绑定状态机、扫码/确认绑定。"""
if TYPE_CHECKING:
_stop: Any
payload: dict
@staticmethod
def _format_local_time(timestamp: int) -> str: ...
@staticmethod
def _to_int(value: Any) -> int: ...
def _resolve_uid(self, account_info: dict) -> int: ...
def _push_log(self, level: str, message: str) -> None: ...
def _mark_task(self, *args: Any, **kwargs: Any) -> None: ...
def _update_task_progress(self, *args: Any, **kwargs: Any) -> None: ...
@staticmethod
def _role_name(bind_status) -> str:
account_data = bind_status.accountData
@@ -29,7 +47,7 @@ class BindMixin:
@staticmethod
def _has_bind_role(bind_status) -> bool:
return bool(bind_status and HuyaBatchRunner._role_name(bind_status))
return bool(bind_status and BindMixin._role_name(bind_status))
@staticmethod
def _bind_role_result(bind_status) -> dict:
@@ -38,7 +56,7 @@ class BindMixin:
game_role = account_data.gameRole
return {
"game_title": bind_status.gameName,
"role_name": HuyaBatchRunner._role_name(bind_status),
"role_name": BindMixin._role_name(bind_status),
"change_bind_day": bind_status.changeBindDay,
"is_bind_account": account_data.isBindAcount,
"is_bind_role": account_data.isBindRole,
@@ -54,8 +72,8 @@ class BindMixin:
parts = [game_role.platName, game_role.areaName]
return " / ".join(part for part in parts if part)
@classmethod
def _bind_change_state(cls, bind_status) -> dict:
@staticmethod
def _bind_change_state(bind_status) -> dict:
account_data = bind_status.accountData
is_bound = bool(account_data.isBindAcount and account_data.isBindRole)
change_time = int(account_data.changBindTime or 0)
@@ -65,16 +83,16 @@ class BindMixin:
"is_bound": is_bound,
"can_change_bind": can_change,
"change_bind_time": change_time,
"change_available_at": cls._format_local_time(change_time),
"change_available_at": HuyaBatchRunnerCore._format_local_time(change_time),
"change_bind_day": int(bind_status.changeBindDay or 0),
}
@classmethod
def _bind_ready_result(cls, bind_status) -> dict:
role_info = cls._bind_role_result(bind_status)
@staticmethod
def _bind_ready_result(bind_status) -> dict:
role_info = BindMixin._bind_role_result(bind_status)
return {
**role_info,
**cls._bind_change_state(bind_status),
**BindMixin._bind_change_state(bind_status),
"bind_status": bind_status.to_dict(),
"bind_ready_for_confirm": bool(role_info["role_name"]),
"bind_phase": "role_ready" if role_info["role_name"] else "waiting_role",
@@ -82,7 +100,7 @@ class BindMixin:
def _resolve_bind_status(
self,
client: HuyaHttpClient,
client: Any,
uid: int,
cookie: str,
b_act_id_int: int,
@@ -106,9 +124,10 @@ class BindMixin:
chosen_status = outer_status
account_data = outer_status.accountData
should_check_inner = (
not self._has_bind_role(outer_status)
and bool(account_data.isNeedActCheck or not account_data.isBindAcount or not account_data.isBindRole)
should_check_inner = not self._has_bind_role(outer_status) and bool(
account_data.isNeedActCheck
or not account_data.isBindAcount
or not account_data.isBindRole
)
if should_check_inner:
inner_status = client.check_user_bind_game_account(
@@ -162,22 +181,28 @@ class BindMixin:
qrcode_token = str(result.get("qrcode_token") or "")
qrcode_finished = not qrcode_token
result["bind_polling"] = True
self._update_task_progress(worker_db, task, "running", "已生成绑定小程序码,等待扫码绑定", result)
self._update_task_progress(
worker_db, task, "running", "已生成绑定小程序码,等待扫码绑定", result
)
while not self._stop.is_set() and time.monotonic() < deadline:
if self._stop.wait(HUYA_BIND_ROLE_POLL_INTERVAL):
break
if qrcode_token and not qrcode_finished:
qrcode_status = client.get_livelink_qrcode_status(qrcode_token, timeout=10.0)
qrcode_status = client.get_livelink_qrcode_status(
qrcode_token, timeout=10.0
)
if qrcode_status is not None:
result["qrcode_status"] = qrcode_status
if qrcode_status["is_expired"] or qrcode_status["is_failure"]:
result.update({
"bind_phase": "qrcode_expired",
"bind_ready_for_confirm": False,
"bind_polling": False,
})
result.update(
{
"bind_phase": "qrcode_expired",
"bind_ready_for_confirm": False,
"bind_polling": False,
}
)
self._update_task_progress(
worker_db,
task,
@@ -205,10 +230,12 @@ class BindMixin:
result.update(bind_query_result)
if bind_status.status != 200:
result.update({
"bind_phase": "role_check_failed",
"bind_status": bind_status.to_dict(),
})
result.update(
{
"bind_phase": "role_check_failed",
"bind_status": bind_status.to_dict(),
}
)
self._update_task_progress(
worker_db,
task,
@@ -250,11 +277,13 @@ class BindMixin:
message = "已生成绑定小程序码,等待扫码绑定"
self._update_task_progress(worker_db, task, "running", message, result)
result.update({
"bind_phase": "role_timeout" if not self._stop.is_set() else "stopped",
"bind_ready_for_confirm": False,
"bind_polling": False,
})
result.update(
{
"bind_phase": "role_timeout" if not self._stop.is_set() else "stopped",
"bind_ready_for_confirm": False,
"bind_polling": False,
}
)
return "", result
def _execute_get_bind_qr(
@@ -265,14 +294,18 @@ class BindMixin:
account_info: dict,
config_info: dict,
):
b_act_id = str(self.payload.get("bind_act_id") or config_info.get("bind_act_id") or "").strip()
b_act_id = str(
self.payload.get("bind_act_id") or config_info.get("bind_act_id") or ""
).strip()
if not b_act_id:
self._mark_task(worker_db, task, "failed", "请先配置虎牙绑定 bActId")
return
b_act_id_int = self._to_int(b_act_id)
if not b_act_id_int:
self._mark_task(worker_db, task, "failed", f"虎牙绑定 bActId 无效: {b_act_id}")
self._mark_task(
worker_db, task, "failed", f"虎牙绑定 bActId 无效: {b_act_id}"
)
return
uid = self._resolve_uid(account_info)
@@ -285,7 +318,9 @@ class BindMixin:
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
return
client = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
client: Any = HuyaHttpClient(
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
)
bind_status, bind_query_result = self._resolve_bind_status(
client=client,
uid=uid,
@@ -308,7 +343,9 @@ class BindMixin:
role_info = self._bind_role_result(bind_status)
change_state = self._bind_change_state(bind_status)
if role_info["role_name"]:
self._apply_role_to_account(account, bind_status, account.status or "imported")
self._apply_role_to_account(
account, bind_status, account.status or "imported"
)
if not change_state["can_change_bind"]:
role_name = role_info["role_name"] or "当前角色"
available_at = change_state["change_available_at"]
@@ -346,9 +383,13 @@ class BindMixin:
)
return
profile_nick = account_info.get("nickname") or account_info.get("username") or ""
profile_nick = (
account_info.get("nickname") or account_info.get("username") or ""
)
profile_avatar = ""
profile_resp = client.get_user_profile_batch(uid=uid, cookie=cookie, target_uids=[uid])
profile_resp = client.get_user_profile_batch(
uid=uid, cookie=cookie, target_uids=[uid]
)
if profile_resp is not None and profile_resp.profiles:
profile = profile_resp.profiles[0]
profile_nick = profile.nick or profile.passport or profile_nick
@@ -380,7 +421,9 @@ class BindMixin:
"mini_qrcode_image": mini_qrcode["mini_qrcode_image"],
"qrcode_token": mini_qrcode.get("qrcode_token") or "",
**bind_query_result,
"bind_phase": "waiting_scan" if mini_qrcode.get("qrcode_token") else "waiting_role",
"bind_phase": "waiting_scan"
if mini_qrcode.get("qrcode_token")
else "waiting_role",
"bind_ready_for_confirm": False,
"bind_polling": True,
"bind_redirect_url": bind_redirect_url,
@@ -410,16 +453,26 @@ class BindMixin:
result=result,
)
if role_name:
self._mark_task(worker_db, task, "success", f"已识别角色: {role_name},待确认绑定", result)
self._mark_task(
worker_db,
task,
"success",
f"已识别角色: {role_name},待确认绑定",
result,
)
return
if result.get("bind_phase") == "stopped":
self._mark_task(worker_db, task, "stopped", "任务已停止", result)
return
if result.get("bind_phase") == "qrcode_expired":
self._mark_task(worker_db, task, "failed", "绑定小程序码已失效,请重新获取", result)
self._mark_task(
worker_db, task, "failed", "绑定小程序码已失效,请重新获取", result
)
return
self._mark_task(worker_db, task, "success", "已生成绑定小程序码,未检测到绑定角色", result)
self._mark_task(
worker_db, task, "success", "已生成绑定小程序码,未检测到绑定角色", result
)
def _execute_query_game_name(
self,
@@ -429,14 +482,18 @@ class BindMixin:
account_info: dict,
config_info: dict,
):
b_act_id = str(self.payload.get("bind_act_id") or config_info.get("bind_act_id") or "").strip()
b_act_id = str(
self.payload.get("bind_act_id") or config_info.get("bind_act_id") or ""
).strip()
if not b_act_id:
self._mark_task(worker_db, task, "failed", "请先配置虎牙绑定 bActId")
return
b_act_id_int = self._to_int(b_act_id)
if not b_act_id_int:
self._mark_task(worker_db, task, "failed", f"虎牙绑定 bActId 无效: {b_act_id}")
self._mark_task(
worker_db, task, "failed", f"虎牙绑定 bActId 无效: {b_act_id}"
)
return
uid = self._resolve_uid(account_info)
@@ -449,7 +506,9 @@ class BindMixin:
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
return
client = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
client: Any = HuyaHttpClient(
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
)
bind_status, bind_query_result = self._resolve_bind_status(
client=client,
uid=uid,
@@ -497,14 +556,18 @@ class BindMixin:
account_info: dict,
config_info: dict,
):
b_act_id = str(self.payload.get("bind_act_id") or config_info.get("bind_act_id") or "").strip()
b_act_id = str(
self.payload.get("bind_act_id") or config_info.get("bind_act_id") or ""
).strip()
if not b_act_id:
self._mark_task(worker_db, task, "failed", "请先配置虎牙绑定 bActId")
return
b_act_id_int = self._to_int(b_act_id)
if not b_act_id_int:
self._mark_task(worker_db, task, "failed", f"虎牙绑定 bActId 无效: {b_act_id}")
self._mark_task(
worker_db, task, "failed", f"虎牙绑定 bActId 无效: {b_act_id}"
)
return
uid = self._resolve_uid(account_info)
@@ -517,7 +580,9 @@ class BindMixin:
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
return
client = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
client: Any = HuyaHttpClient(
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
)
role_status, role_query_result = self._resolve_bind_status(
client=client,
uid=uid,
@@ -545,7 +610,13 @@ class BindMixin:
"bind_confirmed": False,
**role_query_result,
}
self._mark_task(worker_db, task, "failed", "尚未识别到待确认角色,请先扫码完成绑定", result)
self._mark_task(
worker_db,
task,
"failed",
"尚未识别到待确认角色,请先扫码完成绑定",
result,
)
return
confirm_resp = client.confirm_bind_act_account(
@@ -589,7 +660,9 @@ class BindMixin:
**role_query_result,
}
self._apply_role_to_account(account, role_status, "bind_confirmed")
self._mark_task(worker_db, task, "success", f"确认绑定: {role_name}", result)
self._mark_task(
worker_db, task, "success", f"确认绑定: {role_name}", result
)
return
if refreshed_status.status != 200:
result = {
@@ -602,7 +675,9 @@ class BindMixin:
"refresh_error": refreshed_query_result,
}
self._apply_role_to_account(account, role_status, "bind_confirmed")
self._mark_task(worker_db, task, "success", f"确认绑定: {role_name}", result)
self._mark_task(
worker_db, task, "success", f"确认绑定: {role_name}", result
)
return
refreshed_confirmed = bool(
@@ -610,8 +685,12 @@ class BindMixin:
and refreshed_status.accountData.isBindRole
)
refreshed_role_info = self._bind_role_result(refreshed_status)
final_status = refreshed_status if refreshed_role_info["role_name"] else role_status
final_role_info = refreshed_role_info if refreshed_role_info["role_name"] else role_info
final_status = (
refreshed_status if refreshed_role_info["role_name"] else role_status
)
final_role_info = (
refreshed_role_info if refreshed_role_info["role_name"] else role_info
)
result = {
"bind_act_id": b_act_id_int,
"bind_confirmed": True,
@@ -625,4 +704,3 @@ class BindMixin:
self._apply_role_to_account(account, final_status, "bind_confirmed")
role_name = final_role_info["role_name"] or role_name or "已绑定"
self._mark_task(worker_db, task, "success", f"确认绑定: {role_name}", result)