自动关闭虎牙支付成功弹窗
This commit is contained in:
@@ -20,6 +20,8 @@ from .huya_service import HUYA_CONFIG_FIELDS, cookie_value, ensure_huya_config,
|
||||
HUYA_RECHARGE_ACT_ID = 25135
|
||||
HUYA_RECHARGE_SOURCE_ID = "yellowcarlist"
|
||||
HUYA_RECHARGE_SCENE = 4
|
||||
HUYA_PAYMENT_POLL_SECONDS = 180
|
||||
HUYA_PAYMENT_POLL_INTERVAL = 3
|
||||
HUYA_RECHARGE_EXTRA_PRODUCTS = [
|
||||
{
|
||||
"spu_id": "hy-5879340",
|
||||
@@ -186,6 +188,110 @@ class HuyaBatchRunner:
|
||||
task.finished_at = datetime.now(timezone.utc)
|
||||
worker_db.commit()
|
||||
|
||||
def _update_task_progress(
|
||||
self,
|
||||
worker_db: Session,
|
||||
task: HuyaTask,
|
||||
status: str,
|
||||
message: str,
|
||||
result: Optional[dict] = None,
|
||||
):
|
||||
task.status = status
|
||||
task.message = message
|
||||
if result is not None:
|
||||
task.result = result
|
||||
worker_db.commit()
|
||||
|
||||
@staticmethod
|
||||
def _huya_order_status_label(status: int) -> str:
|
||||
from core.huya.shop_structs import OrderStatus
|
||||
|
||||
labels = {
|
||||
OrderStatus.DEPOSIT_WAIT_PAY: "待支付",
|
||||
OrderStatus.DEPOSIT_PAID: "已支付",
|
||||
OrderStatus.WAIT_DELIVER: "待发货",
|
||||
OrderStatus.WAIT_RECEIVE: "待收货",
|
||||
OrderStatus.FINISHED: "已完成",
|
||||
OrderStatus.FINISHED_CLOSED: "已关闭",
|
||||
OrderStatus.CANCELLED: "已取消",
|
||||
OrderStatus.BALANCE_WAIT_PAY: "尾款待支付",
|
||||
OrderStatus.CANCELLED_BALANCE_EXPIRED: "尾款超时取消",
|
||||
}
|
||||
return labels.get(int(status or 0), str(status or "未知"))
|
||||
|
||||
@classmethod
|
||||
def _is_huya_order_paid(cls, order) -> bool:
|
||||
from core.huya.shop_structs import OrderStatus
|
||||
|
||||
paid_statuses = {
|
||||
OrderStatus.DEPOSIT_PAID,
|
||||
OrderStatus.WAIT_DELIVER,
|
||||
OrderStatus.WAIT_RECEIVE,
|
||||
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
|
||||
|
||||
def _wait_recharge_payment(
|
||||
self,
|
||||
client: HuyaHttpClient,
|
||||
uid: int,
|
||||
guid: str,
|
||||
cookie: str,
|
||||
order_id: 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,
|
||||
)
|
||||
checked_at = datetime.now(timezone.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)
|
||||
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,
|
||||
})
|
||||
return "paid", last_order
|
||||
break
|
||||
else:
|
||||
result["payment_checked_at"] = checked_at
|
||||
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,
|
||||
})
|
||||
if last_order:
|
||||
result["payment_order"] = last_order
|
||||
return result["payment_status"], last_order
|
||||
|
||||
def _execute_query_points(
|
||||
self,
|
||||
worker_db: Session,
|
||||
@@ -745,13 +851,38 @@ class HuyaBatchRunner:
|
||||
"app_order_id": pay_resp.appOrderId,
|
||||
"pay_order_id": pay_resp.payOrderId,
|
||||
"pay_url": pay_resp.payUrl,
|
||||
"payment_status": "pending",
|
||||
"payment_status_label": "等待支付",
|
||||
"payment_paid": False,
|
||||
"goods": detail,
|
||||
"order": order_result,
|
||||
}
|
||||
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._mark_task(worker_db, task, "success", message, result)
|
||||
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,
|
||||
uid=uid,
|
||||
guid="",
|
||||
cookie=cookie,
|
||||
order_id=order_resp.orderId,
|
||||
result=result,
|
||||
)
|
||||
account.updated_at = datetime.now(timezone.utc)
|
||||
if payment_status == "paid":
|
||||
account.status = "recharge_paid"
|
||||
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)}"
|
||||
self._mark_task(worker_db, task, "success", paid_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)
|
||||
|
||||
def _execute_get_bind_qr(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user