Spaces:
Running
Running
Upload router_wallet.py
Browse files- router_wallet.py +3 -50
router_wallet.py
CHANGED
|
@@ -7,7 +7,8 @@ import hashlib
|
|
| 7 |
import os
|
| 8 |
from database_sql import get_db
|
| 9 |
from models_sql import Wallet, Transaction, Ownership
|
| 10 |
-
|
|
|
|
| 11 |
import 数据库连接 as json_db
|
| 12 |
from router_users import VERIFY_CODES
|
| 13 |
|
|
@@ -227,6 +228,7 @@ async def withdraw_earnings(req: WithdrawRequest, db: Session = Depends(get_db))
|
|
| 227 |
except Exception as e:
|
| 228 |
raise HTTPException(status_code=500, detail=f"提现接口异常: {str(e)}")
|
| 229 |
|
|
|
|
| 230 |
@router.post("/api/wallet/tip")
|
| 231 |
async def tip_user(req: TipRequest, db: Session = Depends(get_db)):
|
| 232 |
"""赞赏接口:处理扣费、收益及赞赏榜单记录"""
|
|
@@ -274,53 +276,4 @@ async def tip_user(req: TipRequest, db: Session = Depends(get_db)):
|
|
| 274 |
db.commit()
|
| 275 |
json_db.save_data("users.json", users_db)
|
| 276 |
|
| 277 |
-
return {"status": "success", "balance": sender_wallet.balance}
|
| 278 |
-
|
| 279 |
-
@router.post("/api/wallet/tip")
|
| 280 |
-
async def tip_user(req: TipRequest, db: Session = Depends(get_db)):
|
| 281 |
-
"""赞赏接口:处理扣费、收益及赞赏榜单记录"""
|
| 282 |
-
if req.sender_account == req.target_account:
|
| 283 |
-
raise HTTPException(status_code=400, detail="不能打赏自己")
|
| 284 |
-
if req.amount <= 0:
|
| 285 |
-
raise HTTPException(status_code=400, detail="打赏金额必须大于0")
|
| 286 |
-
|
| 287 |
-
users_db = json_db.load_data("users.json", default_data={})
|
| 288 |
-
if req.target_account not in users_db:
|
| 289 |
-
raise HTTPException(status_code=404, detail="目标用户不存在")
|
| 290 |
-
|
| 291 |
-
target_user = users_db[req.target_account]
|
| 292 |
-
tips_received = target_user.get("tips_received", {})
|
| 293 |
-
|
| 294 |
-
# 计算等级额度 (10分=1星, 50分=1月, 250分=1太阳。上限9太阳=2250分)
|
| 295 |
-
current_tip = tips_received.get(req.sender_account, {}).get("amount", 0)
|
| 296 |
-
if current_tip + req.amount > 2250:
|
| 297 |
-
raise HTTPException(status_code=400, detail=f"您对该用户的打赏已达上限 (9个太阳/2250积分),最多还能打赏 {2250 - current_tip} 积分")
|
| 298 |
-
|
| 299 |
-
# 1. 扣除打赏者余额 (悲观锁防并发)
|
| 300 |
-
sender_wallet = db.query(Wallet).filter(Wallet.account == req.sender_account).with_for_update().first()
|
| 301 |
-
if not sender_wallet or sender_wallet.balance < req.amount:
|
| 302 |
-
raise HTTPException(status_code=400, detail="积分余额不足,请先充值")
|
| 303 |
-
|
| 304 |
-
sender_wallet.balance -= req.amount
|
| 305 |
-
record_transaction(db, req.sender_account, "TIP_SEND", -req.amount, req.target_account)
|
| 306 |
-
|
| 307 |
-
# 2. 增加作者收益
|
| 308 |
-
target_wallet = db.query(Wallet).filter(Wallet.account == req.target_account).with_for_update().first()
|
| 309 |
-
if not target_wallet:
|
| 310 |
-
target_wallet = Wallet(account=req.target_account)
|
| 311 |
-
db.add(target_wallet)
|
| 312 |
-
target_wallet.earn_balance += req.amount
|
| 313 |
-
record_transaction(db, req.target_account, "TIP_RECEIVE", req.amount, req.sender_account)
|
| 314 |
-
|
| 315 |
-
# 3. 永久记录到该用户的榜单 JSON 中
|
| 316 |
-
tips_received[req.sender_account] = {
|
| 317 |
-
"amount": current_tip + req.amount,
|
| 318 |
-
"is_anonymous": req.is_anonymous,
|
| 319 |
-
"sender_name": users_db.get(req.sender_account, {}).get("name", req.sender_account)
|
| 320 |
-
}
|
| 321 |
-
target_user["tips_received"] = tips_received
|
| 322 |
-
|
| 323 |
-
db.commit()
|
| 324 |
-
json_db.save_data("users.json", users_db)
|
| 325 |
-
|
| 326 |
return {"status": "success", "balance": sender_wallet.balance}
|
|
|
|
| 7 |
import os
|
| 8 |
from database_sql import get_db
|
| 9 |
from models_sql import Wallet, Transaction, Ownership
|
| 10 |
+
# 【修复 1】:在这里补充导入了 TipRequest,防止云端崩溃!
|
| 11 |
+
from models import RechargeRequest, WithdrawRequest, PurchaseRequest, TipRequest
|
| 12 |
import 数据库连接 as json_db
|
| 13 |
from router_users import VERIFY_CODES
|
| 14 |
|
|
|
|
| 228 |
except Exception as e:
|
| 229 |
raise HTTPException(status_code=500, detail=f"提现接口异常: {str(e)}")
|
| 230 |
|
| 231 |
+
# 【修复 2】:去除了底部重复多余的旧代码,只保留最新正确的一个打赏接口
|
| 232 |
@router.post("/api/wallet/tip")
|
| 233 |
async def tip_user(req: TipRequest, db: Session = Depends(get_db)):
|
| 234 |
"""赞赏接口:处理扣费、收益及赞赏榜单记录"""
|
|
|
|
| 276 |
db.commit()
|
| 277 |
json_db.save_data("users.json", users_db)
|
| 278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
return {"status": "success", "balance": sender_wallet.balance}
|