type: 收窄虎牙绑定与充值执行器类型
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import time
|
import time
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
@@ -18,10 +19,27 @@ HUYA_BIND_ROLE_POLL_SECONDS = 180
|
|||||||
HUYA_BIND_ROLE_POLL_INTERVAL = 3
|
HUYA_BIND_ROLE_POLL_INTERVAL = 3
|
||||||
HUYA_BIND_ZT_UUID = "b02faae1"
|
HUYA_BIND_ZT_UUID = "b02faae1"
|
||||||
HUYA_BIND_ROOM_ID = "30596253"
|
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:
|
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
|
@staticmethod
|
||||||
def _role_name(bind_status) -> str:
|
def _role_name(bind_status) -> str:
|
||||||
account_data = bind_status.accountData
|
account_data = bind_status.accountData
|
||||||
@@ -29,7 +47,7 @@ class BindMixin:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _has_bind_role(bind_status) -> bool:
|
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
|
@staticmethod
|
||||||
def _bind_role_result(bind_status) -> dict:
|
def _bind_role_result(bind_status) -> dict:
|
||||||
@@ -38,7 +56,7 @@ class BindMixin:
|
|||||||
game_role = account_data.gameRole
|
game_role = account_data.gameRole
|
||||||
return {
|
return {
|
||||||
"game_title": bind_status.gameName,
|
"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,
|
"change_bind_day": bind_status.changeBindDay,
|
||||||
"is_bind_account": account_data.isBindAcount,
|
"is_bind_account": account_data.isBindAcount,
|
||||||
"is_bind_role": account_data.isBindRole,
|
"is_bind_role": account_data.isBindRole,
|
||||||
@@ -54,8 +72,8 @@ class BindMixin:
|
|||||||
parts = [game_role.platName, game_role.areaName]
|
parts = [game_role.platName, game_role.areaName]
|
||||||
return " / ".join(part for part in parts if part)
|
return " / ".join(part for part in parts if part)
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
def _bind_change_state(cls, bind_status) -> dict:
|
def _bind_change_state(bind_status) -> dict:
|
||||||
account_data = bind_status.accountData
|
account_data = bind_status.accountData
|
||||||
is_bound = bool(account_data.isBindAcount and account_data.isBindRole)
|
is_bound = bool(account_data.isBindAcount and account_data.isBindRole)
|
||||||
change_time = int(account_data.changBindTime or 0)
|
change_time = int(account_data.changBindTime or 0)
|
||||||
@@ -65,16 +83,16 @@ class BindMixin:
|
|||||||
"is_bound": is_bound,
|
"is_bound": is_bound,
|
||||||
"can_change_bind": can_change,
|
"can_change_bind": can_change,
|
||||||
"change_bind_time": change_time,
|
"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),
|
"change_bind_day": int(bind_status.changeBindDay or 0),
|
||||||
}
|
}
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
def _bind_ready_result(cls, bind_status) -> dict:
|
def _bind_ready_result(bind_status) -> dict:
|
||||||
role_info = cls._bind_role_result(bind_status)
|
role_info = BindMixin._bind_role_result(bind_status)
|
||||||
return {
|
return {
|
||||||
**role_info,
|
**role_info,
|
||||||
**cls._bind_change_state(bind_status),
|
**BindMixin._bind_change_state(bind_status),
|
||||||
"bind_status": bind_status.to_dict(),
|
"bind_status": bind_status.to_dict(),
|
||||||
"bind_ready_for_confirm": bool(role_info["role_name"]),
|
"bind_ready_for_confirm": bool(role_info["role_name"]),
|
||||||
"bind_phase": "role_ready" if role_info["role_name"] else "waiting_role",
|
"bind_phase": "role_ready" if role_info["role_name"] else "waiting_role",
|
||||||
@@ -82,7 +100,7 @@ class BindMixin:
|
|||||||
|
|
||||||
def _resolve_bind_status(
|
def _resolve_bind_status(
|
||||||
self,
|
self,
|
||||||
client: HuyaHttpClient,
|
client: Any,
|
||||||
uid: int,
|
uid: int,
|
||||||
cookie: str,
|
cookie: str,
|
||||||
b_act_id_int: int,
|
b_act_id_int: int,
|
||||||
@@ -106,9 +124,10 @@ class BindMixin:
|
|||||||
|
|
||||||
chosen_status = outer_status
|
chosen_status = outer_status
|
||||||
account_data = outer_status.accountData
|
account_data = outer_status.accountData
|
||||||
should_check_inner = (
|
should_check_inner = not self._has_bind_role(outer_status) and bool(
|
||||||
not self._has_bind_role(outer_status)
|
account_data.isNeedActCheck
|
||||||
and bool(account_data.isNeedActCheck or not account_data.isBindAcount or not account_data.isBindRole)
|
or not account_data.isBindAcount
|
||||||
|
or not account_data.isBindRole
|
||||||
)
|
)
|
||||||
if should_check_inner:
|
if should_check_inner:
|
||||||
inner_status = client.check_user_bind_game_account(
|
inner_status = client.check_user_bind_game_account(
|
||||||
@@ -162,22 +181,28 @@ class BindMixin:
|
|||||||
qrcode_token = str(result.get("qrcode_token") or "")
|
qrcode_token = str(result.get("qrcode_token") or "")
|
||||||
qrcode_finished = not qrcode_token
|
qrcode_finished = not qrcode_token
|
||||||
result["bind_polling"] = True
|
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:
|
while not self._stop.is_set() and time.monotonic() < deadline:
|
||||||
if self._stop.wait(HUYA_BIND_ROLE_POLL_INTERVAL):
|
if self._stop.wait(HUYA_BIND_ROLE_POLL_INTERVAL):
|
||||||
break
|
break
|
||||||
|
|
||||||
if qrcode_token and not qrcode_finished:
|
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:
|
if qrcode_status is not None:
|
||||||
result["qrcode_status"] = qrcode_status
|
result["qrcode_status"] = qrcode_status
|
||||||
if qrcode_status["is_expired"] or qrcode_status["is_failure"]:
|
if qrcode_status["is_expired"] or qrcode_status["is_failure"]:
|
||||||
result.update({
|
result.update(
|
||||||
"bind_phase": "qrcode_expired",
|
{
|
||||||
"bind_ready_for_confirm": False,
|
"bind_phase": "qrcode_expired",
|
||||||
"bind_polling": False,
|
"bind_ready_for_confirm": False,
|
||||||
})
|
"bind_polling": False,
|
||||||
|
}
|
||||||
|
)
|
||||||
self._update_task_progress(
|
self._update_task_progress(
|
||||||
worker_db,
|
worker_db,
|
||||||
task,
|
task,
|
||||||
@@ -205,10 +230,12 @@ class BindMixin:
|
|||||||
result.update(bind_query_result)
|
result.update(bind_query_result)
|
||||||
|
|
||||||
if bind_status.status != 200:
|
if bind_status.status != 200:
|
||||||
result.update({
|
result.update(
|
||||||
"bind_phase": "role_check_failed",
|
{
|
||||||
"bind_status": bind_status.to_dict(),
|
"bind_phase": "role_check_failed",
|
||||||
})
|
"bind_status": bind_status.to_dict(),
|
||||||
|
}
|
||||||
|
)
|
||||||
self._update_task_progress(
|
self._update_task_progress(
|
||||||
worker_db,
|
worker_db,
|
||||||
task,
|
task,
|
||||||
@@ -250,11 +277,13 @@ class BindMixin:
|
|||||||
message = "已生成绑定小程序码,等待扫码绑定"
|
message = "已生成绑定小程序码,等待扫码绑定"
|
||||||
self._update_task_progress(worker_db, task, "running", message, result)
|
self._update_task_progress(worker_db, task, "running", message, result)
|
||||||
|
|
||||||
result.update({
|
result.update(
|
||||||
"bind_phase": "role_timeout" if not self._stop.is_set() else "stopped",
|
{
|
||||||
"bind_ready_for_confirm": False,
|
"bind_phase": "role_timeout" if not self._stop.is_set() else "stopped",
|
||||||
"bind_polling": False,
|
"bind_ready_for_confirm": False,
|
||||||
})
|
"bind_polling": False,
|
||||||
|
}
|
||||||
|
)
|
||||||
return "", result
|
return "", result
|
||||||
|
|
||||||
def _execute_get_bind_qr(
|
def _execute_get_bind_qr(
|
||||||
@@ -265,14 +294,18 @@ class BindMixin:
|
|||||||
account_info: dict,
|
account_info: dict,
|
||||||
config_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:
|
if not b_act_id:
|
||||||
self._mark_task(worker_db, task, "failed", "请先配置虎牙绑定 bActId")
|
self._mark_task(worker_db, task, "failed", "请先配置虎牙绑定 bActId")
|
||||||
return
|
return
|
||||||
|
|
||||||
b_act_id_int = self._to_int(b_act_id)
|
b_act_id_int = self._to_int(b_act_id)
|
||||||
if not b_act_id_int:
|
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
|
return
|
||||||
|
|
||||||
uid = self._resolve_uid(account_info)
|
uid = self._resolve_uid(account_info)
|
||||||
@@ -285,7 +318,9 @@ class BindMixin:
|
|||||||
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
||||||
return
|
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(
|
bind_status, bind_query_result = self._resolve_bind_status(
|
||||||
client=client,
|
client=client,
|
||||||
uid=uid,
|
uid=uid,
|
||||||
@@ -308,7 +343,9 @@ class BindMixin:
|
|||||||
role_info = self._bind_role_result(bind_status)
|
role_info = self._bind_role_result(bind_status)
|
||||||
change_state = self._bind_change_state(bind_status)
|
change_state = self._bind_change_state(bind_status)
|
||||||
if role_info["role_name"]:
|
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"]:
|
if not change_state["can_change_bind"]:
|
||||||
role_name = role_info["role_name"] or "当前角色"
|
role_name = role_info["role_name"] or "当前角色"
|
||||||
available_at = change_state["change_available_at"]
|
available_at = change_state["change_available_at"]
|
||||||
@@ -346,9 +383,13 @@ class BindMixin:
|
|||||||
)
|
)
|
||||||
return
|
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_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:
|
if profile_resp is not None and profile_resp.profiles:
|
||||||
profile = profile_resp.profiles[0]
|
profile = profile_resp.profiles[0]
|
||||||
profile_nick = profile.nick or profile.passport or profile_nick
|
profile_nick = profile.nick or profile.passport or profile_nick
|
||||||
@@ -380,7 +421,9 @@ class BindMixin:
|
|||||||
"mini_qrcode_image": mini_qrcode["mini_qrcode_image"],
|
"mini_qrcode_image": mini_qrcode["mini_qrcode_image"],
|
||||||
"qrcode_token": mini_qrcode.get("qrcode_token") or "",
|
"qrcode_token": mini_qrcode.get("qrcode_token") or "",
|
||||||
**bind_query_result,
|
**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_ready_for_confirm": False,
|
||||||
"bind_polling": True,
|
"bind_polling": True,
|
||||||
"bind_redirect_url": bind_redirect_url,
|
"bind_redirect_url": bind_redirect_url,
|
||||||
@@ -410,16 +453,26 @@ class BindMixin:
|
|||||||
result=result,
|
result=result,
|
||||||
)
|
)
|
||||||
if role_name:
|
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
|
return
|
||||||
|
|
||||||
if result.get("bind_phase") == "stopped":
|
if result.get("bind_phase") == "stopped":
|
||||||
self._mark_task(worker_db, task, "stopped", "任务已停止", result)
|
self._mark_task(worker_db, task, "stopped", "任务已停止", result)
|
||||||
return
|
return
|
||||||
if result.get("bind_phase") == "qrcode_expired":
|
if result.get("bind_phase") == "qrcode_expired":
|
||||||
self._mark_task(worker_db, task, "failed", "绑定小程序码已失效,请重新获取", result)
|
self._mark_task(
|
||||||
|
worker_db, task, "failed", "绑定小程序码已失效,请重新获取", result
|
||||||
|
)
|
||||||
return
|
return
|
||||||
self._mark_task(worker_db, task, "success", "已生成绑定小程序码,未检测到绑定角色", result)
|
self._mark_task(
|
||||||
|
worker_db, task, "success", "已生成绑定小程序码,未检测到绑定角色", result
|
||||||
|
)
|
||||||
|
|
||||||
def _execute_query_game_name(
|
def _execute_query_game_name(
|
||||||
self,
|
self,
|
||||||
@@ -429,14 +482,18 @@ class BindMixin:
|
|||||||
account_info: dict,
|
account_info: dict,
|
||||||
config_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:
|
if not b_act_id:
|
||||||
self._mark_task(worker_db, task, "failed", "请先配置虎牙绑定 bActId")
|
self._mark_task(worker_db, task, "failed", "请先配置虎牙绑定 bActId")
|
||||||
return
|
return
|
||||||
|
|
||||||
b_act_id_int = self._to_int(b_act_id)
|
b_act_id_int = self._to_int(b_act_id)
|
||||||
if not b_act_id_int:
|
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
|
return
|
||||||
|
|
||||||
uid = self._resolve_uid(account_info)
|
uid = self._resolve_uid(account_info)
|
||||||
@@ -449,7 +506,9 @@ class BindMixin:
|
|||||||
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
||||||
return
|
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(
|
bind_status, bind_query_result = self._resolve_bind_status(
|
||||||
client=client,
|
client=client,
|
||||||
uid=uid,
|
uid=uid,
|
||||||
@@ -497,14 +556,18 @@ class BindMixin:
|
|||||||
account_info: dict,
|
account_info: dict,
|
||||||
config_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:
|
if not b_act_id:
|
||||||
self._mark_task(worker_db, task, "failed", "请先配置虎牙绑定 bActId")
|
self._mark_task(worker_db, task, "failed", "请先配置虎牙绑定 bActId")
|
||||||
return
|
return
|
||||||
|
|
||||||
b_act_id_int = self._to_int(b_act_id)
|
b_act_id_int = self._to_int(b_act_id)
|
||||||
if not b_act_id_int:
|
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
|
return
|
||||||
|
|
||||||
uid = self._resolve_uid(account_info)
|
uid = self._resolve_uid(account_info)
|
||||||
@@ -517,7 +580,9 @@ class BindMixin:
|
|||||||
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
||||||
return
|
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(
|
role_status, role_query_result = self._resolve_bind_status(
|
||||||
client=client,
|
client=client,
|
||||||
uid=uid,
|
uid=uid,
|
||||||
@@ -545,7 +610,13 @@ class BindMixin:
|
|||||||
"bind_confirmed": False,
|
"bind_confirmed": False,
|
||||||
**role_query_result,
|
**role_query_result,
|
||||||
}
|
}
|
||||||
self._mark_task(worker_db, task, "failed", "尚未识别到待确认角色,请先扫码完成绑定", result)
|
self._mark_task(
|
||||||
|
worker_db,
|
||||||
|
task,
|
||||||
|
"failed",
|
||||||
|
"尚未识别到待确认角色,请先扫码完成绑定",
|
||||||
|
result,
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
confirm_resp = client.confirm_bind_act_account(
|
confirm_resp = client.confirm_bind_act_account(
|
||||||
@@ -589,7 +660,9 @@ class BindMixin:
|
|||||||
**role_query_result,
|
**role_query_result,
|
||||||
}
|
}
|
||||||
self._apply_role_to_account(account, role_status, "bind_confirmed")
|
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
|
return
|
||||||
if refreshed_status.status != 200:
|
if refreshed_status.status != 200:
|
||||||
result = {
|
result = {
|
||||||
@@ -602,7 +675,9 @@ class BindMixin:
|
|||||||
"refresh_error": refreshed_query_result,
|
"refresh_error": refreshed_query_result,
|
||||||
}
|
}
|
||||||
self._apply_role_to_account(account, role_status, "bind_confirmed")
|
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
|
return
|
||||||
|
|
||||||
refreshed_confirmed = bool(
|
refreshed_confirmed = bool(
|
||||||
@@ -610,8 +685,12 @@ class BindMixin:
|
|||||||
and refreshed_status.accountData.isBindRole
|
and refreshed_status.accountData.isBindRole
|
||||||
)
|
)
|
||||||
refreshed_role_info = self._bind_role_result(refreshed_status)
|
refreshed_role_info = self._bind_role_result(refreshed_status)
|
||||||
final_status = refreshed_status if refreshed_role_info["role_name"] else role_status
|
final_status = (
|
||||||
final_role_info = refreshed_role_info if refreshed_role_info["role_name"] else role_info
|
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 = {
|
result = {
|
||||||
"bind_act_id": b_act_id_int,
|
"bind_act_id": b_act_id_int,
|
||||||
"bind_confirmed": True,
|
"bind_confirmed": True,
|
||||||
@@ -625,4 +704,3 @@ class BindMixin:
|
|||||||
self._apply_role_to_account(account, final_status, "bind_confirmed")
|
self._apply_role_to_account(account, final_status, "bind_confirmed")
|
||||||
role_name = final_role_info["role_name"] or role_name or "已绑定"
|
role_name = final_role_info["role_name"] or role_name or "已绑定"
|
||||||
self._mark_task(worker_db, task, "success", f"确认绑定: {role_name}", result)
|
self._mark_task(worker_db, task, "success", f"确认绑定: {role_name}", result)
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import time
|
import time
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
from typing import Any, TYPE_CHECKING
|
||||||
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
@@ -24,8 +25,28 @@ HUYA_RECHARGE_EXTRA_PRODUCTS = [
|
|||||||
]
|
]
|
||||||
from .huya_runner_core import HUYA_RECHARGE_SOURCE_ID
|
from .huya_runner_core import HUYA_RECHARGE_SOURCE_ID
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .huya_runner import HuyaBatchRunner
|
||||||
|
|
||||||
|
|
||||||
class RechargeMixin:
|
class RechargeMixin:
|
||||||
"""充值域:充值商品、下单与到账轮询。"""
|
"""充值域:充值商品、下单与到账轮询。"""
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
_stop: Any
|
||||||
|
payload: dict
|
||||||
|
|
||||||
|
@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 _format_local_time(timestamp: int) -> str: ...
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _huya_order_status_label(status: int) -> str:
|
def _huya_order_status_label(status: int) -> str:
|
||||||
from core.huya.shop_structs import OrderStatus
|
from core.huya.shop_structs import OrderStatus
|
||||||
@@ -43,8 +64,8 @@ class RechargeMixin:
|
|||||||
}
|
}
|
||||||
return labels.get(int(status or 0), str(status or "未知"))
|
return labels.get(int(status or 0), str(status or "未知"))
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
def _is_huya_order_paid(cls, order) -> bool:
|
def _is_huya_order_paid(order) -> bool:
|
||||||
from core.huya.shop_structs import OrderStatus
|
from core.huya.shop_structs import OrderStatus
|
||||||
|
|
||||||
paid_statuses = {
|
paid_statuses = {
|
||||||
@@ -54,11 +75,14 @@ class RechargeMixin:
|
|||||||
OrderStatus.FINISHED,
|
OrderStatus.FINISHED,
|
||||||
OrderStatus.FINISHED_CLOSED,
|
OrderStatus.FINISHED_CLOSED,
|
||||||
}
|
}
|
||||||
return int(getattr(order, "payTime", 0) or 0) > 0 or int(getattr(order, "orderStatus", 0) or 0) in paid_statuses
|
return (
|
||||||
|
int(getattr(order, "payTime", 0) or 0) > 0
|
||||||
|
or int(getattr(order, "orderStatus", 0) or 0) in paid_statuses
|
||||||
|
)
|
||||||
|
|
||||||
def _wait_recharge_payment(
|
def _wait_recharge_payment(
|
||||||
self,
|
self,
|
||||||
client: HuyaHttpClient,
|
client: Any,
|
||||||
uid: int,
|
uid: int,
|
||||||
guid: str,
|
guid: str,
|
||||||
cookie: str,
|
cookie: str,
|
||||||
@@ -86,19 +110,25 @@ class RechargeMixin:
|
|||||||
continue
|
continue
|
||||||
last_order = order.to_dict()
|
last_order = order.to_dict()
|
||||||
status = int(getattr(order, "orderStatus", 0) or 0)
|
status = int(getattr(order, "orderStatus", 0) or 0)
|
||||||
result.update({
|
result.update(
|
||||||
"payment_checked_at": checked_at,
|
{
|
||||||
"payment_order": last_order,
|
"payment_checked_at": checked_at,
|
||||||
"payment_order_status": status,
|
"payment_order": last_order,
|
||||||
"payment_order_status_label": self._huya_order_status_label(status),
|
"payment_order_status": status,
|
||||||
})
|
"payment_order_status_label": self._huya_order_status_label(
|
||||||
|
status
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
if self._is_huya_order_paid(order):
|
if self._is_huya_order_paid(order):
|
||||||
result.update({
|
result.update(
|
||||||
"payment_status": "paid",
|
{
|
||||||
"payment_status_label": "已支付",
|
"payment_status": "paid",
|
||||||
"payment_paid": True,
|
"payment_status_label": "已支付",
|
||||||
"payment_paid_at": checked_at,
|
"payment_paid": True,
|
||||||
})
|
"payment_paid_at": checked_at,
|
||||||
|
}
|
||||||
|
)
|
||||||
return "paid", last_order
|
return "paid", last_order
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
@@ -106,12 +136,16 @@ class RechargeMixin:
|
|||||||
if self._stop.wait(HUYA_PAYMENT_POLL_INTERVAL):
|
if self._stop.wait(HUYA_PAYMENT_POLL_INTERVAL):
|
||||||
break
|
break
|
||||||
|
|
||||||
result.update({
|
result.update(
|
||||||
"payment_status": "timeout" if not self._stop.is_set() else "stopped",
|
{
|
||||||
"payment_status_label": "等待支付超时" if not self._stop.is_set() else "已停止监听",
|
"payment_status": "timeout" if not self._stop.is_set() else "stopped",
|
||||||
"payment_paid": False,
|
"payment_status_label": "等待支付超时"
|
||||||
"payment_timeout_seconds": HUYA_PAYMENT_POLL_SECONDS,
|
if not self._stop.is_set()
|
||||||
})
|
else "已停止监听",
|
||||||
|
"payment_paid": False,
|
||||||
|
"payment_timeout_seconds": HUYA_PAYMENT_POLL_SECONDS,
|
||||||
|
}
|
||||||
|
)
|
||||||
if last_order:
|
if last_order:
|
||||||
result["payment_order"] = last_order
|
result["payment_order"] = last_order
|
||||||
return result["payment_status"], last_order
|
return result["payment_status"], last_order
|
||||||
@@ -157,8 +191,12 @@ class RechargeMixin:
|
|||||||
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
||||||
return
|
return
|
||||||
|
|
||||||
client = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
|
client: Any = HuyaHttpClient(
|
||||||
task_resp = client.get_act_task_detail(uid=uid, cookie=cookie, act_id=HUYA_RECHARGE_ACT_ID)
|
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
|
||||||
|
)
|
||||||
|
task_resp = client.get_act_task_detail(
|
||||||
|
uid=uid, cookie=cookie, act_id=HUYA_RECHARGE_ACT_ID
|
||||||
|
)
|
||||||
if task_resp is None:
|
if task_resp is None:
|
||||||
self._mark_task(worker_db, task, "error", "虎牙充值任务详情接口无响应")
|
self._mark_task(worker_db, task, "error", "虎牙充值任务详情接口无响应")
|
||||||
return
|
return
|
||||||
@@ -190,20 +228,24 @@ class RechargeMixin:
|
|||||||
for index, item in enumerate(task_result.get("tasks", []), start=1):
|
for index, item in enumerate(task_result.get("tasks", []), start=1):
|
||||||
if int(item.get("task_type") or 0) != 67:
|
if int(item.get("task_type") or 0) != 67:
|
||||||
continue
|
continue
|
||||||
add_candidate({
|
add_candidate(
|
||||||
"spu_id": item.get("spu_id") or "",
|
{
|
||||||
"name": item.get("name") or "",
|
"spu_id": item.get("spu_id") or "",
|
||||||
"task_id": str(item.get("task_id") or ""),
|
"name": item.get("name") or "",
|
||||||
"task_name": item.get("name") or "",
|
"task_id": str(item.get("task_id") or ""),
|
||||||
"description": item.get("description") or "",
|
"task_name": item.get("name") or "",
|
||||||
"icon": item.get("icon") or "",
|
"description": item.get("description") or "",
|
||||||
"task_url": item.get("task_url") or "",
|
"icon": item.get("icon") or "",
|
||||||
"prizes": item.get("prizes") or [],
|
"task_url": item.get("task_url") or "",
|
||||||
"sort": index,
|
"prizes": item.get("prizes") or [],
|
||||||
})
|
"sort": index,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
if not candidates:
|
if not candidates:
|
||||||
self._mark_task(worker_db, task, "failed", "未从活动任务中发现充值商品", task_result)
|
self._mark_task(
|
||||||
|
worker_db, task, "failed", "未从活动任务中发现充值商品", task_result
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
now = datetime.now(timezone.utc)
|
now = datetime.now(timezone.utc)
|
||||||
@@ -228,11 +270,14 @@ class RechargeMixin:
|
|||||||
continue
|
continue
|
||||||
detail = detail_resp.to_dict()
|
detail = detail_resp.to_dict()
|
||||||
if detail_resp.code != 200 or not detail.get("sku_id"):
|
if detail_resp.code != 200 or not detail.get("sku_id"):
|
||||||
failed.append({
|
failed.append(
|
||||||
"spu_id": spu_id,
|
{
|
||||||
"message": detail_resp.message or f"商品详情获取失败: {detail_resp.code}",
|
"spu_id": spu_id,
|
||||||
"detail": detail,
|
"message": detail_resp.message
|
||||||
})
|
or f"商品详情获取失败: {detail_resp.code}",
|
||||||
|
"detail": detail,
|
||||||
|
}
|
||||||
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
item = {
|
item = {
|
||||||
@@ -241,7 +286,9 @@ class RechargeMixin:
|
|||||||
"spu_id": detail.get("spu_id") or spu_id,
|
"spu_id": detail.get("spu_id") or spu_id,
|
||||||
"sku_id": str(detail.get("sku_id") or ""),
|
"sku_id": str(detail.get("sku_id") or ""),
|
||||||
"name": detail.get("name") or candidate.get("name") or spu_id,
|
"name": detail.get("name") or candidate.get("name") or spu_id,
|
||||||
"description": detail.get("description") or candidate.get("description") or "",
|
"description": detail.get("description")
|
||||||
|
or candidate.get("description")
|
||||||
|
or "",
|
||||||
"icon": detail.get("icon") or candidate.get("icon") or "",
|
"icon": detail.get("icon") or candidate.get("icon") or "",
|
||||||
"task_id": candidate.get("task_id") or "",
|
"task_id": candidate.get("task_id") or "",
|
||||||
"task_name": candidate.get("task_name") or candidate.get("name") or "",
|
"task_name": candidate.get("task_name") or candidate.get("name") or "",
|
||||||
@@ -251,20 +298,22 @@ class RechargeMixin:
|
|||||||
|
|
||||||
worker_db.query(HuyaRechargeGoodsSnapshot).delete(synchronize_session=False)
|
worker_db.query(HuyaRechargeGoodsSnapshot).delete(synchronize_session=False)
|
||||||
for item in goods:
|
for item in goods:
|
||||||
worker_db.add(HuyaRechargeGoodsSnapshot(
|
worker_db.add(
|
||||||
spu_id=item["spu_id"],
|
HuyaRechargeGoodsSnapshot(
|
||||||
sku_id=item["sku_id"],
|
spu_id=item["spu_id"],
|
||||||
name=item["name"],
|
sku_id=item["sku_id"],
|
||||||
price=item.get("price") or None,
|
name=item["name"],
|
||||||
stock=item.get("stock") or None,
|
price=item.get("price") or None,
|
||||||
buy_limit=item.get("buy_limit") or None,
|
stock=item.get("stock") or None,
|
||||||
icon=item.get("icon") or "",
|
buy_limit=item.get("buy_limit") or None,
|
||||||
description=item.get("description") or "",
|
icon=item.get("icon") or "",
|
||||||
task_id=item.get("task_id") or "",
|
description=item.get("description") or "",
|
||||||
task_name=item.get("task_name") or "",
|
task_id=item.get("task_id") or "",
|
||||||
raw=item,
|
task_name=item.get("task_name") or "",
|
||||||
updated_at=now,
|
raw=item,
|
||||||
))
|
updated_at=now,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
account.status = "recharge_goods_refreshed"
|
account.status = "recharge_goods_refreshed"
|
||||||
account.updated_at = now
|
account.updated_at = now
|
||||||
@@ -279,7 +328,9 @@ class RechargeMixin:
|
|||||||
"failed": failed,
|
"failed": failed,
|
||||||
"task_detail": task_result,
|
"task_detail": task_result,
|
||||||
}
|
}
|
||||||
self._mark_task(worker_db, task, "success" if goods else "failed", message, result)
|
self._mark_task(
|
||||||
|
worker_db, task, "success" if goods else "failed", message, result
|
||||||
|
)
|
||||||
|
|
||||||
def _execute_create_recharge_order(
|
def _execute_create_recharge_order(
|
||||||
self,
|
self,
|
||||||
@@ -301,7 +352,9 @@ class RechargeMixin:
|
|||||||
|
|
||||||
count = self._to_int(self.payload.get("count")) or 1
|
count = self._to_int(self.payload.get("count")) or 1
|
||||||
count = max(1, min(count, 999))
|
count = max(1, min(count, 999))
|
||||||
pay_channel = self._normalize_pay_channel(self.payload.get("pay_channel") or config_info.get("pay_channel"))
|
pay_channel = self._normalize_pay_channel(
|
||||||
|
self.payload.get("pay_channel") or config_info.get("pay_channel")
|
||||||
|
)
|
||||||
|
|
||||||
uid = self._resolve_uid(account_info)
|
uid = self._resolve_uid(account_info)
|
||||||
if not uid:
|
if not uid:
|
||||||
@@ -313,15 +366,23 @@ class RechargeMixin:
|
|||||||
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
||||||
return
|
return
|
||||||
|
|
||||||
snapshot = worker_db.query(HuyaRechargeGoodsSnapshot).filter(
|
snapshot = (
|
||||||
HuyaRechargeGoodsSnapshot.spu_id == spu_id
|
worker_db.query(HuyaRechargeGoodsSnapshot)
|
||||||
).first()
|
.filter(HuyaRechargeGoodsSnapshot.spu_id == spu_id)
|
||||||
|
.first()
|
||||||
|
)
|
||||||
payload_sku_id = self._to_int(self.payload.get("sku_id"))
|
payload_sku_id = self._to_int(self.payload.get("sku_id"))
|
||||||
sku_id = payload_sku_id or self._to_int(snapshot.sku_id if snapshot else "")
|
sku_id = payload_sku_id or self._to_int(snapshot.sku_id if snapshot else "")
|
||||||
product_name = str(self.payload.get("product_name") or (snapshot.name if snapshot else "") or spu_id)
|
product_name = str(
|
||||||
|
self.payload.get("product_name")
|
||||||
|
or (snapshot.name if snapshot else "")
|
||||||
|
or spu_id
|
||||||
|
)
|
||||||
unit_price = int(snapshot.price or 0) if snapshot else 0
|
unit_price = int(snapshot.price or 0) if snapshot else 0
|
||||||
|
|
||||||
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}")
|
||||||
|
)
|
||||||
detail_resp = client.get_goods_info(
|
detail_resp = client.get_goods_info(
|
||||||
uid=uid,
|
uid=uid,
|
||||||
guid="",
|
guid="",
|
||||||
@@ -351,7 +412,13 @@ class RechargeMixin:
|
|||||||
product_name = detail.get("name") or product_name
|
product_name = detail.get("name") or product_name
|
||||||
unit_price = int(detail.get("price") or unit_price or 0)
|
unit_price = int(detail.get("price") or unit_price or 0)
|
||||||
if not sku_id:
|
if not sku_id:
|
||||||
self._mark_task(worker_db, task, "failed", "充值商品缺少 SKU,请先刷新充值商品列表", detail)
|
self._mark_task(
|
||||||
|
worker_db,
|
||||||
|
task,
|
||||||
|
"failed",
|
||||||
|
"充值商品缺少 SKU,请先刷新充值商品列表",
|
||||||
|
detail,
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
order_resp = client.create_order(
|
order_resp = client.create_order(
|
||||||
@@ -393,7 +460,13 @@ class RechargeMixin:
|
|||||||
item_count=count,
|
item_count=count,
|
||||||
)
|
)
|
||||||
if pay_resp is None:
|
if pay_resp is None:
|
||||||
self._mark_task(worker_db, task, "error", "虎牙支付接口无响应", {"goods": detail, "order": order_result})
|
self._mark_task(
|
||||||
|
worker_db,
|
||||||
|
task,
|
||||||
|
"error",
|
||||||
|
"虎牙支付接口无响应",
|
||||||
|
{"goods": detail, "order": order_result},
|
||||||
|
)
|
||||||
return
|
return
|
||||||
pay_result = pay_resp.to_dict()
|
pay_result = pay_resp.to_dict()
|
||||||
if pay_resp.code != 200 or not pay_resp.payUrl:
|
if pay_resp.code != 200 or not pay_resp.payUrl:
|
||||||
@@ -430,8 +503,12 @@ class RechargeMixin:
|
|||||||
account.status = "recharge_order_created"
|
account.status = "recharge_order_created"
|
||||||
account.updated_at = datetime.now(timezone.utc)
|
account.updated_at = datetime.now(timezone.utc)
|
||||||
message = f"{product_name} x{count} {self._pay_channel_label(pay_channel)} {result['amount_text']}"
|
message = f"{product_name} x{count} {self._pay_channel_label(pay_channel)} {result['amount_text']}"
|
||||||
self._update_task_progress(worker_db, task, "running", f"{message},等待扫码支付", result)
|
self._update_task_progress(
|
||||||
self._push_log("info", f"[{uid}] 已生成虎牙支付二维码,开始监听订单 {order_resp.orderId}")
|
worker_db, task, "running", f"{message},等待扫码支付", result
|
||||||
|
)
|
||||||
|
self._push_log(
|
||||||
|
"info", f"[{uid}] 已生成虎牙支付二维码,开始监听订单 {order_resp.orderId}"
|
||||||
|
)
|
||||||
|
|
||||||
payment_status, payment_order = self._wait_recharge_payment(
|
payment_status, payment_order = self._wait_recharge_payment(
|
||||||
client=client,
|
client=client,
|
||||||
@@ -451,10 +528,11 @@ class RechargeMixin:
|
|||||||
return
|
return
|
||||||
if payment_status == "stopped":
|
if payment_status == "stopped":
|
||||||
account.status = "recharge_order_created"
|
account.status = "recharge_order_created"
|
||||||
self._mark_task(worker_db, task, "stopped", f"{message},已停止监听支付", result)
|
self._mark_task(
|
||||||
|
worker_db, task, "stopped", f"{message},已停止监听支付", result
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
account.status = "recharge_order_created"
|
account.status = "recharge_order_created"
|
||||||
timeout_message = f"{message},{result['payment_status_label']}"
|
timeout_message = f"{message},{result['payment_status_label']}"
|
||||||
self._mark_task(worker_db, task, "success", timeout_message, result)
|
self._mark_task(worker_db, task, "success", timeout_message, result)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user