Spaces:
Running
Running
Upload 5 files
Browse files- router_items.py +1 -1
- router_wallet.py +13 -8
router_items.py
CHANGED
|
@@ -77,7 +77,7 @@ async def get_items(type: str = "tool", sort: str = "time", limit: int = 50): #
|
|
| 77 |
return {"status": "success", "data": filtered_items[:limit]}
|
| 78 |
|
| 79 |
@router.get("/api/creators")
|
| 80 |
-
async def get_creators(sort: str = "downloads", limit: int =
|
| 81 |
"""
|
| 82 |
获取创作者列表
|
| 83 |
🚀 P1性能优化:预构建 author->items 索引,避免 N+1 查询
|
|
|
|
| 77 |
return {"status": "success", "data": filtered_items[:limit]}
|
| 78 |
|
| 79 |
@router.get("/api/creators")
|
| 80 |
+
async def get_creators(sort: str = "downloads", limit: int = 100):
|
| 81 |
"""
|
| 82 |
获取创作者列表
|
| 83 |
🚀 P1性能优化:预构建 author->items 索引,避免 N+1 查询
|
router_wallet.py
CHANGED
|
@@ -178,39 +178,44 @@ async def check_order(order_id: str, account: str = None, db: Session = Depends(
|
|
| 178 |
tx = db.query(Transaction).filter(Transaction.tx_id == order_id).first()
|
| 179 |
if tx:
|
| 180 |
return {"status": "SUCCESS"}
|
| 181 |
-
|
| 182 |
# 防线 2:主动向支付宝发起查单(解决 Hugging Face 收不到回调的绝杀技)
|
| 183 |
if alipay and account:
|
| 184 |
try:
|
| 185 |
# 拿着订单号去问支付宝:这笔钱到底付了没?
|
| 186 |
result = alipay.api_alipay_trade_query(out_trade_no=order_id)
|
| 187 |
if result.get("code") == "10000" and result.get("trade_status") in ("TRADE_SUCCESS", "TRADE_FINISHED"):
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
# 钱真的到了!立刻加锁入账
|
| 190 |
amount = int(float(result.get("total_amount", 0)))
|
| 191 |
-
|
| 192 |
wallet = db.query(Wallet).filter(Wallet.account == account).with_for_update().first()
|
| 193 |
if not wallet:
|
| 194 |
wallet = Wallet(account=account, balance=0, earn_balance=0, tip_balance=0, frozen_balance=0)
|
| 195 |
db.add(wallet)
|
| 196 |
-
|
| 197 |
wallet.balance = (wallet.balance or 0) + amount
|
| 198 |
-
|
| 199 |
last_tx = db.query(Transaction).filter(Transaction.account == account).order_by(Transaction.created_at.desc()).first()
|
| 200 |
prev_hash = last_tx.tx_hash if last_tx else "GENESIS_HASH"
|
| 201 |
tx_hash = calculate_tx_hash(order_id, account, "RECHARGE", amount, prev_hash)
|
| 202 |
-
|
| 203 |
new_tx = Transaction(
|
| 204 |
tx_id=order_id, account=account, tx_type="RECHARGE", amount=amount,
|
| 205 |
prev_hash=prev_hash, tx_hash=tx_hash
|
| 206 |
)
|
| 207 |
db.add(new_tx)
|
| 208 |
db.commit()
|
| 209 |
-
|
| 210 |
return {"status": "SUCCESS"}
|
| 211 |
except Exception as e:
|
| 212 |
print(f"主动查单发生异常: {e}")
|
| 213 |
-
|
| 214 |
# 如果没查到支付成功状态,继续让前端等
|
| 215 |
return {"status": "PENDING"}
|
| 216 |
|
|
|
|
| 178 |
tx = db.query(Transaction).filter(Transaction.tx_id == order_id).first()
|
| 179 |
if tx:
|
| 180 |
return {"status": "SUCCESS"}
|
| 181 |
+
|
| 182 |
# 防线 2:主动向支付宝发起查单(解决 Hugging Face 收不到回调的绝杀技)
|
| 183 |
if alipay and account:
|
| 184 |
try:
|
| 185 |
# 拿着订单号去问支付宝:这笔钱到底付了没?
|
| 186 |
result = alipay.api_alipay_trade_query(out_trade_no=order_id)
|
| 187 |
if result.get("code") == "10000" and result.get("trade_status") in ("TRADE_SUCCESS", "TRADE_FINISHED"):
|
| 188 |
+
|
| 189 |
+
# 🔒 防重检查:支付宝查单成功后,再次检查是否已被回调处理过
|
| 190 |
+
existing_check = db.query(Transaction).filter(Transaction.tx_id == order_id).first()
|
| 191 |
+
if existing_check:
|
| 192 |
+
return {"status": "SUCCESS"} # 已被回调入账,不重复处理
|
| 193 |
+
|
| 194 |
# 钱真的到了!立刻加锁入账
|
| 195 |
amount = int(float(result.get("total_amount", 0)))
|
| 196 |
+
|
| 197 |
wallet = db.query(Wallet).filter(Wallet.account == account).with_for_update().first()
|
| 198 |
if not wallet:
|
| 199 |
wallet = Wallet(account=account, balance=0, earn_balance=0, tip_balance=0, frozen_balance=0)
|
| 200 |
db.add(wallet)
|
| 201 |
+
|
| 202 |
wallet.balance = (wallet.balance or 0) + amount
|
| 203 |
+
|
| 204 |
last_tx = db.query(Transaction).filter(Transaction.account == account).order_by(Transaction.created_at.desc()).first()
|
| 205 |
prev_hash = last_tx.tx_hash if last_tx else "GENESIS_HASH"
|
| 206 |
tx_hash = calculate_tx_hash(order_id, account, "RECHARGE", amount, prev_hash)
|
| 207 |
+
|
| 208 |
new_tx = Transaction(
|
| 209 |
tx_id=order_id, account=account, tx_type="RECHARGE", amount=amount,
|
| 210 |
prev_hash=prev_hash, tx_hash=tx_hash
|
| 211 |
)
|
| 212 |
db.add(new_tx)
|
| 213 |
db.commit()
|
| 214 |
+
|
| 215 |
return {"status": "SUCCESS"}
|
| 216 |
except Exception as e:
|
| 217 |
print(f"主动查单发生异常: {e}")
|
| 218 |
+
|
| 219 |
# 如果没查到支付成功状态,继续让前端等
|
| 220 |
return {"status": "PENDING"}
|
| 221 |
|