feat(huya): 精英宝典工作台支持导入外部 CK
- 新增 POST /api/huya/workbench-accounts/import-cookies(huya:task + huya:import): 外部 CK 复用现有解析入库后直接加入当前用户的工作台集合,导入完即可在工作台执行 「生成开通支付码/绑定二维码/查询角色/刷新积分」等操作 - 新账号自动 assigned_to=导入者:客服的账号列表按归属过滤,不设归属会「导入成功但看不到」 - import_huya_cookies_detailed 返回账号对象供工作台复用,原 import_huya_cookies 行为不变 - 工作台账号卡片新增「导入外部 CK」入口(huya:import 权限控制)与导入弹窗 验证:新增 tests/test_huya_workbench_import_ck.py(入库、归属、工作台集合、重复导入幂等、 全脏数据不建记录);按 AGENTS.md 做了反向验证(关掉归属赋值后该用例变红,还原后恢复)。 前端 tsc -b 与全量 pytest 通过。
This commit is contained in:
@@ -39,6 +39,7 @@ from .workbench import (
|
||||
create_task_batch,
|
||||
get_config,
|
||||
get_task,
|
||||
import_workbench_cookies,
|
||||
list_goods,
|
||||
list_huya_workbench_accounts,
|
||||
list_recharge_goods,
|
||||
@@ -79,6 +80,7 @@ __all__ = [
|
||||
"get_task",
|
||||
"import_cookies",
|
||||
"import_password_accounts",
|
||||
"import_workbench_cookies",
|
||||
"list_accounts",
|
||||
"list_goods",
|
||||
"list_huya_account_ids",
|
||||
|
||||
@@ -89,6 +89,7 @@ from ...schemas import (
|
||||
HuyaTaskOut,
|
||||
HuyaTestSmsUrlRequest,
|
||||
HuyaWorkbenchAccountsUpdate,
|
||||
HuyaWorkbenchCookieImport,
|
||||
)
|
||||
from ...services.audit_service import record_audit
|
||||
from ...services.huya_exchange_targets import (
|
||||
@@ -111,6 +112,7 @@ from ...services.huya_service import (
|
||||
ensure_huya_config,
|
||||
huya_config_value,
|
||||
import_huya_cookies,
|
||||
import_huya_cookies_detailed,
|
||||
import_huya_password_accounts,
|
||||
rebuild_web_cookie_from_raw,
|
||||
save_huya_login_cookie_to_account,
|
||||
|
||||
@@ -384,3 +384,70 @@ async def ws_huya_logs(websocket: WebSocket, batch_id: str):
|
||||
latest = huya_batch_registry.get(batch_id)
|
||||
if latest and latest.get("finished"):
|
||||
huya_batch_registry.pop(batch_id)
|
||||
|
||||
|
||||
@router.post("/workbench-accounts/import-cookies")
|
||||
def import_workbench_cookies(
|
||||
req: HuyaWorkbenchCookieImport,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("huya:task")),
|
||||
):
|
||||
"""导入外部 CK:入库为虎牙账号并直接加入当前用户的工作台。
|
||||
|
||||
导入后的账号会出现在工作台账号列表里,可照常执行「生成开通支付码 / 绑定二维码 /
|
||||
查询角色 / 刷新积分」等操作。没有归属的新账号自动归属到导入者,否则客服看不到。
|
||||
"""
|
||||
_require_huya_perm(current, "huya:import")
|
||||
accounts, skipped = import_huya_cookies_detailed(
|
||||
db, req.text, req.tag, assign_to=current.id
|
||||
)
|
||||
imported_ids = sorted({account.id for account in accounts})
|
||||
if imported_ids:
|
||||
workbench = (
|
||||
db.query(HuyaWorkbench)
|
||||
.filter(
|
||||
HuyaWorkbench.user_id == current.id,
|
||||
HuyaWorkbench.handbook_scope == req.handbook_scope,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if workbench is None:
|
||||
db.add(HuyaWorkbench(user_id=current.id, handbook_scope=req.handbook_scope))
|
||||
else:
|
||||
workbench.updated_at = datetime.now(UTC)
|
||||
existing_ids = {
|
||||
account_id
|
||||
for (account_id,) in db.query(HuyaWorkbenchAccount.account_id)
|
||||
.filter(
|
||||
HuyaWorkbenchAccount.user_id == current.id,
|
||||
HuyaWorkbenchAccount.handbook_scope == req.handbook_scope,
|
||||
)
|
||||
.all()
|
||||
}
|
||||
db.add_all(
|
||||
[
|
||||
HuyaWorkbenchAccount(
|
||||
user_id=current.id,
|
||||
handbook_scope=req.handbook_scope,
|
||||
account_id=account_id,
|
||||
)
|
||||
for account_id in imported_ids
|
||||
if account_id not in existing_ids
|
||||
]
|
||||
)
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action="huya:workbench:import_ck",
|
||||
target=f"huya_workbench:{req.handbook_scope}",
|
||||
detail={"count": len(imported_ids), "skipped": skipped, "tag": req.tag},
|
||||
)
|
||||
db.commit()
|
||||
|
||||
return {
|
||||
"message": f"导入 {len(imported_ids)} 条,跳过 {skipped} 条",
|
||||
"success": True,
|
||||
"count": len(imported_ids),
|
||||
"skipped": skipped,
|
||||
"account_ids": _workbench_account_ids(db, current, req.handbook_scope),
|
||||
}
|
||||
|
||||
@@ -657,6 +657,14 @@ class HuyaWorkbenchAccountsUpdate(BaseModel):
|
||||
account_ids: list[int] = Field(default_factory=list)
|
||||
|
||||
|
||||
class HuyaWorkbenchCookieImport(BaseModel):
|
||||
"""工作台「导入外部 CK」:把外部 Cookie 入库并直接加入自己的工作台。"""
|
||||
|
||||
text: str = Field(..., min_length=1)
|
||||
tag: str = ""
|
||||
handbook_scope: str = Field("elite", pattern="^elite$|^browser_exchange$")
|
||||
|
||||
|
||||
class HuyaBrowserExchangeRequest(BaseModel):
|
||||
"""浏览器兑换的目标页选择(常量见 services/huya_exchange_targets.py)。"""
|
||||
|
||||
|
||||
@@ -349,9 +349,16 @@ def upsert_huya_cookie(
|
||||
return account
|
||||
|
||||
|
||||
def import_huya_cookies(db: Session, text: str, tag: str = "") -> tuple[int, int]:
|
||||
"""导入虎牙 Cookie,返回 (成功数, 跳过数)。"""
|
||||
created_or_updated = 0
|
||||
def import_huya_cookies_detailed(
|
||||
db: Session, text: str, tag: str = "", assign_to: int | None = None
|
||||
) -> tuple[list[HuyaAccount], int]:
|
||||
"""导入虎牙 Cookie,返回 (账号列表, 跳过数)。
|
||||
|
||||
与 ``import_huya_cookies`` 的差别:把每个账号对象交回调用方 —— 工作台
|
||||
「导入外部 CK」需要拿到刚导入的账号加入自己的工作台集合;``assign_to``
|
||||
用于给尚无归属的新账号打上导入者(否则客服看不到自己导入的账号)。
|
||||
"""
|
||||
accounts: list[HuyaAccount] = []
|
||||
skipped = 0
|
||||
tag = (tag or "").strip()
|
||||
|
||||
@@ -361,12 +368,22 @@ def import_huya_cookies(db: Session, text: str, tag: str = "") -> tuple[int, int
|
||||
skipped += 1
|
||||
continue
|
||||
|
||||
_upsert_huya_account(db, parsed, tag=tag)
|
||||
created_or_updated += 1
|
||||
account = _upsert_huya_account(db, parsed, tag=tag)
|
||||
if assign_to and account.assigned_to is None:
|
||||
account.assigned_to = assign_to
|
||||
accounts.append(account)
|
||||
|
||||
if created_or_updated:
|
||||
if accounts:
|
||||
db.commit()
|
||||
return created_or_updated, skipped
|
||||
for account in accounts:
|
||||
db.refresh(account)
|
||||
return accounts, skipped
|
||||
|
||||
|
||||
def import_huya_cookies(db: Session, text: str, tag: str = "") -> tuple[int, int]:
|
||||
"""导入虎牙 Cookie,返回 (成功数, 跳过数)。"""
|
||||
accounts, skipped = import_huya_cookies_detailed(db, text, tag)
|
||||
return len(accounts), skipped
|
||||
|
||||
|
||||
def persist_huya_session_cookie(
|
||||
|
||||
Reference in New Issue
Block a user