重构虎牙精英宝典协议与支付状态链路
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import copy
|
||||
import threading
|
||||
import time
|
||||
from datetime import UTC, datetime
|
||||
@@ -130,6 +131,7 @@ class HuyaBatchRunnerCore:
|
||||
task.result = result
|
||||
task.finished_at = datetime.now(UTC)
|
||||
worker_db.commit()
|
||||
self._push_task_event(task)
|
||||
|
||||
def _update_task_progress(
|
||||
self,
|
||||
@@ -144,6 +146,34 @@ class HuyaBatchRunnerCore:
|
||||
if result is not None:
|
||||
task.result = result
|
||||
worker_db.commit()
|
||||
self._push_task_event(task)
|
||||
|
||||
def _push_task_event(self, task: HuyaTask) -> None:
|
||||
"""向批次 WS 推送任务状态;二维码图片留给详情接口按需读取。"""
|
||||
if not self.log_queue or not self.loop:
|
||||
return
|
||||
result = copy.deepcopy(task.result) if isinstance(task.result, dict) else None
|
||||
if result and isinstance(result.get("mini_qrcode_image"), str):
|
||||
result.pop("mini_qrcode_image", None)
|
||||
result["has_mini_qrcode"] = True
|
||||
event = {
|
||||
"level": "task",
|
||||
"message": "",
|
||||
"task": {
|
||||
"id": task.id,
|
||||
"batch_id": task.batch_id,
|
||||
"account_id": task.account_id,
|
||||
"task_type": task.task_type,
|
||||
"handbook_scope": getattr(task, "handbook_scope", "legacy") or "legacy",
|
||||
"status": task.status or "",
|
||||
"message": task.message or "",
|
||||
"result": result,
|
||||
"created_by": task.created_by,
|
||||
"created_at": task.created_at.isoformat() if task.created_at else None,
|
||||
"finished_at": task.finished_at.isoformat() if task.finished_at else None,
|
||||
},
|
||||
}
|
||||
asyncio.run_coroutine_threadsafe(self.log_queue.put(event), self.loop)
|
||||
|
||||
|
||||
class HuyaBatchRegistry:
|
||||
|
||||
@@ -58,6 +58,12 @@ class GoodsMixin:
|
||||
return
|
||||
result = response.to_dict()
|
||||
result["act_id"] = act_id
|
||||
act_info = client.get_act_info(act_id=act_id)
|
||||
user_tasks = client.get_act_user_task_detail(uid=uid, cookie=cookie, act_id=act_id)
|
||||
if act_info is not None:
|
||||
result["act_info"] = act_info.to_dict()
|
||||
if user_tasks is not None:
|
||||
result["user_task_detail"] = user_tasks.to_dict()
|
||||
if response.status != 200:
|
||||
self._mark_task(
|
||||
worker_db,
|
||||
@@ -324,6 +330,44 @@ class GoodsMixin:
|
||||
client: Any = HuyaHttpClient(
|
||||
logger=lambda msg: self._push_log("info", f"[{uid}] {msg}")
|
||||
)
|
||||
detail = client.get_act_prize_detail(
|
||||
uid=uid, cookie=cookie, sid=sid_int, pid=product_id
|
||||
)
|
||||
if detail is None:
|
||||
self._mark_task(worker_db, task, "error", "虎牙兑换详情接口无响应")
|
||||
return
|
||||
detail_result = detail.to_dict()
|
||||
prize = detail.prize
|
||||
if detail.status != 200 or prize is None:
|
||||
self._mark_task(
|
||||
worker_db,
|
||||
task,
|
||||
"failed",
|
||||
detail.msg or f"虎牙兑换详情获取失败: {detail.status}",
|
||||
detail_result,
|
||||
)
|
||||
return
|
||||
now_ts = int(time.time())
|
||||
if prize.isCanExchange == 0:
|
||||
self._mark_task(worker_db, task, "failed", "该商品当前不可兑换", detail_result)
|
||||
return
|
||||
if prize.isShowNum and prize.leftNum <= 0:
|
||||
self._mark_task(worker_db, task, "failed", "该商品库存不足", detail_result)
|
||||
return
|
||||
if prize.exchangeStartTime and now_ts < prize.exchangeStartTime:
|
||||
self._mark_task(worker_db, task, "failed", "该商品尚未开始兑换", detail_result)
|
||||
return
|
||||
if prize.exchangeEndTime and now_ts > prize.exchangeEndTime:
|
||||
self._mark_task(worker_db, task, "failed", "该商品兑换已结束", detail_result)
|
||||
return
|
||||
score = client.query_user_score(uid=uid, cookie=cookie, sid=sid_int)
|
||||
if score is None:
|
||||
self._mark_task(worker_db, task, "error", "兑换前积分查询无响应", detail_result)
|
||||
return
|
||||
if score.status != 200 or score.available_score < prize.newScore:
|
||||
detail_result["score"] = score.to_dict()
|
||||
self._mark_task(worker_db, task, "failed", "可用积分不足", detail_result)
|
||||
return
|
||||
response = client.score_exchange_prize(
|
||||
uid=uid, cookie=cookie, sid=sid_int, pid=product_id
|
||||
)
|
||||
@@ -340,6 +384,7 @@ class GoodsMixin:
|
||||
"scheduled_at": scheduled_at.isoformat() if scheduled_at else "",
|
||||
"executed_at": datetime.now(UTC).isoformat(),
|
||||
"goods": snapshot.raw if snapshot else None,
|
||||
"prize_detail": detail_result,
|
||||
}
|
||||
)
|
||||
if response.status != 200:
|
||||
@@ -352,6 +397,13 @@ class GoodsMixin:
|
||||
)
|
||||
return
|
||||
|
||||
post_score = client.query_user_score(uid=uid, cookie=cookie, sid=sid_int)
|
||||
post_records = client.get_user_prize_records(uid=uid, cookie=cookie, sid=sid_int)
|
||||
if post_score is not None:
|
||||
result["post_exchange_score"] = post_score.to_dict()
|
||||
account.points = post_score.available_score
|
||||
if post_records is not None:
|
||||
result["post_exchange_records"] = post_records.to_dict()
|
||||
account.status = "goods_exchanged"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
message = response.msg or f"兑换成功: {product_name}"
|
||||
|
||||
@@ -86,52 +86,66 @@ class RechargeMixin:
|
||||
guid: str,
|
||||
cookie: str,
|
||||
order_id: int,
|
||||
sid: int,
|
||||
result: dict,
|
||||
) -> tuple[str, dict | None]:
|
||||
deadline = time.time() + HUYA_PAYMENT_POLL_SECONDS
|
||||
order_id_text = str(order_id)
|
||||
last_order = None
|
||||
while not self._stop.is_set() and time.time() < deadline:
|
||||
resp = client.query_user_order_list(
|
||||
uid=uid,
|
||||
guid=guid,
|
||||
cookie=cookie,
|
||||
offset=0,
|
||||
page_size=10,
|
||||
order_type=1,
|
||||
status=0,
|
||||
timeout=10.0,
|
||||
detail = client.order_detail(
|
||||
uid=uid, guid=guid, cookie=cookie, order_id=order_id, timeout=10.0
|
||||
)
|
||||
checked_at = datetime.now(UTC).isoformat()
|
||||
if resp is not None and getattr(resp, "orders", None):
|
||||
for order in resp.orders:
|
||||
if str(getattr(order, "orderId", "")) != order_id_text:
|
||||
continue
|
||||
last_order = order.to_dict()
|
||||
status = int(getattr(order, "orderStatus", 0) or 0)
|
||||
order = getattr(detail, "order", None) if detail is not None else None
|
||||
if order is not None and (
|
||||
not getattr(order, "orderId", 0)
|
||||
or str(getattr(order, "orderId", "")) == order_id_text
|
||||
):
|
||||
last_order = order.to_dict()
|
||||
status = int(getattr(order, "orderStatus", 0) or 0)
|
||||
result.update(
|
||||
{
|
||||
"payment_checked_at": checked_at,
|
||||
"payment_order": last_order,
|
||||
"payment_order_status": status,
|
||||
"payment_order_status_label": self._huya_order_status_label(status),
|
||||
}
|
||||
)
|
||||
if self._is_huya_order_paid(order):
|
||||
result.update(
|
||||
{
|
||||
"payment_checked_at": checked_at,
|
||||
"payment_order": last_order,
|
||||
"payment_order_status": status,
|
||||
"payment_order_status_label": self._huya_order_status_label(
|
||||
status
|
||||
),
|
||||
"payment_status": "paid",
|
||||
"payment_status_label": "已支付",
|
||||
"payment_paid": True,
|
||||
"payment_paid_at": checked_at,
|
||||
}
|
||||
)
|
||||
if self._is_huya_order_paid(order):
|
||||
result.update(
|
||||
{
|
||||
"payment_status": "paid",
|
||||
"payment_status_label": "已支付",
|
||||
"payment_paid": True,
|
||||
"payment_paid_at": checked_at,
|
||||
}
|
||||
)
|
||||
return "paid", last_order
|
||||
break
|
||||
return "paid", last_order
|
||||
else:
|
||||
result["payment_checked_at"] = checked_at
|
||||
# 兼容旧 HTTP 网关不提供 orderDetailV5 的情况。
|
||||
resp = client.query_user_order_list(
|
||||
uid=uid, guid=guid, cookie=cookie, offset=0, page_size=10,
|
||||
order_type=1, status=0, timeout=10.0,
|
||||
)
|
||||
for legacy_order in getattr(resp, "orders", []) if resp else []:
|
||||
if str(getattr(legacy_order, "orderId", "")) != order_id_text:
|
||||
continue
|
||||
last_order = legacy_order.to_dict()
|
||||
status = int(getattr(legacy_order, "orderStatus", 0) or 0)
|
||||
result.update({
|
||||
"payment_checked_at": checked_at,
|
||||
"payment_order": last_order,
|
||||
"payment_order_status": status,
|
||||
"payment_order_status_label": self._huya_order_status_label(status),
|
||||
})
|
||||
if self._is_huya_order_paid(legacy_order):
|
||||
result.update({
|
||||
"payment_status": "paid", "payment_status_label": "已支付",
|
||||
"payment_paid": True, "payment_paid_at": checked_at,
|
||||
})
|
||||
return "paid", last_order
|
||||
result.setdefault("payment_checked_at", checked_at)
|
||||
if self._stop.wait(HUYA_PAYMENT_POLL_INTERVAL):
|
||||
break
|
||||
|
||||
@@ -260,7 +274,7 @@ class RechargeMixin:
|
||||
pid=pid,
|
||||
spu_id=spu_id,
|
||||
sku_id=0,
|
||||
game_id="0",
|
||||
game_id="",
|
||||
source_id=HUYA_RECHARGE_SOURCE_ID,
|
||||
scene=HUYA_RECHARGE_SCENE,
|
||||
)
|
||||
@@ -389,7 +403,7 @@ class RechargeMixin:
|
||||
pid=pid,
|
||||
spu_id=spu_id,
|
||||
sku_id=sku_id or 0,
|
||||
game_id="0",
|
||||
game_id="",
|
||||
source_id=HUYA_RECHARGE_SOURCE_ID,
|
||||
scene=HUYA_RECHARGE_SCENE,
|
||||
)
|
||||
@@ -515,11 +529,18 @@ class RechargeMixin:
|
||||
guid="",
|
||||
cookie=cookie,
|
||||
order_id=order_resp.orderId,
|
||||
sid=self._to_int(config_info.get("sid")) or 2203,
|
||||
result=result,
|
||||
)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
if payment_status == "paid":
|
||||
account.status = "recharge_paid"
|
||||
post_score = client.query_user_score(
|
||||
uid=uid, cookie=cookie, sid=self._to_int(config_info.get("sid")) or 2203
|
||||
)
|
||||
if post_score is not None:
|
||||
result["post_payment_score"] = post_score.to_dict()
|
||||
account.points = post_score.available_score
|
||||
paid_message = f"支付成功: {product_name} x{count} {result['amount_text']}"
|
||||
if payment_order and payment_order.get("pay_time"):
|
||||
paid_message += f",支付时间 {self._format_local_time(int(payment_order['pay_time']) // 1000)}"
|
||||
@@ -534,4 +555,4 @@ class RechargeMixin:
|
||||
|
||||
account.status = "recharge_order_created"
|
||||
timeout_message = f"{message},{result['payment_status_label']}"
|
||||
self._mark_task(worker_db, task, "success", timeout_message, result)
|
||||
self._mark_task(worker_db, task, "timeout", timeout_message, result)
|
||||
|
||||
@@ -57,6 +57,13 @@ def apply_huya_config_defaults(config: HuyaConfig) -> bool:
|
||||
setattr(config, field, HUYA_CONFIG_DEFAULTS[field])
|
||||
changed = True
|
||||
continue
|
||||
if (
|
||||
field == "outer_act_id"
|
||||
and str(getattr(config, field, "") or "").strip() == "9504"
|
||||
):
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user