From ce971e2b36863c8e926e2dbdbd0705608e6bbc24 Mon Sep 17 00:00:00 2001 From: yml Date: Tue, 24 Nov 2026 15:58:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(login):=20=E7=99=BB=E5=BD=95=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E5=B1=95=E7=A4=BA=E8=A7=A6=E5=8F=91=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E4=B8=8E=E6=89=A7=E8=A1=8C=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 后端按批次/账号关联 LoginAttempt,返回 trigger_source/category/操作人 - 登录任务页增加“触发方式”“执行人”列 - 区分自动化/人工、首次登录/高价值重登/普通重登/账号检测 - 补充登录任务来源回归测试 --- tests/test_login_tasks_pagination.py | 27 ++++++++++++++- web/backend/routers/login.py | 40 ++++++++++++++++++++++- web/backend/schemas.py | 6 ++++ web/frontend/src/api/types.ts | 3 ++ web/frontend/src/pages/LoginTasksPage.tsx | 40 +++++++++++++++++++++++ 5 files changed, 114 insertions(+), 2 deletions(-) diff --git a/tests/test_login_tasks_pagination.py b/tests/test_login_tasks_pagination.py index fdefe8b..e674fb8 100644 --- a/tests/test_login_tasks_pagination.py +++ b/tests/test_login_tasks_pagination.py @@ -12,12 +12,14 @@ 走 ``/stop/{batch_id}``,不是删除。 """ +from datetime import UTC, datetime + import pytest from sqlalchemy.orm import sessionmaker import web.backend.database as database_module from tests.support.db import get_test_engine, make_test_session -from web.backend.models import Account, LoginTask, User +from web.backend.models import Account, LoginAttempt, LoginTask, User from web.backend.routers.login import delete_task, delete_tasks, list_tasks @@ -53,6 +55,17 @@ class TestTaskPagination: self.session.add(task) self.session.flush() self.task_ids.append(task.id) + self.session.add( + LoginAttempt( + account_id=self.account.id, + batch_id="paged-batch", + category="relogin_ordinary", + trigger_source="automation", + created_by=self.user.id, + status="success", + quota_day=datetime.now(UTC).date(), + ) + ) self.session.commit() def teardown_method(self): @@ -116,6 +129,18 @@ class TestTaskPagination: assert data["total"] == 12 + def test_login_task_exposes_trigger_source_and_operator(self): + db = self._fresh() + try: + data = list_tasks(page=1, page_size=20, db=db, current=self.user) + finally: + db.close() + item = data["items"][0] + assert item.trigger_source == "automation" + assert item.category == "relogin_ordinary" + assert item.created_by_username == "admin" + + class TestBulkDelete: def setup_method(self): self.engine = get_test_engine() diff --git a/web/backend/routers/login.py b/web/backend/routers/login.py index ed558ff..214ff34 100644 --- a/web/backend/routers/login.py +++ b/web/backend/routers/login.py @@ -19,7 +19,7 @@ from sqlalchemy.orm import Session from ..database import WorkerSessionLocal, get_db from ..deps import authenticate_websocket, get_current_user, require_permission -from ..models import Account, ExecutionJob, LoginTask, User +from ..models import Account, ExecutionJob, LoginAttempt, LoginTask, User from ..permissions import user_has_permission from ..schemas import LOGIN_BATCH_MAX_ACCOUNTS, LoginBatchRequest, LoginTaskOut from ..services.audit_service import record_audit @@ -276,10 +276,45 @@ def _serialize_tasks(db: Session, rows) -> list[LoginTaskOut]: ): accounts_map[int(row[0])] = row + # 登录任务记录本身不带来源;来源在 LoginAttempt(人工/自动化/首次登录/重登)。 + batch_ids = {str(t.attempt_batch_id or t.batch_id or "") for t in rows} + batch_ids.discard("") + attempt_map: dict[tuple[str, int], LoginAttempt] = {} + if batch_ids and account_ids: + for attempt in ( + db.query(LoginAttempt) + .filter( + LoginAttempt.batch_id.in_(batch_ids), + LoginAttempt.account_id.in_(account_ids), + ) + .order_by(LoginAttempt.id.asc()) + .all() + ): + attempt_map[(str(attempt.batch_id or ""), int(attempt.account_id))] = attempt + creator_ids = { + int(value) + for value in [ + *(t.created_by for t in rows), + *(attempt.created_by for attempt in attempt_map.values()), + ] + if value + } + creator_map = ( + { + int(user_id): str(username or "") + for user_id, username in db.query(User.id, User.username) + .filter(User.id.in_(creator_ids)) + .all() + } + if creator_ids + else {} + ) result = [] for t in rows: attempt_status = t.attempt_status or "" account = accounts_map.get(int(t.account_id)) + batch_id = str(t.attempt_batch_id or t.batch_id or "") + attempt = attempt_map.get((batch_id, int(t.account_id))) result.append( LoginTaskOut( id=t.id, @@ -299,6 +334,9 @@ def _serialize_tasks(db: Session, rows) -> list[LoginTaskOut]: or t.message or "", created_by=t.created_by, + created_by_username=creator_map.get(int(t.created_by), ""), + trigger_source=(str(attempt.trigger_source or "") if attempt else ""), + category=(str(attempt.category or "") if attempt else ""), created_at=t.created_at, finished_at=t.attempt_at or t.finished_at, ) diff --git a/web/backend/schemas.py b/web/backend/schemas.py index e992c81..a4e82d8 100644 --- a/web/backend/schemas.py +++ b/web/backend/schemas.py @@ -402,6 +402,9 @@ class LoginTaskOut(BaseModel): cookie: str = "" message: str = "" created_by: int + created_by_username: str = "" + trigger_source: str = "" + category: str = "" created_at: datetime | None = None finished_at: datetime | None = None @@ -425,6 +428,9 @@ class LoginTaskOut(BaseModel): "cookie": self.cookie, "message": self.message, "created_by": self.created_by, + "created_by_username": self.created_by_username, + "trigger_source": self.trigger_source, + "category": self.category, "created_at": _isoformat(self.created_at), "finished_at": _isoformat(self.finished_at), } diff --git a/web/frontend/src/api/types.ts b/web/frontend/src/api/types.ts index 82d764c..aec8b8c 100644 --- a/web/frontend/src/api/types.ts +++ b/web/frontend/src/api/types.ts @@ -486,6 +486,9 @@ export interface LoginTaskItem { cookie: string; message: string; created_by: number; + created_by_username: string; + trigger_source: string; + category: string; created_at: string | null; finished_at: string | null; } diff --git a/web/frontend/src/pages/LoginTasksPage.tsx b/web/frontend/src/pages/LoginTasksPage.tsx index 6f82d6e..88b343d 100644 --- a/web/frontend/src/pages/LoginTasksPage.tsx +++ b/web/frontend/src/pages/LoginTasksPage.tsx @@ -43,6 +43,31 @@ const STATUS_LABELS: Record = { account_auth_unknown: '认证状态未知', }; +const TRIGGER_SOURCE_LABELS: Record = { + automation: '自动化', + manual: '人工', +}; + +const LOGIN_CATEGORY_LABELS: Record = { + onboarding: '首次登录', + relogin_high_value: '高价值重登', + relogin_ordinary: '普通重登', + manual: '人工登录', + account_check: '账号检测', +}; + +function loginSourceLabel(task: LoginTaskItem) { + const source = TRIGGER_SOURCE_LABELS[task.trigger_source] || '未知'; + const category = LOGIN_CATEGORY_LABELS[task.category] || ''; + return category ? `${source} · ${category}` : source; +} + +function loginSourceColor(task: LoginTaskItem) { + if (task.trigger_source === 'automation') return 'blue'; + if (task.trigger_source === 'manual') return 'green'; + return 'default'; +} + const ACCOUNT_STATUS_LABELS: Record = { imported: '未登录', active: '正常', @@ -590,6 +615,21 @@ export default function LoginTasksPage() { width: 130, render: (name: string) => name || 未分配, }, + { + title: '触发方式', + dataIndex: 'trigger_source', + width: 170, + render: (_: string, record: LoginTaskItem) => ( + {loginSourceLabel(record)} + ), + }, + { + title: '执行人', + dataIndex: 'created_by_username', + width: 110, + render: (name: string, record: LoginTaskItem) => + name || `#${record.created_by}`, + }, { title: 'CK 状态', dataIndex: 'ck_check_status',