type: 收窄虎牙绑定与充值执行器类型
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, TYPE_CHECKING
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -24,8 +25,28 @@ HUYA_RECHARGE_EXTRA_PRODUCTS = [
|
||||
]
|
||||
from .huya_runner_core import HUYA_RECHARGE_SOURCE_ID
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .huya_runner import HuyaBatchRunner
|
||||
|
||||
|
||||
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
|
||||
def _huya_order_status_label(status: int) -> str:
|
||||
from core.huya.shop_structs import OrderStatus
|
||||
@@ -43,8 +64,8 @@ class RechargeMixin:
|
||||
}
|
||||
return labels.get(int(status or 0), str(status or "未知"))
|
||||
|
||||
@classmethod
|
||||
def _is_huya_order_paid(cls, order) -> bool:
|
||||
@staticmethod
|
||||
def _is_huya_order_paid(order) -> bool:
|
||||
from core.huya.shop_structs import OrderStatus
|
||||
|
||||
paid_statuses = {
|
||||
@@ -54,11 +75,14 @@ class RechargeMixin:
|
||||
OrderStatus.FINISHED,
|
||||
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(
|
||||
self,
|
||||
client: HuyaHttpClient,
|
||||
client: Any,
|
||||
uid: int,
|
||||
guid: str,
|
||||
cookie: str,
|
||||
@@ -86,19 +110,25 @@ class RechargeMixin:
|
||||
continue
|
||||
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),
|
||||
})
|
||||
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_status": "paid",
|
||||
"payment_status_label": "已支付",
|
||||
"payment_paid": True,
|
||||
"payment_paid_at": checked_at,
|
||||
})
|
||||
result.update(
|
||||
{
|
||||
"payment_status": "paid",
|
||||
"payment_status_label": "已支付",
|
||||
"payment_paid": True,
|
||||
"payment_paid_at": checked_at,
|
||||
}
|
||||
)
|
||||
return "paid", last_order
|
||||
break
|
||||
else:
|
||||
@@ -106,12 +136,16 @@ class RechargeMixin:
|
||||
if self._stop.wait(HUYA_PAYMENT_POLL_INTERVAL):
|
||||
break
|
||||
|
||||
result.update({
|
||||
"payment_status": "timeout" if not self._stop.is_set() else "stopped",
|
||||
"payment_status_label": "等待支付超时" if not self._stop.is_set() else "已停止监听",
|
||||
"payment_paid": False,
|
||||
"payment_timeout_seconds": HUYA_PAYMENT_POLL_SECONDS,
|
||||
})
|
||||
result.update(
|
||||
{
|
||||
"payment_status": "timeout" if not self._stop.is_set() else "stopped",
|
||||
"payment_status_label": "等待支付超时"
|
||||
if not self._stop.is_set()
|
||||
else "已停止监听",
|
||||
"payment_paid": False,
|
||||
"payment_timeout_seconds": HUYA_PAYMENT_POLL_SECONDS,
|
||||
}
|
||||
)
|
||||
if last_order:
|
||||
result["payment_order"] = last_order
|
||||
return result["payment_status"], last_order
|
||||
@@ -157,8 +191,12 @@ class RechargeMixin:
|
||||
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
||||
return
|
||||
|
||||
client = HuyaHttpClient(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)
|
||||
client: Any = HuyaHttpClient(
|
||||
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:
|
||||
self._mark_task(worker_db, task, "error", "虎牙充值任务详情接口无响应")
|
||||
return
|
||||
@@ -190,20 +228,24 @@ class RechargeMixin:
|
||||
for index, item in enumerate(task_result.get("tasks", []), start=1):
|
||||
if int(item.get("task_type") or 0) != 67:
|
||||
continue
|
||||
add_candidate({
|
||||
"spu_id": item.get("spu_id") or "",
|
||||
"name": item.get("name") or "",
|
||||
"task_id": str(item.get("task_id") or ""),
|
||||
"task_name": item.get("name") or "",
|
||||
"description": item.get("description") or "",
|
||||
"icon": item.get("icon") or "",
|
||||
"task_url": item.get("task_url") or "",
|
||||
"prizes": item.get("prizes") or [],
|
||||
"sort": index,
|
||||
})
|
||||
add_candidate(
|
||||
{
|
||||
"spu_id": item.get("spu_id") or "",
|
||||
"name": item.get("name") or "",
|
||||
"task_id": str(item.get("task_id") or ""),
|
||||
"task_name": item.get("name") or "",
|
||||
"description": item.get("description") or "",
|
||||
"icon": item.get("icon") or "",
|
||||
"task_url": item.get("task_url") or "",
|
||||
"prizes": item.get("prizes") or [],
|
||||
"sort": index,
|
||||
}
|
||||
)
|
||||
|
||||
if not candidates:
|
||||
self._mark_task(worker_db, task, "failed", "未从活动任务中发现充值商品", task_result)
|
||||
self._mark_task(
|
||||
worker_db, task, "failed", "未从活动任务中发现充值商品", task_result
|
||||
)
|
||||
return
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
@@ -228,11 +270,14 @@ class RechargeMixin:
|
||||
continue
|
||||
detail = detail_resp.to_dict()
|
||||
if detail_resp.code != 200 or not detail.get("sku_id"):
|
||||
failed.append({
|
||||
"spu_id": spu_id,
|
||||
"message": detail_resp.message or f"商品详情获取失败: {detail_resp.code}",
|
||||
"detail": detail,
|
||||
})
|
||||
failed.append(
|
||||
{
|
||||
"spu_id": spu_id,
|
||||
"message": detail_resp.message
|
||||
or f"商品详情获取失败: {detail_resp.code}",
|
||||
"detail": detail,
|
||||
}
|
||||
)
|
||||
continue
|
||||
|
||||
item = {
|
||||
@@ -241,7 +286,9 @@ class RechargeMixin:
|
||||
"spu_id": detail.get("spu_id") or spu_id,
|
||||
"sku_id": str(detail.get("sku_id") or ""),
|
||||
"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 "",
|
||||
"task_id": candidate.get("task_id") 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)
|
||||
for item in goods:
|
||||
worker_db.add(HuyaRechargeGoodsSnapshot(
|
||||
spu_id=item["spu_id"],
|
||||
sku_id=item["sku_id"],
|
||||
name=item["name"],
|
||||
price=item.get("price") or None,
|
||||
stock=item.get("stock") or None,
|
||||
buy_limit=item.get("buy_limit") or None,
|
||||
icon=item.get("icon") or "",
|
||||
description=item.get("description") or "",
|
||||
task_id=item.get("task_id") or "",
|
||||
task_name=item.get("task_name") or "",
|
||||
raw=item,
|
||||
updated_at=now,
|
||||
))
|
||||
worker_db.add(
|
||||
HuyaRechargeGoodsSnapshot(
|
||||
spu_id=item["spu_id"],
|
||||
sku_id=item["sku_id"],
|
||||
name=item["name"],
|
||||
price=item.get("price") or None,
|
||||
stock=item.get("stock") or None,
|
||||
buy_limit=item.get("buy_limit") or None,
|
||||
icon=item.get("icon") or "",
|
||||
description=item.get("description") or "",
|
||||
task_id=item.get("task_id") or "",
|
||||
task_name=item.get("task_name") or "",
|
||||
raw=item,
|
||||
updated_at=now,
|
||||
)
|
||||
)
|
||||
|
||||
account.status = "recharge_goods_refreshed"
|
||||
account.updated_at = now
|
||||
@@ -279,7 +328,9 @@ class RechargeMixin:
|
||||
"failed": failed,
|
||||
"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(
|
||||
self,
|
||||
@@ -301,7 +352,9 @@ class RechargeMixin:
|
||||
|
||||
count = self._to_int(self.payload.get("count")) or 1
|
||||
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)
|
||||
if not uid:
|
||||
@@ -313,15 +366,23 @@ class RechargeMixin:
|
||||
self._mark_task(worker_db, task, "failed", "账号 Cookie 为空")
|
||||
return
|
||||
|
||||
snapshot = worker_db.query(HuyaRechargeGoodsSnapshot).filter(
|
||||
HuyaRechargeGoodsSnapshot.spu_id == spu_id
|
||||
).first()
|
||||
snapshot = (
|
||||
worker_db.query(HuyaRechargeGoodsSnapshot)
|
||||
.filter(HuyaRechargeGoodsSnapshot.spu_id == spu_id)
|
||||
.first()
|
||||
)
|
||||
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 "")
|
||||
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
|
||||
|
||||
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(
|
||||
uid=uid,
|
||||
guid="",
|
||||
@@ -351,7 +412,13 @@ class RechargeMixin:
|
||||
product_name = detail.get("name") or product_name
|
||||
unit_price = int(detail.get("price") or unit_price or 0)
|
||||
if not sku_id:
|
||||
self._mark_task(worker_db, task, "failed", "充值商品缺少 SKU,请先刷新充值商品列表", detail)
|
||||
self._mark_task(
|
||||
worker_db,
|
||||
task,
|
||||
"failed",
|
||||
"充值商品缺少 SKU,请先刷新充值商品列表",
|
||||
detail,
|
||||
)
|
||||
return
|
||||
|
||||
order_resp = client.create_order(
|
||||
@@ -393,7 +460,13 @@ class RechargeMixin:
|
||||
item_count=count,
|
||||
)
|
||||
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
|
||||
pay_result = pay_resp.to_dict()
|
||||
if pay_resp.code != 200 or not pay_resp.payUrl:
|
||||
@@ -430,8 +503,12 @@ class RechargeMixin:
|
||||
account.status = "recharge_order_created"
|
||||
account.updated_at = datetime.now(timezone.utc)
|
||||
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._push_log("info", f"[{uid}] 已生成虎牙支付二维码,开始监听订单 {order_resp.orderId}")
|
||||
self._update_task_progress(
|
||||
worker_db, task, "running", f"{message},等待扫码支付", result
|
||||
)
|
||||
self._push_log(
|
||||
"info", f"[{uid}] 已生成虎牙支付二维码,开始监听订单 {order_resp.orderId}"
|
||||
)
|
||||
|
||||
payment_status, payment_order = self._wait_recharge_payment(
|
||||
client=client,
|
||||
@@ -451,10 +528,11 @@ class RechargeMixin:
|
||||
return
|
||||
if payment_status == "stopped":
|
||||
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
|
||||
|
||||
account.status = "recharge_order_created"
|
||||
timeout_message = f"{message},{result['payment_status_label']}"
|
||||
self._mark_task(worker_db, task, "success", timeout_message, result)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user