946 lines
36 KiB
Python
946 lines
36 KiB
Python
"""虎牙任务执行器:角色绑定(由 huya_runner.py 按功能域拆分)。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from datetime import UTC, datetime
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from core.huya.http_client import query_bind_game_account
|
|
|
|
from ..huya_defaults import HUYA_DEFAULT_OUTER_ACT_ID
|
|
from ..models import HuyaAccount, HuyaTask
|
|
from .huya_runner_core import HUYA_RECHARGE_SOURCE_ID, HuyaBatchRunnerCore
|
|
|
|
HUYA_BIND_ROLE_POLL_SECONDS = 180
|
|
HUYA_BIND_ROLE_POLL_INTERVAL = 3
|
|
HUYA_BIND_ZT_UUID = "b02faae1"
|
|
HUYA_BIND_ROOM_ID = "30596253"
|
|
# 与外部单协作的新 CK 探针保持一致:HTTP cdnws + 外层活动参数 + 15 秒超时。
|
|
HUYA_CK_PROBE_TIMEOUT_SECONDS = 15.0
|
|
|
|
if TYPE_CHECKING:
|
|
from core.huya import HuyaHttpClient
|
|
|
|
|
|
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: ...
|
|
def _activity_client(self, uid: int, cookie: str): ...
|
|
def _http_client(self): ...
|
|
|
|
@staticmethod
|
|
def _role_name(bind_status) -> str:
|
|
account_data = bind_status.accountData
|
|
return account_data.gameRole.roleName or ""
|
|
|
|
@staticmethod
|
|
def _bind_role_result(bind_status) -> dict:
|
|
account_data = bind_status.accountData
|
|
game_account = account_data.gameAccount
|
|
game_role = account_data.gameRole
|
|
return {
|
|
"game_title": bind_status.gameName,
|
|
"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,
|
|
"is_need_act_check": account_data.isNeedActCheck,
|
|
"change_bind_time": account_data.changBindTime,
|
|
"game_account": game_account.to_dict(),
|
|
"game_role": game_role.to_dict(),
|
|
}
|
|
|
|
def _bind_query_roles(
|
|
self, bind_status: Any, bind_query_result: dict[str, Any]
|
|
) -> tuple[dict, dict | None, dict | None]:
|
|
"""从统一查询结果中取当前绑定和待确认角色。
|
|
|
|
新查询结果带 ``current_role`` / ``pending_role``;兼容旧测试桩或旧任务数据时,
|
|
没有这两个标记就继续把 ``bind_status`` 当成唯一的当前角色。
|
|
"""
|
|
has_role_split = (
|
|
"current_role" in bind_query_result or "pending_role" in bind_query_result
|
|
)
|
|
current = bind_query_result.get("current_role")
|
|
pending = bind_query_result.get("pending_role")
|
|
selected = (
|
|
{}
|
|
if has_role_split and (current or pending)
|
|
else self._bind_role_result(bind_status)
|
|
)
|
|
if not has_role_split:
|
|
current = selected if selected.get("role_name") else None
|
|
pending = None
|
|
return (
|
|
selected,
|
|
current if isinstance(current, dict) else None,
|
|
pending if isinstance(pending, dict) else None,
|
|
)
|
|
|
|
@staticmethod
|
|
def _bind_change_state_from_role(role_info: dict | None) -> dict:
|
|
"""用标准角色候选计算绑定/换绑时间状态。"""
|
|
if not role_info:
|
|
return {
|
|
"is_bound": False,
|
|
"can_change_bind": True,
|
|
"change_bind_time": 0,
|
|
"change_available_at": "",
|
|
"change_bind_day": 0,
|
|
}
|
|
is_bound = bool(
|
|
role_info.get("is_bind_account") and role_info.get("is_bind_role")
|
|
)
|
|
change_time = int(role_info.get("change_bind_time") or 0)
|
|
now = int(datetime.now(UTC).timestamp())
|
|
can_change = not is_bound or not change_time or change_time <= now
|
|
return {
|
|
"is_bound": is_bound,
|
|
"can_change_bind": can_change,
|
|
"change_bind_time": change_time,
|
|
"change_available_at": HuyaBatchRunnerCore._format_local_time(change_time),
|
|
"change_bind_day": int(role_info.get("change_bind_day") or 0),
|
|
}
|
|
|
|
@staticmethod
|
|
def _role_channel(bind_status) -> str:
|
|
game_role = bind_status.accountData.gameRole
|
|
parts = [game_role.platName, game_role.areaName]
|
|
return " / ".join(part for part in parts if part)
|
|
|
|
@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)
|
|
now = int(datetime.now(UTC).timestamp())
|
|
can_change = not is_bound or not change_time or change_time <= now
|
|
return {
|
|
"is_bound": is_bound,
|
|
"can_change_bind": can_change,
|
|
"change_bind_time": change_time,
|
|
"change_available_at": HuyaBatchRunnerCore._format_local_time(change_time),
|
|
"change_bind_day": int(bind_status.changeBindDay or 0),
|
|
}
|
|
|
|
@staticmethod
|
|
def _bind_ready_result(bind_status) -> dict:
|
|
role_info = BindMixin._bind_role_result(bind_status)
|
|
return {
|
|
**role_info,
|
|
**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",
|
|
}
|
|
|
|
@staticmethod
|
|
def _bind_protocol(config_info: dict, payload: dict | None = None) -> dict:
|
|
"""读取绑定协议参数;有 gid 时用 2026-11 抓包协议,否则回退旧协议。"""
|
|
payload = payload or {}
|
|
bind_gid_text = str(
|
|
payload.get("bind_gid") or config_info.get("bind_gid") or ""
|
|
).strip()
|
|
bind_gid = HuyaBatchRunnerCore._to_int(bind_gid_text)
|
|
outer_act_id = str(
|
|
payload.get("outer_act_id")
|
|
or config_info.get("outer_act_id")
|
|
or HUYA_DEFAULT_OUTER_ACT_ID
|
|
).strip()
|
|
if bind_gid and outer_act_id:
|
|
return {
|
|
"bind_gid": bind_gid_text,
|
|
"b_act_id": 0,
|
|
"gid": bind_gid,
|
|
"outer_act_id": outer_act_id,
|
|
"is_use_outer_act_id": 0,
|
|
"scene": "undefined",
|
|
}
|
|
b_act_id_text = str(
|
|
payload.get("bind_act_id") or config_info.get("bind_act_id") or ""
|
|
).strip()
|
|
return {
|
|
"bind_gid": "",
|
|
"b_act_id": HuyaBatchRunnerCore._to_int(b_act_id_text),
|
|
"gid": 0,
|
|
# 旧协议最初不传 outerActId;需要旧路径的地方按原逻辑显式补默认值。
|
|
"outer_act_id": "",
|
|
"is_use_outer_act_id": 1,
|
|
"scene": "",
|
|
}
|
|
|
|
def _resolve_bind_status(
|
|
self,
|
|
client: Any,
|
|
uid: int,
|
|
cookie: str,
|
|
b_act_id_int: int,
|
|
*,
|
|
outer_act_id: str = "",
|
|
timeout: float | None = None,
|
|
gid: int = 0,
|
|
is_use_outer_act_id: int = 1,
|
|
scene: str = "",
|
|
):
|
|
"""按活动页逻辑解析绑定状态(统一委托 core.huya.http_client 的通用查询)。"""
|
|
return query_bind_game_account(
|
|
client,
|
|
uid=uid,
|
|
cookie=cookie,
|
|
b_act_id=b_act_id_int,
|
|
gid=gid,
|
|
outer_act_id=outer_act_id,
|
|
is_use_outer_act_id=is_use_outer_act_id,
|
|
scene=scene,
|
|
timeout=timeout,
|
|
)
|
|
|
|
def _probe_bind_status(
|
|
self,
|
|
uid: int,
|
|
cookie: str,
|
|
b_act_id_int: int,
|
|
outer_act_id: str = "",
|
|
gid: int = 0,
|
|
is_use_outer_act_id: int = 1,
|
|
scene: str = "",
|
|
):
|
|
"""用外部单协作的新 CK 探针查角色。
|
|
|
|
关键点(2026-10-16 真机验证):必须走 HTTP cdnws 只读通道,带上
|
|
``outer_act_id`` 和 15 秒超时;响应为 None 就说明这份 CK 已经不认了。
|
|
不能再用 WSS 业务会话的默认参数,否则过期 CK 只会表现为“接口无响应”,
|
|
任务结果里没有可判定的失效标记。
|
|
"""
|
|
return self._resolve_bind_status(
|
|
self._http_client(),
|
|
uid,
|
|
cookie,
|
|
b_act_id_int,
|
|
outer_act_id=outer_act_id or HUYA_DEFAULT_OUTER_ACT_ID,
|
|
timeout=HUYA_CK_PROBE_TIMEOUT_SECONDS,
|
|
gid=gid,
|
|
is_use_outer_act_id=is_use_outer_act_id,
|
|
scene=scene,
|
|
)
|
|
|
|
def _apply_role_to_account(self, account: HuyaAccount, bind_status, status: str):
|
|
role_info = self._bind_role_result(bind_status)
|
|
self._apply_role_info_to_account(account, role_info, status)
|
|
|
|
@staticmethod
|
|
def _apply_role_info_to_account(
|
|
account: HuyaAccount, role_info: dict | None, status: str
|
|
) -> None:
|
|
"""只允许“当前已绑定角色”调用这里;待确认角色不得污染账号主角色。"""
|
|
role_name = str((role_info or {}).get("role_name") or "")
|
|
game_role = (role_info or {}).get("game_role") or {}
|
|
channel = " / ".join(
|
|
str(part)
|
|
for part in (
|
|
game_role.get("plat_name") if isinstance(game_role, dict) else "",
|
|
game_role.get("area_name") if isinstance(game_role, dict) else "",
|
|
)
|
|
if part
|
|
)
|
|
account.status = status
|
|
account.game_name = role_name or account.game_name
|
|
account.game_channel = channel or account.game_channel
|
|
account.updated_at = datetime.now(UTC)
|
|
|
|
@staticmethod
|
|
def _bind_redirect_url(config_info: dict) -> str:
|
|
room_pid = str(config_info.get("room_pid") or "").strip()
|
|
if not room_pid:
|
|
return ""
|
|
return (
|
|
f"https://zt.huya.com/{HUYA_BIND_ZT_UUID}/pc/index.html"
|
|
f"?sourceId={HUYA_RECHARGE_SOURCE_ID}"
|
|
f"&pid={room_pid}"
|
|
f"&anchorUid={room_pid}"
|
|
f"&roomid={HUYA_BIND_ROOM_ID}"
|
|
)
|
|
|
|
def _wait_bind_role_result(
|
|
self,
|
|
client: HuyaHttpClient,
|
|
worker_db: Session,
|
|
task: HuyaTask,
|
|
account: HuyaAccount,
|
|
uid: int,
|
|
cookie: str,
|
|
b_act_id_int: int,
|
|
result: dict,
|
|
*,
|
|
gid: int = 0,
|
|
outer_act_id: str = "",
|
|
is_use_outer_act_id: int = 1,
|
|
scene: str = "",
|
|
) -> tuple[str, dict]:
|
|
"""生成二维码后轮询扫码状态与角色同步,直到识别到角色、超时或停止。"""
|
|
deadline = time.monotonic() + HUYA_BIND_ROLE_POLL_SECONDS
|
|
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
|
|
)
|
|
|
|
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
|
|
)
|
|
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,
|
|
}
|
|
)
|
|
self._update_task_progress(
|
|
worker_db,
|
|
task,
|
|
"running",
|
|
"绑定小程序码已失效,请重新获取",
|
|
result,
|
|
)
|
|
return "", result
|
|
if qrcode_status["is_completed"]:
|
|
qrcode_finished = True
|
|
result["bind_phase"] = "qrcode_completed"
|
|
elif qrcode_status["is_scan"]:
|
|
result["bind_phase"] = "qrcode_scanned"
|
|
else:
|
|
result["bind_phase"] = "waiting_scan"
|
|
|
|
bind_status, bind_query_result = self._resolve_bind_status(
|
|
client=client,
|
|
uid=uid,
|
|
cookie=cookie,
|
|
b_act_id_int=b_act_id_int,
|
|
outer_act_id=outer_act_id,
|
|
gid=gid,
|
|
is_use_outer_act_id=is_use_outer_act_id,
|
|
scene=scene,
|
|
)
|
|
if bind_status is None:
|
|
continue
|
|
result.update(bind_query_result)
|
|
|
|
if bind_status.status != 200:
|
|
result.update(
|
|
{
|
|
"bind_phase": "role_check_failed",
|
|
"bind_status": bind_status.to_dict(),
|
|
}
|
|
)
|
|
self._update_task_progress(
|
|
worker_db,
|
|
task,
|
|
"running",
|
|
bind_status.msg or "等待绑定角色同步",
|
|
result,
|
|
)
|
|
continue
|
|
|
|
previous_phase = result.get("bind_phase")
|
|
ready = self._bind_ready_result(bind_status)
|
|
selected_info, current_info, pending_info = self._bind_query_roles(
|
|
bind_status, bind_query_result
|
|
)
|
|
has_role_split = (
|
|
"current_role" in bind_query_result
|
|
or "pending_role" in bind_query_result
|
|
)
|
|
target_info = pending_info or (
|
|
selected_info
|
|
if not has_role_split and selected_info.get("role_name")
|
|
else None
|
|
)
|
|
current_role_name = str((current_info or {}).get("role_name") or "")
|
|
pending_role_name = str((target_info or {}).get("role_name") or "")
|
|
ready.update(
|
|
{
|
|
"current_role_name": current_role_name,
|
|
"current_game_role": (current_info or {}).get("game_role"),
|
|
"current_game_channel": (current_info or {}).get("channel", ""),
|
|
"pending_role_name": pending_role_name,
|
|
"pending_game_role": (target_info or {}).get("game_role"),
|
|
"pending_game_channel": (target_info or {}).get("channel", ""),
|
|
"role_name": pending_role_name,
|
|
}
|
|
)
|
|
# 角色未就绪时保留扫码阶段文案,避免状态来回跳。
|
|
if not pending_role_name and previous_phase in {
|
|
"waiting_scan",
|
|
"qrcode_scanned",
|
|
"qrcode_completed",
|
|
}:
|
|
ready["bind_phase"] = previous_phase
|
|
result.update(ready)
|
|
result["bind_polling"] = True
|
|
role_name = pending_role_name
|
|
if role_name:
|
|
result["bind_polling"] = False
|
|
self._update_task_progress(
|
|
worker_db,
|
|
task,
|
|
"running",
|
|
f"已识别角色: {role_name},待确认绑定",
|
|
result,
|
|
)
|
|
return role_name, result
|
|
|
|
if result.get("bind_phase") == "qrcode_completed":
|
|
message = "小程序绑定已完成,等待角色同步"
|
|
elif result.get("bind_phase") == "qrcode_scanned":
|
|
message = "已扫码,等待小程序绑定完成"
|
|
else:
|
|
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,
|
|
}
|
|
)
|
|
return "", result
|
|
|
|
def _execute_get_bind_qr(
|
|
self,
|
|
worker_db: Session,
|
|
task: HuyaTask,
|
|
account: HuyaAccount,
|
|
account_info: dict,
|
|
config_info: dict,
|
|
):
|
|
protocol = self._bind_protocol(config_info, self.payload)
|
|
b_act_id_int = protocol["b_act_id"]
|
|
if not protocol["bind_gid"] and not b_act_id_int:
|
|
self._mark_task(worker_db, task, "failed", "请先配置虎牙绑定 bActId")
|
|
return
|
|
|
|
uid = self._resolve_uid(account_info)
|
|
if not uid:
|
|
self._mark_task(worker_db, task, "failed", "无法从账号或 Cookie 解析 yyuid")
|
|
return
|
|
|
|
cookie = account_info.get("cookie") or ""
|
|
if not cookie:
|
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
|
return
|
|
|
|
client: Any = self._activity_client(uid, cookie)
|
|
bind_status, bind_query_result = self._resolve_bind_status(
|
|
client=client,
|
|
uid=uid,
|
|
cookie=cookie,
|
|
b_act_id_int=b_act_id_int,
|
|
outer_act_id=protocol["outer_act_id"],
|
|
gid=protocol["gid"],
|
|
is_use_outer_act_id=protocol["is_use_outer_act_id"],
|
|
scene=protocol["scene"],
|
|
)
|
|
if bind_status is None:
|
|
self._mark_task(worker_db, task, "error", "虎牙绑定状态接口无响应")
|
|
return
|
|
if bind_status.status != 200:
|
|
self._mark_task(
|
|
worker_db,
|
|
task,
|
|
"failed",
|
|
bind_status.msg or f"虎牙绑定状态查询失败: {bind_status.status}",
|
|
bind_query_result or bind_status.to_dict(),
|
|
)
|
|
return
|
|
|
|
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"
|
|
)
|
|
if not change_state["can_change_bind"]:
|
|
role_name = role_info["role_name"] or "当前角色"
|
|
available_at = change_state["change_available_at"]
|
|
result = {
|
|
"bind_act_id": b_act_id_int,
|
|
"bind_gid": protocol["bind_gid"],
|
|
"gid": protocol["gid"],
|
|
**role_info,
|
|
**change_state,
|
|
"bind_status": bind_status.to_dict(),
|
|
}
|
|
self._mark_task(
|
|
worker_db,
|
|
task,
|
|
"failed",
|
|
f"{role_name} 暂不能更换,{available_at} 后可更换",
|
|
result,
|
|
)
|
|
return
|
|
|
|
live_link = client.get_live_link_param(
|
|
uid=uid,
|
|
cookie=cookie,
|
|
b_act_id=b_act_id_int,
|
|
gid=protocol["gid"],
|
|
outer_act_id=int(protocol["outer_act_id"] or 0),
|
|
game_auth_scene=bind_status.gameAuthScene,
|
|
)
|
|
if live_link is None:
|
|
self._mark_task(worker_db, task, "error", "虎牙绑定二维码参数接口无响应")
|
|
return
|
|
if live_link.status != 200:
|
|
self._mark_task(
|
|
worker_db,
|
|
task,
|
|
"failed",
|
|
live_link.msg or f"虎牙绑定二维码参数获取失败: {live_link.status}",
|
|
live_link.to_log_dict(),
|
|
)
|
|
return
|
|
|
|
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]
|
|
)
|
|
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
|
|
profile_avatar = profile.avatar or ""
|
|
|
|
bind_redirect_url = self._bind_redirect_url(config_info)
|
|
urls = client.build_bind_urls(
|
|
live_link.livelinkParam,
|
|
b_act_id_int,
|
|
game_auth_scene=bind_status.gameAuthScene,
|
|
nick_name=profile_nick,
|
|
face_url=profile_avatar,
|
|
redirect_url=bind_redirect_url,
|
|
)
|
|
mini_qrcode = client.get_livelink_mini_qrcode(urls["qr_url"])
|
|
if not mini_qrcode:
|
|
result = {
|
|
"bind_act_id": b_act_id_int,
|
|
"profile": {
|
|
"nick": profile_nick,
|
|
"avatar": profile_avatar,
|
|
},
|
|
}
|
|
self._mark_task(worker_db, task, "failed", "绑定小程序码获取失败", result)
|
|
return
|
|
|
|
result = {
|
|
"bind_act_id": b_act_id_int,
|
|
"bind_gid": protocol["bind_gid"],
|
|
"gid": protocol["gid"],
|
|
"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_ready_for_confirm": False,
|
|
"bind_polling": True,
|
|
"bind_redirect_url": bind_redirect_url,
|
|
**role_info,
|
|
**change_state,
|
|
"profile": {
|
|
"nick": profile_nick,
|
|
"avatar": profile_avatar,
|
|
},
|
|
}
|
|
|
|
account.status = "bind_qr_generated"
|
|
account.game_name = role_info["role_name"] or account.game_name
|
|
account.game_channel = self._role_channel(bind_status) or account.game_channel
|
|
account.nickname = profile_nick or account.nickname
|
|
account.updated_at = datetime.now(UTC)
|
|
|
|
# 生成二维码后自动轮询扫码/角色,避免用户必须手动点「查询角色」。
|
|
role_name, result = self._wait_bind_role_result(
|
|
client=client,
|
|
worker_db=worker_db,
|
|
task=task,
|
|
account=account,
|
|
uid=uid,
|
|
cookie=cookie,
|
|
b_act_id_int=b_act_id_int,
|
|
result=result,
|
|
gid=protocol["gid"],
|
|
outer_act_id=protocol["outer_act_id"],
|
|
is_use_outer_act_id=protocol["is_use_outer_act_id"],
|
|
scene=protocol["scene"],
|
|
)
|
|
if role_name:
|
|
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
|
|
)
|
|
return
|
|
self._mark_task(
|
|
worker_db, task, "success", "已生成绑定小程序码,未检测到绑定角色", result
|
|
)
|
|
|
|
def _execute_query_game_name(
|
|
self,
|
|
worker_db: Session,
|
|
task: HuyaTask,
|
|
account: HuyaAccount,
|
|
account_info: dict,
|
|
config_info: dict,
|
|
):
|
|
protocol = self._bind_protocol(config_info, self.payload)
|
|
b_act_id_int = protocol["b_act_id"]
|
|
if not protocol["bind_gid"] and not b_act_id_int:
|
|
self._mark_task(worker_db, task, "failed", "请先配置虎牙绑定 bActId")
|
|
return
|
|
|
|
uid = self._resolve_uid(account_info)
|
|
if not uid:
|
|
self._mark_task(worker_db, task, "failed", "无法从账号或 Cookie 解析 yyuid")
|
|
return
|
|
|
|
cookie = account_info.get("cookie") or ""
|
|
if not cookie:
|
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
|
return
|
|
|
|
bind_status, bind_query_result = self._probe_bind_status(
|
|
uid=uid,
|
|
cookie=cookie,
|
|
b_act_id_int=b_act_id_int,
|
|
outer_act_id=protocol["outer_act_id"],
|
|
gid=protocol["gid"],
|
|
is_use_outer_act_id=protocol["is_use_outer_act_id"],
|
|
scene=protocol["scene"],
|
|
)
|
|
if bind_status is None:
|
|
# 新探针没有回包 = 这份 CK 已经不认了,和外部单协作的判定一致。
|
|
account.status = "ck_expired"
|
|
account.updated_at = datetime.now(UTC)
|
|
self._mark_task(
|
|
worker_db,
|
|
task,
|
|
"error",
|
|
"虎牙角色信息接口无响应(CK 已失效,请重新登录或重新导入 Cookie)",
|
|
{
|
|
"bind_act_id": b_act_id_int,
|
|
"bind_gid": protocol["bind_gid"],
|
|
"gid": protocol["gid"],
|
|
"ck_expired": True,
|
|
"ck_expired_reason": "角色信息接口无响应",
|
|
},
|
|
)
|
|
return
|
|
if bind_status.status != 200:
|
|
# 有回包、只是业务码非 200(例如活动已下线)不算 CK 失效,避免误标红。
|
|
self._mark_task(
|
|
worker_db,
|
|
task,
|
|
"failed",
|
|
bind_status.msg or f"虎牙角色信息查询失败: {bind_status.status}",
|
|
{
|
|
"bind_act_id": b_act_id_int,
|
|
"bind_gid": protocol["bind_gid"],
|
|
"gid": protocol["gid"],
|
|
**bind_query_result,
|
|
},
|
|
)
|
|
return
|
|
|
|
selected_info, current_info, pending_info = self._bind_query_roles(
|
|
bind_status, bind_query_result
|
|
)
|
|
role_info = current_info or pending_info or selected_info
|
|
current_role_name = str((current_info or {}).get("role_name") or "")
|
|
pending_role_name = str((pending_info or {}).get("role_name") or "")
|
|
change_state = (
|
|
self._bind_change_state_from_role(current_info)
|
|
if current_info
|
|
else self._bind_change_state(bind_status)
|
|
)
|
|
result = {
|
|
"bind_act_id": b_act_id_int,
|
|
"bind_gid": protocol["bind_gid"],
|
|
"gid": protocol["gid"],
|
|
**role_info,
|
|
**change_state,
|
|
**bind_query_result,
|
|
"role_name": current_role_name or pending_role_name,
|
|
"current_role_name": current_role_name,
|
|
"current_game_role": (current_info or {}).get("game_role"),
|
|
"current_game_channel": (current_info or {}).get("channel", ""),
|
|
"pending_role_name": pending_role_name,
|
|
"pending_game_role": (pending_info or {}).get("game_role"),
|
|
"pending_game_channel": (pending_info or {}).get("channel", ""),
|
|
"bind_ready_for_confirm": bool(pending_role_name),
|
|
"bind_confirmed": bool(current_role_name),
|
|
"bind_phase": (
|
|
"role_ready"
|
|
if pending_role_name
|
|
else ("confirmed" if current_role_name else "waiting_role")
|
|
),
|
|
}
|
|
if current_info:
|
|
self._apply_role_info_to_account(account, current_info, "game_queried")
|
|
elif pending_info:
|
|
# 待确认角色只供页面展示和后续确认,不写账号主角色。
|
|
account.status = "game_not_bound"
|
|
account.updated_at = datetime.now(UTC)
|
|
elif selected_info.get("role_name"):
|
|
self._apply_role_to_account(account, bind_status, "game_queried")
|
|
|
|
if current_role_name or pending_role_name:
|
|
if current_role_name and pending_role_name:
|
|
message = (
|
|
f"当前已绑定: {current_role_name};待确认: {pending_role_name}"
|
|
)
|
|
elif current_role_name:
|
|
message = f"角色: {current_role_name}"
|
|
else:
|
|
message = f"待确认角色: {pending_role_name}"
|
|
self._mark_task(worker_db, task, "success", message, result)
|
|
return
|
|
|
|
account.status = "game_not_bound"
|
|
account.updated_at = datetime.now(UTC)
|
|
self._mark_task(worker_db, task, "success", "未绑定游戏角色", result)
|
|
|
|
def _execute_confirm_bind(
|
|
self,
|
|
worker_db: Session,
|
|
task: HuyaTask,
|
|
account: HuyaAccount,
|
|
account_info: dict,
|
|
config_info: dict,
|
|
):
|
|
protocol = self._bind_protocol(config_info, self.payload)
|
|
b_act_id_int = protocol["b_act_id"]
|
|
if not protocol["bind_gid"] and not b_act_id_int:
|
|
self._mark_task(worker_db, task, "failed", "请先配置虎牙绑定 bActId")
|
|
return
|
|
|
|
uid = self._resolve_uid(account_info)
|
|
if not uid:
|
|
self._mark_task(worker_db, task, "failed", "无法从账号或 Cookie 解析 yyuid")
|
|
return
|
|
|
|
cookie = account_info.get("cookie") or ""
|
|
if not cookie:
|
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
|
return
|
|
|
|
client: Any = self._activity_client(uid, cookie)
|
|
role_status, role_query_result = self._resolve_bind_status(
|
|
client=client,
|
|
uid=uid,
|
|
cookie=cookie,
|
|
b_act_id_int=b_act_id_int,
|
|
outer_act_id=protocol["outer_act_id"],
|
|
gid=protocol["gid"],
|
|
is_use_outer_act_id=protocol["is_use_outer_act_id"],
|
|
scene=protocol["scene"],
|
|
)
|
|
if role_status is None:
|
|
self._mark_task(worker_db, task, "error", "虎牙绑定角色查询接口无响应")
|
|
return
|
|
if role_status.status != 200:
|
|
self._mark_task(
|
|
worker_db,
|
|
task,
|
|
"failed",
|
|
role_status.msg or f"虎牙绑定角色查询失败: {role_status.status}",
|
|
{"bind_act_id": b_act_id_int, **role_query_result},
|
|
)
|
|
return
|
|
|
|
selected_info, current_info, pending_info = self._bind_query_roles(
|
|
role_status, role_query_result
|
|
)
|
|
role_info = pending_info or current_info or selected_info
|
|
role_name = str(role_info.get("role_name") or "")
|
|
if not role_name:
|
|
result = {
|
|
"bind_act_id": b_act_id_int,
|
|
"bind_gid": protocol["bind_gid"],
|
|
"gid": protocol["gid"],
|
|
"bind_confirmed": False,
|
|
**role_query_result,
|
|
}
|
|
self._mark_task(
|
|
worker_db,
|
|
task,
|
|
"failed",
|
|
"尚未识别到待确认角色,请先扫码完成绑定",
|
|
result,
|
|
)
|
|
return
|
|
|
|
confirm_resp = client.confirm_bind_act_account(
|
|
uid=uid,
|
|
cookie=cookie,
|
|
b_act_id=b_act_id_int,
|
|
gid=protocol["gid"],
|
|
outer_act_id=protocol["outer_act_id"],
|
|
)
|
|
if confirm_resp is None:
|
|
self._mark_task(worker_db, task, "error", "虎牙确认绑定接口无响应")
|
|
return
|
|
if confirm_resp.status != 200:
|
|
result = {
|
|
"bind_act_id": b_act_id_int,
|
|
"bind_gid": protocol["bind_gid"],
|
|
"gid": protocol["gid"],
|
|
"bind_confirmed": False,
|
|
"confirm_result": confirm_resp.to_dict(),
|
|
"before_bind_status": role_status.to_dict(),
|
|
**role_query_result,
|
|
"before_current_role_name": str(
|
|
(current_info or {}).get("role_name") or ""
|
|
),
|
|
"before_pending_role_name": str(
|
|
(pending_info or {}).get("role_name") or ""
|
|
),
|
|
}
|
|
self._mark_task(
|
|
worker_db,
|
|
task,
|
|
"failed",
|
|
confirm_resp.msg or f"虎牙确认绑定失败: {confirm_resp.status}",
|
|
result,
|
|
)
|
|
return
|
|
|
|
refreshed_status, refreshed_query_result = self._resolve_bind_status(
|
|
client=client,
|
|
uid=uid,
|
|
cookie=cookie,
|
|
b_act_id_int=b_act_id_int,
|
|
outer_act_id=protocol["outer_act_id"],
|
|
gid=protocol["gid"],
|
|
is_use_outer_act_id=protocol["is_use_outer_act_id"],
|
|
scene=protocol["scene"],
|
|
)
|
|
if refreshed_status is None:
|
|
result = {
|
|
"bind_act_id": b_act_id_int,
|
|
"bind_gid": protocol["bind_gid"],
|
|
"gid": protocol["gid"],
|
|
"bind_confirmed": True,
|
|
"confirm_result": confirm_resp.to_dict(),
|
|
"before_bind_status": role_status.to_dict(),
|
|
**role_info,
|
|
**role_query_result,
|
|
"bind_role_name": role_name,
|
|
}
|
|
self._apply_role_info_to_account(account, role_info, "bind_confirmed")
|
|
self._mark_task(
|
|
worker_db, task, "success", f"确认绑定: {role_name}", result
|
|
)
|
|
return
|
|
if refreshed_status.status != 200:
|
|
result = {
|
|
"bind_act_id": b_act_id_int,
|
|
"bind_gid": protocol["bind_gid"],
|
|
"gid": protocol["gid"],
|
|
"bind_confirmed": True,
|
|
"confirm_result": confirm_resp.to_dict(),
|
|
"before_bind_status": role_status.to_dict(),
|
|
**role_info,
|
|
**role_query_result,
|
|
"refresh_error": refreshed_query_result,
|
|
"bind_role_name": role_name,
|
|
}
|
|
self._apply_role_info_to_account(account, role_info, "bind_confirmed")
|
|
self._mark_task(
|
|
worker_db, task, "success", f"确认绑定: {role_name}", result
|
|
)
|
|
return
|
|
|
|
refreshed_selected, refreshed_current, refreshed_pending = (
|
|
self._bind_query_roles(refreshed_status, refreshed_query_result)
|
|
)
|
|
refreshed_confirmed = bool(
|
|
refreshed_current or refreshed_selected.get("role_name")
|
|
)
|
|
confirmed_target = (
|
|
refreshed_pending
|
|
if refreshed_pending
|
|
and str(refreshed_pending.get("role_name") or "") == role_name
|
|
else None
|
|
)
|
|
confirmed_pending_role = bool(pending_info)
|
|
refreshed_current_is_target = bool(
|
|
refreshed_current
|
|
and str(refreshed_current.get("role_name") or "") == role_name
|
|
)
|
|
final_role_info = (
|
|
(refreshed_current if refreshed_current_is_target else confirmed_target)
|
|
if confirmed_pending_role
|
|
else refreshed_current
|
|
)
|
|
final_role_info = (
|
|
final_role_info
|
|
or (refreshed_selected if refreshed_selected.get("role_name") else None)
|
|
or role_info
|
|
)
|
|
result = {
|
|
"bind_act_id": b_act_id_int,
|
|
"bind_gid": protocol["bind_gid"],
|
|
"gid": protocol["gid"],
|
|
"bind_confirmed": True,
|
|
"refreshed_is_bound": refreshed_confirmed,
|
|
"confirm_result": confirm_resp.to_dict(),
|
|
"before_bind_status": role_status.to_dict(),
|
|
**role_query_result,
|
|
"refresh_result": refreshed_query_result,
|
|
**final_role_info,
|
|
"bind_role_name": final_role_info.get("role_name") or role_name,
|
|
}
|
|
self._apply_role_info_to_account(account, final_role_info, "bind_confirmed")
|
|
role_name = final_role_info["role_name"] or role_name or "已绑定"
|
|
self._mark_task(worker_db, task, "success", f"确认绑定: {role_name}", result)
|