From 435bd2d81a63b2e2b212a23eed78f39d47adbe9e Mon Sep 17 00:00:00 2001 From: ddrwode <34234@3来 34> Date: Fri, 27 Feb 2026 15:22:28 +0800 Subject: [PATCH] haha --- API文档.md | 13 +++++++------ server/api/tasks.py | 15 ++++++++------- server/models.py | 1 - server/serializers.py | 2 +- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/API文档.md b/API文档.md index 39d5291..17af2ec 100644 --- a/API文档.md +++ b/API文档.md @@ -358,7 +358,7 @@ Set-Cookie: auth_token=a1b2c3d4e5f6...; HttpOnly; Max-Age=31536000; SameSite=Lax | 状态码 | 说明 | |--------|------| | 201 | 成功,任务已创建并派发 | -| 400 | 未指定 worker_id 或 account_name | +| 400 | 未指定 id、worker_id 或 account_name | | 401 | 未登录或 token 失效 | | 404 | 未找到拥有该浏览器环境的在线 Worker | | 503 | Worker 不在线 / WebSocket 连接不存在 / 派发失败 | @@ -368,17 +368,18 @@ Set-Cookie: auth_token=a1b2c3d4e5f6...; HttpOnly; Max-Age=31536000; SameSite=Lax | 参数 | 类型 | 必填 | 说明 | |------|------|------|------| | task_type | string | 是 | 任务类型:`check_login`(检查登录)、`boss_recruit`(招聘) | -| worker_id | string | 否 | 目标 Worker ID(与 account_name 二选一) | +| id | int | 否 | 账号 ID(直接指定,推荐) | +| worker_id | string | 否 | 目标 Worker ID | | account_name | string | 否 | 浏览器环境名称(系统自动查找对应 Worker) | | params | object | 否 | 任务附加参数,默认 `{}` | -**路由规则**: `worker_id` 优先;若未指定则通过 `account_name` 自动查找对应的在线 Worker。 +**路由规则**: `id` 优先,根据账号 ID 自动解析电脑和环境;否则 `worker_id` 或 `account_name`。 -**请求示例 — 检查登录**: +**请求示例 — 检查登录(推荐用 id)**: ```json { "task_type": "check_login", - "account_name": "第一个" + "id": 2 } ``` @@ -415,7 +416,7 @@ Set-Cookie: auth_token=a1b2c3d4e5f6...; HttpOnly; Max-Age=31536000; SameSite=Lax | 状态码 | 说明 | |--------|------| -| 400 | 未指定 worker_id 或 account_name | +| 400 | 未指定 id、worker_id 或 account_name | | 401 | 未登录或 token 失效 | | 404 | 未找到拥有该浏览器环境的在线 Worker | | 503 | Worker 不在线 / WebSocket 连接不存在 / 派发失败 | diff --git a/server/api/tasks.py b/server/api/tasks.py index 41e1163..a12d2cb 100644 --- a/server/api/tasks.py +++ b/server/api/tasks.py @@ -65,18 +65,19 @@ def task_list(request): ser.is_valid(raise_exception=True) validated = ser.validated_data.copy() - boss_id_raw = validated.pop("boss_id", "") or "" + account_id = validated.pop("id", None) req = TaskCreate(**validated) target_worker_id = req.worker_id or "" account_name = req.account_name or "" - # 未指定 worker_id/account_name 时,用 boss_id 自动解析 - if not target_worker_id and not account_name and boss_id_raw: - account = BossAccount.objects.filter(boss_id=boss_id_raw.strip()).first() - if not account: + # 指定 id 时,直接从数据库解析 worker_id 和 account_name + if account_id: + try: + account = BossAccount.objects.get(pk=account_id) + except BossAccount.DoesNotExist: return api_error( http_status.HTTP_404_NOT_FOUND, - f"未找到 boss_id 为 '{boss_id_raw}' 的账号", + f"未找到 id={account_id} 的账号", ) target_worker_id = account.worker_id account_name = account.browser_name @@ -93,7 +94,7 @@ def task_list(request): req.worker_id = target_worker_id if not target_worker_id: - return api_error(http_status.HTTP_400_BAD_REQUEST, "请指定 worker_id、account_name 或 boss_id") + return api_error(http_status.HTTP_400_BAD_REQUEST, "请指定 id、worker_id 或 account_name") if not worker_manager.is_online(target_worker_id): return api_error(http_status.HTTP_503_SERVICE_UNAVAILABLE, f"Worker {target_worker_id} 不在线") diff --git a/server/models.py b/server/models.py index 54daa98..1e63695 100644 --- a/server/models.py +++ b/server/models.py @@ -200,7 +200,6 @@ class TaskCreate(BaseModel): task_type: TaskType worker_id: Optional[str] = None account_name: Optional[str] = None - boss_id: Optional[str] = None params: Dict[str, Any] = {} diff --git a/server/serializers.py b/server/serializers.py index 363dfc1..1f0c4dd 100644 --- a/server/serializers.py +++ b/server/serializers.py @@ -37,9 +37,9 @@ class AccountBindSerializer(serializers.Serializer): class TaskCreateSerializer(serializers.Serializer): """提交任务请求。""" task_type = serializers.CharField(max_length=64) + id = serializers.IntegerField(required=False, allow_null=True, default=None) # 账号 ID,直接指定 worker_id = serializers.CharField(max_length=64, required=False, allow_blank=True, default="") account_name = serializers.CharField(max_length=128, required=False, allow_blank=True, default="") - boss_id = serializers.CharField(max_length=128, required=False, allow_blank=True, default="") params = serializers.JSONField(required=False, default=dict)