diff --git a/tests/test_platform_fulfillment.py b/tests/test_platform_fulfillment.py index 94f276d..20d3504 100644 --- a/tests/test_platform_fulfillment.py +++ b/tests/test_platform_fulfillment.py @@ -69,6 +69,19 @@ class _Base: views.invalidate_task_list_count_cache() self.session.close() + def complete_customer_profile(self, task: PlatformFulfillmentTask) -> None: + task.customer_role_name = "小淋笨" + task.customer_platform = "wechat" + task.customer_system = "android" + task.customer_home_images = [ + { + "url": "https://example.com/game-home.png", + "thumbnailUrl": "https://example.com/game-home-thumb.png", + "objectKey": "game-home.png", + } + ] + self.session.commit() + def account( self, name: str, @@ -3732,6 +3745,7 @@ class TestBindingAndCompletion(_Base): def test_customer_start_queues_allocation_without_sync_network(self, monkeypatch): task = self.task() + self.complete_customer_profile(task) calls: list[list[int]] = [] monkeypatch.setattr( "web.backend.services.platform_fulfillment.auto_allocator.wake_auto_allocator", @@ -3753,6 +3767,7 @@ class TestBindingAndCompletion(_Base): def test_customer_start_creates_one_binding_task_and_links_platform(self, monkeypatch): task = self._allocated() + self.complete_customer_profile(task) monkeypatch.setattr( service.binding, "enqueue_execution_job", @@ -3778,6 +3793,7 @@ class TestBindingAndCompletion(_Base): def test_customer_started_binding_does_not_capture_request_loop(self, monkeypatch): task = self._allocated() + self.complete_customer_profile(task) captured: dict[str, object] = {} monkeypatch.setattr( service.binding, @@ -3790,6 +3806,17 @@ class TestBindingAndCompletion(_Base): assert captured["job_type"] == "douyu_binding" assert captured["batch_id"].startswith(f"pfbind-{task.id}-") + def test_customer_start_requires_game_home_profile(self): + task = self._allocated() + + with pytest.raises(HTTPException) as exc_info: + asyncio.run( + service.customer_start_binding(self.session, task_id=task.id) + ) + + assert exc_info.value.status_code == 409 + assert "游戏主页截图" in str(exc_info.value.detail) + def test_concurrent_start_binding_creates_one_task(self, monkeypatch): task = self._allocated() monkeypatch.setattr( diff --git a/web/backend/services/platform_fulfillment/auto_allocator.py b/web/backend/services/platform_fulfillment/auto_allocator.py index a0b5d56..461bbc7 100644 --- a/web/backend/services/platform_fulfillment/auto_allocator.py +++ b/web/backend/services/platform_fulfillment/auto_allocator.py @@ -10,13 +10,13 @@ from datetime import UTC, datetime, timedelta from typing import Any from loguru import logger -from sqlalchemy import or_ +from sqlalchemy import func, or_ from sqlalchemy.orm import Session from ...database import WorkerSessionLocal from ...models import PlatformFulfillmentTask from . import service -from .common import FLOW_VERSION_CLEAN +from .common import FLOW_VERSION_CLEAN, _has_game_home_profile DEFAULT_INTERVAL_SECONDS = 20 DEFAULT_MIN_INTERVAL_SECONDS = 5 @@ -194,6 +194,8 @@ def run_auto_allocation_once( if allocated_task is not None: allocated_task.next_allocate_at = None db.commit() + if not _has_game_home_profile(allocated_task): + continue try: service.start_binding_sync( db, @@ -280,6 +282,10 @@ def start_allocated_bindings_once( PlatformFulfillmentTask.binding_status == "not_started", PlatformFulfillmentTask.account_id.isnot(None), PlatformFulfillmentTask.lease_id.isnot(None), + PlatformFulfillmentTask.customer_role_name != "", + PlatformFulfillmentTask.customer_platform != "", + PlatformFulfillmentTask.customer_system != "", + func.json_length(PlatformFulfillmentTask.customer_home_images) > 0, ) .order_by( PlatformFulfillmentTask.updated_at.asc(), PlatformFulfillmentTask.id.asc() @@ -292,6 +298,8 @@ def start_allocated_bindings_once( actor = service._integration_actor(db) started = 0 for task in rows: + if not _has_game_home_profile(task): + continue try: service.start_binding_sync( db, diff --git a/web/backend/services/platform_fulfillment/common.py b/web/backend/services/platform_fulfillment/common.py index 0ed122c..f6de4e5 100644 --- a/web/backend/services/platform_fulfillment/common.py +++ b/web/backend/services/platform_fulfillment/common.py @@ -141,6 +141,21 @@ def _order_snapshot_from_event(payload: dict[str, Any]) -> dict[str, Any]: return data if isinstance(data, dict) else payload +def _has_game_home_profile(task: PlatformFulfillmentTask) -> bool: + """Clean 任务必须同时有游戏角色、登录方式、系统和至少一张主页截图。""" + images = task.customer_home_images or [] + return bool( + str(task.customer_role_name or "").strip() + and str(task.customer_platform or "").strip() in {"wechat", "qq"} + and str(task.customer_system or "").strip() in {"ios", "android"} + and isinstance(images, list) + and any( + isinstance(item, dict) and str(item.get("url") or "").strip() + for item in images + ) + ) + + def _order_match_started_at(db: Session) -> datetime | None: row = ( db.query(PlatformFulfillmentSetting) diff --git a/web/backend/services/platform_fulfillment/customer.py b/web/backend/services/platform_fulfillment/customer.py index 97af17c..14467ed 100644 --- a/web/backend/services/platform_fulfillment/customer.py +++ b/web/backend/services/platform_fulfillment/customer.py @@ -17,6 +17,7 @@ from .binding import ( refresh_binding, start_binding, ) +from .common import FLOW_VERSION_CLEAN, _has_game_home_profile from .order_sync import sync_order_item_ids from .repository import ( _find_customer_task, @@ -126,6 +127,8 @@ async def customer_start_binding( return customer_binding_out(db, task.id) if task.issue_code == "mapping_missing": return customer_binding_out(db, task.id) + if task.flow_version == FLOW_VERSION_CLEAN and not _has_game_home_profile(task): + raise HTTPException(status_code=409, detail="请先提交游戏角色和游戏主页截图") if not task.account_id or not task.lease_id: from .auto_allocator import wake_auto_allocator