feat(douyu): 外部单协作支持复制 CK
This commit is contained in:
@@ -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