feat(weibo): 增加外部 CK 账号 CRUD 与生命周期
This commit is contained in:
@@ -16,7 +16,9 @@ from ..deps import get_current_user
|
||||
from ..models import User, WeiboAccount
|
||||
from ..permissions import user_has_permission
|
||||
from ..schemas import (
|
||||
WeiboAccountDisableRequest,
|
||||
WeiboAccountOut,
|
||||
WeiboAccountUpdate,
|
||||
WeiboBindQrOut,
|
||||
WeiboBindSchemeOut,
|
||||
WeiboCookieImport,
|
||||
@@ -38,6 +40,8 @@ from ..schemas import (
|
||||
from ..services.audit_service import record_audit
|
||||
from ..services.weibo_service import (
|
||||
create_weibo_recharge_order,
|
||||
disable_weibo_account,
|
||||
enable_weibo_account,
|
||||
exchange_weibo_goods,
|
||||
generate_weibo_bind_schemes,
|
||||
import_weibo_cookies,
|
||||
@@ -46,6 +50,7 @@ from ..services.weibo_service import (
|
||||
query_weibo_payment_status,
|
||||
query_weibo_point_history,
|
||||
refresh_weibo_account,
|
||||
update_weibo_account_metadata,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api/weibo", tags=["微博微豆"])
|
||||
@@ -81,6 +86,11 @@ def _account_out(account: WeiboAccount) -> WeiboAccountOut:
|
||||
return WeiboAccountOut.model_validate(account)
|
||||
|
||||
|
||||
def _ensure_enabled(account: WeiboAccount) -> None:
|
||||
if not account.is_enabled:
|
||||
raise HTTPException(status_code=409, detail="微博账号已停用,请先启用")
|
||||
|
||||
|
||||
def _refresh_or_http(account: WeiboAccount, db: Session):
|
||||
try:
|
||||
return refresh_weibo_account(
|
||||
@@ -128,6 +138,7 @@ def _record_audit_safely(
|
||||
def list_accounts(
|
||||
search: str = Query(""),
|
||||
bind_status: str = Query(""),
|
||||
enabled: bool | None = Query(None),
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(20, ge=1, le=200),
|
||||
db: Session = Depends(get_db),
|
||||
@@ -146,8 +157,11 @@ def list_accounts(
|
||||
WeiboAccount.nickname.ilike(pattern),
|
||||
WeiboAccount.game_role_name.ilike(pattern),
|
||||
WeiboAccount.tag.ilike(pattern),
|
||||
WeiboAccount.remark.ilike(pattern),
|
||||
)
|
||||
)
|
||||
if isinstance(enabled, bool):
|
||||
query = query.filter(WeiboAccount.is_enabled.is_(enabled))
|
||||
bind_value = _query_str(bind_status)
|
||||
if bind_value == "unbound":
|
||||
query = query.filter(WeiboAccount.is_bound.is_(False))
|
||||
@@ -175,6 +189,95 @@ def list_accounts(
|
||||
}
|
||||
|
||||
|
||||
@router.get("/accounts/{account_id}", response_model=WeiboAccountOut)
|
||||
def get_account(
|
||||
account_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""查看一个微博账号的本地资料。"""
|
||||
|
||||
_require_weibo_perm(current, "weibo:view")
|
||||
return _account_out(_get_account(db, account_id))
|
||||
|
||||
|
||||
@router.put("/accounts/{account_id}", response_model=WeiboAccountOut)
|
||||
def update_account(
|
||||
account_id: int,
|
||||
req: WeiboAccountUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""更新账号的人工标签和备注。"""
|
||||
|
||||
_require_weibo_perm(current, "weibo:manage")
|
||||
account = _get_account(db, account_id)
|
||||
payload = req.model_dump(exclude_unset=True)
|
||||
changes = update_weibo_account_metadata(
|
||||
db,
|
||||
account,
|
||||
tag=payload.get("tag"),
|
||||
remark=payload.get("remark"),
|
||||
)
|
||||
if changes:
|
||||
_record_audit_safely(
|
||||
db,
|
||||
current,
|
||||
action="weibo:account:update",
|
||||
target=f"weibo_account:{account.id}",
|
||||
detail={"account_id": account.id, "uid": account.uid, "changes": changes},
|
||||
)
|
||||
return _account_out(account)
|
||||
|
||||
|
||||
@router.post("/accounts/{account_id}/disable", response_model=WeiboAccountOut)
|
||||
def disable_account(
|
||||
account_id: int,
|
||||
req: WeiboAccountDisableRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""停用账号,保留 CK 和历史资料。"""
|
||||
|
||||
_require_weibo_perm(current, "weibo:manage")
|
||||
account = _get_account(db, account_id)
|
||||
disable_weibo_account(db, account, reason=req.reason)
|
||||
_record_audit_safely(
|
||||
db,
|
||||
current,
|
||||
action="weibo:account:disable",
|
||||
target=f"weibo_account:{account.id}",
|
||||
detail={
|
||||
"account_id": account.id,
|
||||
"uid": account.uid,
|
||||
"reason": account.disabled_reason,
|
||||
},
|
||||
)
|
||||
return _account_out(account)
|
||||
|
||||
|
||||
@router.post("/accounts/{account_id}/enable", response_model=WeiboAccountOut)
|
||||
def enable_account(
|
||||
account_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""重新检测 CK 与支付风险,通过后启用账号。"""
|
||||
|
||||
_require_weibo_perm(current, "weibo:manage")
|
||||
account = _get_account(db, account_id)
|
||||
_refresh_or_http(account, db)
|
||||
enable_weibo_account(db, account)
|
||||
_record_audit_safely(
|
||||
db,
|
||||
current,
|
||||
action="weibo:account:enable",
|
||||
target=f"weibo_account:{account.id}",
|
||||
detail={"account_id": account.id, "uid": account.uid},
|
||||
)
|
||||
return _account_out(account)
|
||||
|
||||
|
||||
@router.post("/accounts/import-cookies", response_model=WeiboImportResult)
|
||||
def import_cookies(
|
||||
req: WeiboCookieImport,
|
||||
@@ -231,6 +334,7 @@ def refresh_account(
|
||||
|
||||
_require_weibo_perm(current, "weibo:view")
|
||||
account = _get_account(db, account_id)
|
||||
_ensure_enabled(account)
|
||||
_refresh_or_http(account, db)
|
||||
_record_audit_safely(
|
||||
db,
|
||||
@@ -252,6 +356,7 @@ def create_bind_qrcodes(
|
||||
|
||||
_require_weibo_perm(current, "weibo:bind")
|
||||
account = _get_account(db, account_id)
|
||||
_ensure_enabled(account)
|
||||
snapshot = _refresh_or_http(account, db)
|
||||
if snapshot.is_bound and not snapshot.can_rebind:
|
||||
detail = (
|
||||
@@ -314,6 +419,7 @@ def list_mall_goods(
|
||||
|
||||
_require_weibo_perm(current, "weibo:view")
|
||||
account = _get_account(db, account_id)
|
||||
_ensure_enabled(account)
|
||||
try:
|
||||
data = query_weibo_mall(
|
||||
db,
|
||||
@@ -366,6 +472,7 @@ def exchange_goods(
|
||||
if not req.confirm:
|
||||
raise HTTPException(status_code=400, detail="请先确认兑换商品")
|
||||
account = _get_account(db, account_id)
|
||||
_ensure_enabled(account)
|
||||
try:
|
||||
result = exchange_weibo_goods(
|
||||
db,
|
||||
@@ -423,6 +530,7 @@ def list_exchange_records(
|
||||
|
||||
_require_weibo_perm(current, "weibo:view")
|
||||
account = _get_account(db, account_id)
|
||||
_ensure_enabled(account)
|
||||
try:
|
||||
data = query_weibo_exchange_records(
|
||||
db,
|
||||
@@ -471,6 +579,7 @@ def list_point_history(
|
||||
|
||||
_require_weibo_perm(current, "weibo:view")
|
||||
account = _get_account(db, account_id)
|
||||
_ensure_enabled(account)
|
||||
try:
|
||||
data = query_weibo_point_history(
|
||||
db,
|
||||
@@ -520,6 +629,7 @@ def create_recharge_order(
|
||||
|
||||
_require_weibo_perm(current, "weibo:recharge")
|
||||
account = _get_account(db, account_id)
|
||||
_ensure_enabled(account)
|
||||
try:
|
||||
order = create_weibo_recharge_order(
|
||||
db,
|
||||
@@ -567,6 +677,7 @@ def get_recharge_status(
|
||||
|
||||
_require_weibo_perm(current, "weibo:recharge")
|
||||
account = _get_account(db, account_id)
|
||||
_ensure_enabled(account)
|
||||
try:
|
||||
status = query_weibo_payment_status(db, account, pay_id=req.pay_id)
|
||||
except (WeiboError, ValueError) as exc:
|
||||
|
||||
Reference in New Issue
Block a user