实现虎牙绑定小程序码展示

This commit is contained in:
yml2213
2026-07-04 19:05:35 +08:00
parent 728706deb2
commit 7e92008f7b
6 changed files with 837 additions and 17 deletions
+116 -2
View File
@@ -141,6 +141,117 @@ class HuyaBatchRunner:
account.updated_at = datetime.now(timezone.utc)
self._mark_task(worker_db, task, "success", f"积分: {points}", result)
def _execute_get_bind_qr(
self,
worker_db: Session,
task: HuyaTask,
account: HuyaAccount,
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()
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}")
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 = HuyaHttpClient(logger=lambda msg: self._push_log("info", f"[{uid}] {msg}"))
bind_status = client.check_user_bind_game_account(
uid=uid,
cookie=cookie,
b_act_id=b_act_id_int,
is_use_outer_act_id=1,
)
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_status.to_dict(),
)
return
live_link = client.get_live_link_param(
uid=uid,
cookie=cookie,
b_act_id=b_act_id_int,
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 ""
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,
)
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,
"mini_qrcode_image": mini_qrcode["mini_qrcode_image"],
"qrcode_token": mini_qrcode["qrcode_token"],
"bind_status": bind_status.to_dict(),
"profile": {
"nick": profile_nick,
"avatar": profile_avatar,
},
}
account.status = "bind_qr_generated"
account.game_name = bind_status.gameName or account.game_name
account.nickname = profile_nick or account.nickname
account.updated_at = datetime.now(timezone.utc)
self._mark_task(worker_db, task, "success", "已生成绑定小程序码", result)
def _execute_one(self, task_id: int, account_info: dict, config_info: dict, total: int):
worker_db = SessionLocal()
try:
@@ -165,13 +276,16 @@ class HuyaBatchRunner:
name = self._account_name(account_info)
self._push_log("info", f"[{current}/{total}] 开始虎牙任务: {name}")
if self.task_type != "query_points":
if self.task_type not in {"query_points", "get_bind_qr"}:
self._mark_task(worker_db, task, "failed", "该虎牙任务执行器暂未实现")
self._push_log("warning", f"[{current}] {name} 暂未实现: {self.task_type}")
return
try:
self._execute_query_points(worker_db, task, account, account_info, config_info)
if self.task_type == "query_points":
self._execute_query_points(worker_db, task, account, account_info, config_info)
elif self.task_type == "get_bind_qr":
self._execute_get_bind_qr(worker_db, task, account, account_info, config_info)
worker_db.refresh(task)
if task.status == "success":
self._push_log("success", f"[{current}] {name} {task.message}")
+4
View File
@@ -32,6 +32,10 @@ def apply_huya_config_defaults(config: HuyaConfig) -> bool:
"""补齐虎牙配置默认值,返回是否发生变更。"""
changed = False
for field in HUYA_CONFIG_FIELDS:
if field == "bind_act_id" and str(getattr(config, field, "") or "").strip() == "17096":
setattr(config, field, HUYA_CONFIG_DEFAULTS[field])
changed = True
continue
normalized = huya_config_value(field, getattr(config, field, None))
if getattr(config, field, None) != normalized:
setattr(config, field, normalized)