Spaces:
Running
Running
增加打赏功能
Browse files- models.py +7 -1
- router_wallet.py +99 -1
models.py
CHANGED
|
@@ -105,4 +105,10 @@ class WithdrawRequest(BaseModel):
|
|
| 105 |
|
| 106 |
class PurchaseRequest(BaseModel):
|
| 107 |
account: str
|
| 108 |
-
item_id: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
class PurchaseRequest(BaseModel):
|
| 107 |
account: str
|
| 108 |
+
item_id: str
|
| 109 |
+
|
| 110 |
+
class TipRequest(BaseModel):
|
| 111 |
+
sender_account: str
|
| 112 |
+
target_account: str
|
| 113 |
+
amount: int
|
| 114 |
+
is_anonymous: bool
|
router_wallet.py
CHANGED
|
@@ -225,4 +225,102 @@ async def withdraw_earnings(req: WithdrawRequest, db: Session = Depends(get_db))
|
|
| 225 |
else:
|
| 226 |
raise HTTPException(status_code=400, detail=f"打款风控拦截: {result.get('sub_msg')}")
|
| 227 |
except Exception as e:
|
| 228 |
-
raise HTTPException(status_code=500, detail=f"提现接口异常: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
else:
|
| 226 |
raise HTTPException(status_code=400, detail=f"打款风控拦截: {result.get('sub_msg')}")
|
| 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 |
+
"""赞赏接口:处理扣费、收益及赞赏榜单记录"""
|
| 233 |
+
if req.sender_account == req.target_account:
|
| 234 |
+
raise HTTPException(status_code=400, detail="不能打赏自己")
|
| 235 |
+
if req.amount <= 0:
|
| 236 |
+
raise HTTPException(status_code=400, detail="打赏金额必须大于0")
|
| 237 |
+
|
| 238 |
+
users_db = json_db.load_data("users.json", default_data={})
|
| 239 |
+
if req.target_account not in users_db:
|
| 240 |
+
raise HTTPException(status_code=404, detail="目标用户不存在")
|
| 241 |
+
|
| 242 |
+
target_user = users_db[req.target_account]
|
| 243 |
+
tips_received = target_user.get("tips_received", {})
|
| 244 |
+
|
| 245 |
+
# 计算等级额度 (10分=1星, 50分=1月, 250分=1太阳。上限9太阳=2250分)
|
| 246 |
+
current_tip = tips_received.get(req.sender_account, {}).get("amount", 0)
|
| 247 |
+
if current_tip + req.amount > 2250:
|
| 248 |
+
raise HTTPException(status_code=400, detail=f"您对该用户的打赏已达上限 (9个太阳/2250积分),最多还能打赏 {2250 - current_tip} 积分")
|
| 249 |
+
|
| 250 |
+
# 1. 扣除打赏者余额 (悲观锁防并发)
|
| 251 |
+
sender_wallet = db.query(Wallet).filter(Wallet.account == req.sender_account).with_for_update().first()
|
| 252 |
+
if not sender_wallet or sender_wallet.balance < req.amount:
|
| 253 |
+
raise HTTPException(status_code=400, detail="积分余额不足,请先充值")
|
| 254 |
+
|
| 255 |
+
sender_wallet.balance -= req.amount
|
| 256 |
+
record_transaction(db, req.sender_account, "TIP_SEND", -req.amount, req.target_account)
|
| 257 |
+
|
| 258 |
+
# 2. 增加作者收益
|
| 259 |
+
target_wallet = db.query(Wallet).filter(Wallet.account == req.target_account).with_for_update().first()
|
| 260 |
+
if not target_wallet:
|
| 261 |
+
target_wallet = Wallet(account=req.target_account)
|
| 262 |
+
db.add(target_wallet)
|
| 263 |
+
target_wallet.earn_balance += req.amount
|
| 264 |
+
record_transaction(db, req.target_account, "TIP_RECEIVE", req.amount, req.sender_account)
|
| 265 |
+
|
| 266 |
+
# 3. 永久记录到该用户的榜单 JSON 中
|
| 267 |
+
tips_received[req.sender_account] = {
|
| 268 |
+
"amount": current_tip + req.amount,
|
| 269 |
+
"is_anonymous": req.is_anonymous,
|
| 270 |
+
"sender_name": users_db.get(req.sender_account, {}).get("name", req.sender_account)
|
| 271 |
+
}
|
| 272 |
+
target_user["tips_received"] = tips_received
|
| 273 |
+
|
| 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}
|