feat(douyu): 外部单协作支持复制 CK
This commit is contained in:
@@ -31,10 +31,12 @@ PERMISSIONS = {
|
||||
# 斗鱼活动
|
||||
"douyu:task": "斗鱼任务管理",
|
||||
"douyu:config": "斗鱼配置管理",
|
||||
# 斗鱼外部单协作(订单挂内部账号,不复制 CK)
|
||||
# 斗鱼外部单协作(订单挂内部账号,导出时实时读取账号 CK)
|
||||
"douyu:collab:view": "查看斗鱼外部单协作",
|
||||
"douyu:collab:edit": "编辑斗鱼外部单协作",
|
||||
"douyu:collab:manage": "删除斗鱼外部单协作",
|
||||
# CK 是凭据:导到剪贴板 = 把凭据带出系统,单独一个点,默认只给运营
|
||||
"douyu:collab:ck_export": "导出斗鱼外部单协作账号的 CK(写剪贴板)",
|
||||
"yyb:session": "应用宝扫码登录与角色查询",
|
||||
"yyb:recharge": "应用宝充值与付款",
|
||||
"yyb:manage": "接管并操作他人的应用宝任务",
|
||||
@@ -94,6 +96,7 @@ ROLE_PERMISSIONS = {
|
||||
"douyu:collab:view",
|
||||
"douyu:collab:edit",
|
||||
"douyu:collab:manage",
|
||||
"douyu:collab:ck_export",
|
||||
"yyb:session",
|
||||
"yyb:recharge",
|
||||
"yyb:manage",
|
||||
|
||||
@@ -19,7 +19,7 @@ from fastapi import (
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
from ..deps import authenticate_websocket, require_permission
|
||||
from ..deps import authenticate_websocket, get_current_user, require_permission
|
||||
from ..models import User
|
||||
from ..permissions import get_user_permissions
|
||||
from ..schemas import (
|
||||
@@ -27,6 +27,8 @@ from ..schemas import (
|
||||
DouyuCollabAccountLookupRequest,
|
||||
DouyuCollabAccountRequest,
|
||||
DouyuCollabArchiveRequest,
|
||||
DouyuCollabCkExportOut,
|
||||
DouyuCollabCkExportRequest,
|
||||
DouyuCollabGoodsOut,
|
||||
DouyuCollabImagesRequest,
|
||||
DouyuCollabMetricOut,
|
||||
@@ -331,6 +333,42 @@ def verify_external_orders_batch(
|
||||
return {"results": results, "success": all(item["success"] for item in results)}
|
||||
|
||||
|
||||
@router.post("/external-orders/ck/export", response_model=DouyuCollabCkExportOut)
|
||||
def export_external_orders_ck(
|
||||
req: DouyuCollabCkExportRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("douyu:collab:ck_export")),
|
||||
):
|
||||
"""勾选多行导出关联内部账号的 CK 明文(前端写剪贴板)。
|
||||
|
||||
斗鱼协作单不存 CK,导出的始终是关联账号的实时 ``cookie``。逐行写审计,
|
||||
但审计里只记单号/账号/CK 状态,不把 CK 本体写进审计。
|
||||
"""
|
||||
try:
|
||||
return collab.export_cks(db, current, req.ids)
|
||||
except collab.CollabError as exc:
|
||||
raise _http_error(exc) from exc
|
||||
|
||||
|
||||
@router.post(
|
||||
"/external-orders/{order_id}/ck/copy",
|
||||
response_model=DouyuCollabCkExportOut,
|
||||
)
|
||||
def copy_external_order_ck(
|
||||
order_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(get_current_user),
|
||||
):
|
||||
"""复制单行 CK 明文(仅超级管理员;与虎牙协作台账口径一致)。"""
|
||||
if current.role != "super_admin":
|
||||
raise HTTPException(status_code=403, detail="仅超级管理员可复制 CK")
|
||||
try:
|
||||
order = collab.get_order(db, order_id)
|
||||
return collab.export_cks(db, current, [order.id])
|
||||
except collab.CollabError as exc:
|
||||
raise _http_error(exc) from exc
|
||||
|
||||
|
||||
@router.post("/external-orders/{order_id}/activity/query")
|
||||
def query_external_order_activity(
|
||||
order_id: int,
|
||||
|
||||
@@ -1870,6 +1870,42 @@ class DouyuCollabArchiveRequest(BaseModel):
|
||||
reason: str = Field("", max_length=256)
|
||||
|
||||
|
||||
class DouyuCollabCkExportRequest(BaseModel):
|
||||
"""勾选多行,导出关联内部账号的 CK 明文(一次最多 50 条)。"""
|
||||
|
||||
ids: list[int] = Field(default_factory=list)
|
||||
|
||||
|
||||
class DouyuCollabCkExportItemOut(BaseModel):
|
||||
"""导出结果里的一行:``ck_text`` 是明文凭据,只走这个接口、不留日志。"""
|
||||
|
||||
id: int
|
||||
order_no: str = ""
|
||||
account_id: int | None = None
|
||||
account_username: str = ""
|
||||
account_nickname: str = ""
|
||||
status: str = ""
|
||||
status_label: str = ""
|
||||
ck_status: str = ""
|
||||
ck_status_label: str = ""
|
||||
ck_text: str = ""
|
||||
|
||||
|
||||
class DouyuCollabCkExportSkippedOut(BaseModel):
|
||||
id: int
|
||||
order_no: str = ""
|
||||
reason: str = ""
|
||||
|
||||
|
||||
class DouyuCollabCkExportOut(BaseModel):
|
||||
"""导出 CK 的结果:``exported + len(skipped) = total``。"""
|
||||
|
||||
total: int
|
||||
exported: int
|
||||
skipped: list[DouyuCollabCkExportSkippedOut] = Field(default_factory=list)
|
||||
items: list[DouyuCollabCkExportItemOut] = Field(default_factory=list)
|
||||
|
||||
|
||||
class DouyuCollabOrderOut(BaseModel):
|
||||
id: int
|
||||
order_no: str
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from collections.abc import Mapping, Sequence
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
@@ -104,6 +104,8 @@ POINTS_GROUP_STATUSES: tuple[str, ...] = (
|
||||
POINTS_THRESHOLD_DEFAULT = 100
|
||||
POINTS_THRESHOLD_MAX = 1_000_000
|
||||
MAX_IMAGES = 20
|
||||
CK_EXPORT_BATCH_MAX = 50
|
||||
CK_EXPORT_ACTION = "douyu:collab:ck_export"
|
||||
|
||||
VERDICT_SUCCESS = "success"
|
||||
VERDICT_FAILED = "failed"
|
||||
@@ -1986,3 +1988,99 @@ def delete_order(
|
||||
db.delete(order)
|
||||
db.commit()
|
||||
return {"success": True, "order_no": order_no}
|
||||
|
||||
|
||||
def export_cks(
|
||||
db: Session,
|
||||
current: User,
|
||||
order_ids: Sequence[int],
|
||||
*,
|
||||
commit: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
"""导出协作单关联内部账号的 **CK 明文**(前端写剪贴板用)。
|
||||
|
||||
斗鱼协作单本身不存 CK,导出的始终是关联 ``accounts.cookie`` 的实时值。
|
||||
和虎牙台账保持同一口径:
|
||||
|
||||
1. 需要独立权限点 ``douyu:collab:ck_export``;
|
||||
2. 每条成功导出都写审计,但审计里只记单号/账号/CK 状态,**不记 CK 本体**;
|
||||
3. 未绑定账号、账号没有 CK、订单已删除的行显式跳过并返回原因。
|
||||
"""
|
||||
requested = list(dict.fromkeys(int(item) for item in order_ids if item))
|
||||
if not requested:
|
||||
raise CollabError("请先勾选要导出的行", code="ck_export_empty")
|
||||
if len(requested) > CK_EXPORT_BATCH_MAX:
|
||||
raise CollabError(
|
||||
f"一次最多导出 {CK_EXPORT_BATCH_MAX} 行,请分批操作",
|
||||
code="ck_export_too_large",
|
||||
)
|
||||
|
||||
rows = (
|
||||
db.query(DouyuExternalOrder).filter(DouyuExternalOrder.id.in_(requested)).all()
|
||||
)
|
||||
by_id = {int(order.id): order for order in rows}
|
||||
items: list[dict[str, Any]] = []
|
||||
skipped: list[dict[str, Any]] = []
|
||||
for order_id in requested:
|
||||
order = by_id.get(order_id)
|
||||
if order is None:
|
||||
skipped.append(
|
||||
{
|
||||
"id": order_id,
|
||||
"order_no": "",
|
||||
"reason": "协作单不存在或已删除",
|
||||
}
|
||||
)
|
||||
continue
|
||||
account = account_by_id(db, order.account_id) or account_by_username(
|
||||
db, order.account_username
|
||||
)
|
||||
ck_text = str(account.cookie or "").strip() if account else ""
|
||||
if not ck_text:
|
||||
reason = (
|
||||
"这单还没绑定斗鱼账号" if account is None else "关联的斗鱼账号还没有 CK"
|
||||
)
|
||||
skipped.append(
|
||||
{
|
||||
"id": int(order.id),
|
||||
"order_no": order.order_no,
|
||||
"reason": reason,
|
||||
}
|
||||
)
|
||||
continue
|
||||
|
||||
ck_status = str(account.ck_check_status or "")
|
||||
items.append(
|
||||
{
|
||||
"id": int(order.id),
|
||||
"order_no": order.order_no,
|
||||
"account_id": int(account.id),
|
||||
"account_username": account.username or order.account_username or "",
|
||||
"account_nickname": account.nickname or order.account_nickname or "",
|
||||
"status": order.status,
|
||||
"status_label": ORDER_STATUS_LABELS.get(order.status, order.status),
|
||||
"ck_status": ck_status,
|
||||
"ck_status_label": CK_LABELS.get(ck_status, ""),
|
||||
"ck_text": ck_text,
|
||||
}
|
||||
)
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action=CK_EXPORT_ACTION,
|
||||
target=f"douyu_external_order:{order.id}",
|
||||
detail={
|
||||
"order_no": order.order_no,
|
||||
"account_username": account.username or order.account_username or "",
|
||||
"ck_status": ck_status,
|
||||
"batch_size": len(requested),
|
||||
},
|
||||
)
|
||||
if commit:
|
||||
db.commit()
|
||||
return {
|
||||
"total": len(requested),
|
||||
"exported": len(items),
|
||||
"skipped": skipped,
|
||||
"items": items,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user