refactor(collab): 拆分斗鱼虎牙协作服务与页面配置
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
"""斗鱼外部单协作的账号凭据与用户名导出。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Sequence
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import DouyuExternalOrder, User
|
||||
from .audit_service import record_audit
|
||||
from .douyu_collab_common import (
|
||||
CK_EXPORT_ACTION,
|
||||
CK_EXPORT_BATCH_MAX,
|
||||
CK_LABELS,
|
||||
ORDER_STATUS_LABELS,
|
||||
USERNAME_EXPORT_BATCH_MAX,
|
||||
CollabError,
|
||||
account_by_id,
|
||||
account_by_username,
|
||||
)
|
||||
|
||||
|
||||
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
|
||||
|
||||
assert account is not None
|
||||
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_password": str(account.password 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,
|
||||
}
|
||||
|
||||
def export_usernames(
|
||||
db: Session,
|
||||
order_ids: Sequence[int],
|
||||
) -> dict[str, Any]:
|
||||
"""导出勾选订单关联的斗鱼用户名(去重、保持选择顺序)。
|
||||
|
||||
用户名不是凭据,能看列表的人本来就能看到,所以这里不额外要求 CK 导出权限,
|
||||
也不写审计;没有用户名的行会显式跳过。
|
||||
"""
|
||||
requested = list(dict.fromkeys(int(item) for item in order_ids if item))
|
||||
if not requested:
|
||||
raise CollabError("请先勾选要导出的行", code="username_export_empty")
|
||||
if len(requested) > USERNAME_EXPORT_BATCH_MAX:
|
||||
raise CollabError(
|
||||
f"一次最多导出 {USERNAME_EXPORT_BATCH_MAX} 行,请分批操作",
|
||||
code="username_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]] = []
|
||||
usernames: list[str] = []
|
||||
seen: set[str] = set()
|
||||
for order_id in requested:
|
||||
order = by_id.get(order_id)
|
||||
if order is None:
|
||||
skipped.append(
|
||||
{
|
||||
"id": order_id,
|
||||
"order_no": "",
|
||||
"reason": "协作单不存在或已删除",
|
||||
}
|
||||
)
|
||||
continue
|
||||
username = str(order.account_username or "").strip()
|
||||
if not username:
|
||||
account = account_by_id(db, order.account_id) or account_by_username(
|
||||
db, order.account_username
|
||||
)
|
||||
username = str(account.username if account else "").strip()
|
||||
if not username:
|
||||
skipped.append(
|
||||
{
|
||||
"id": int(order.id),
|
||||
"order_no": order.order_no,
|
||||
"reason": "这单还没填斗鱼用户名",
|
||||
}
|
||||
)
|
||||
continue
|
||||
items.append(
|
||||
{
|
||||
"id": int(order.id),
|
||||
"order_no": order.order_no,
|
||||
"account_username": username,
|
||||
}
|
||||
)
|
||||
if username not in seen:
|
||||
seen.add(username)
|
||||
usernames.append(username)
|
||||
return {
|
||||
"total": len(requested),
|
||||
"exported": len(items),
|
||||
"unique": len(usernames),
|
||||
"skipped": skipped,
|
||||
"items": items,
|
||||
"usernames": usernames,
|
||||
}
|
||||
Reference in New Issue
Block a user