35 lines
1.4 KiB
Python
35 lines
1.4 KiB
Python
"""虎牙支付短链:把「开通支付码」的二维码内容换成可扫的短链。
|
|
|
|
手机扫码时没有登录态(不是当前浏览器),所以这条路由**不带鉴权依赖**,
|
|
只校验 `huya_pay_link.pay_qr_path` 里那个 HMAC 签名;签名不对或任务里没有
|
|
支付地址就 404。除了跳到支付宝/微信的支付地址,这里不返回任何数据。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi.responses import RedirectResponse
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ...database import get_db
|
|
from ...models import HuyaTask
|
|
from ...services.huya_pay_link import verify_pay_token
|
|
|
|
router = APIRouter(tags=["虎牙"])
|
|
|
|
|
|
@router.get("/pay/{task_id}")
|
|
def redirect_huya_pay(task_id: int, k: str = "", db: Session = Depends(get_db)):
|
|
"""302 到该任务真实的下单支付地址(供手机扫码后打开)。"""
|
|
if not verify_pay_token(task_id, k):
|
|
raise HTTPException(status_code=404, detail="支付链接无效")
|
|
|
|
task = db.query(HuyaTask).filter(HuyaTask.id == int(task_id)).first()
|
|
pay_url = str(
|
|
((task.result or {}) if task is not None else {}).get("pay_url") or ""
|
|
)
|
|
if not pay_url.startswith(("http://", "https://")):
|
|
raise HTTPException(status_code=404, detail="支付链接不存在")
|
|
|
|
return RedirectResponse(pay_url, status_code=302)
|