feat(fulfillment): 接收并展示 order_site 客户主页截图

- 履约任务新增客户角色/平台/系统/主页截图/更新时间字段
- 迁移 0131
- order_site contractVersion=3 快照自动落客户资料
- 增加客户资料 integration 写入接口
- 工作台详情展示客户资料、平台角色比对和截图
- 补充客户资料保存/快照解析测试与对接文档
This commit is contained in:
yml
2026-11-24 21:25:14 +08:00
parent 06a226f51f
commit 6ec8248068
14 changed files with 531 additions and 2 deletions
@@ -2,6 +2,7 @@
from __future__ import annotations
from datetime import UTC, datetime
from typing import Any
from fastapi import HTTPException
@@ -21,7 +22,9 @@ from .repository import (
_find_customer_task,
_integration_actor,
_load_task,
_load_task_for_update,
)
from .runtime import _append_event
from .views import customer_binding_out
@@ -61,6 +64,57 @@ def ensure_customer_session(
return customer_binding_out(db, task.id)
def save_customer_profile(
db: Session,
*,
task_id: int,
customer_role_name: str,
platform: str,
system: str,
home_images: list[dict[str, str]],
commit: bool = True,
) -> dict[str, Any]:
"""保存 order_site 客户资料;至少一张主页截图由 order_site 端强制。"""
task = _load_task_for_update(db, task_id)
if task.execution_status in {"completed", "cancelled"}:
raise HTTPException(status_code=409, detail="订单已结束,不能修改客户资料")
images = []
for item in home_images[:5]:
url = str(item.get("url") or "").strip()
if not url:
continue
images.append(
{
"url": url[:2048],
"thumbnailUrl": str(item.get("thumbnail_url") or item.get("thumbnailUrl") or url)[:2048],
"objectKey": str(item.get("object_key") or item.get("objectKey") or "")[:512],
}
)
if not images:
raise HTTPException(status_code=409, detail="客户主页截图至少需要 1 张")
task.customer_role_name = str(customer_role_name or "").strip()[:128]
task.customer_platform = str(platform or "").strip()[:16]
task.customer_system = str(system or "").strip()[:16]
task.customer_home_images = images
task.customer_profile_updated_at = datetime.now(UTC)
task.updated_at = datetime.now(UTC)
_append_event(
db,
task.id,
"customer_profile_saved",
actor_type="integration",
detail={
"customerRoleName": task.customer_role_name,
"platform": task.customer_platform,
"system": task.customer_system,
"homeImageCount": len(images),
},
)
if commit:
db.commit()
return customer_binding_out(db, task.id)
async def customer_start_binding(
db: Session,
*,
@@ -42,6 +42,72 @@ _AUTO_SYNC_LOCK = threading.Lock()
_AUTO_SYNC_LAST_ATTEMPT = float("-inf")
def _apply_customer_profile(
task: PlatformFulfillmentTask,
snapshot: dict[str, Any],
) -> None:
"""order_site contractVersion=3 快照里的客户资料和主页截图。"""
keys = {
"customerRoleName",
"customer_role_name",
"homeImages",
"home_images",
}
if not keys.intersection(snapshot):
return
task.customer_role_name = str(
_snapshot_value(snapshot, "customerRoleName", "customer_role_name")
or ""
)[:128]
task.customer_platform = str(
_snapshot_value(snapshot, "customerPlatform", "customer_platform")
or ""
)[:16]
task.customer_system = str(
_snapshot_value(snapshot, "customerSystem", "customer_system")
or ""
)[:16]
raw_images = _snapshot_value(
snapshot,
"homeImages",
"home_images",
default=[],
)
images: list[dict[str, str]] = []
if isinstance(raw_images, list):
for item in raw_images[:5]:
if isinstance(item, str):
url = item.strip()
if url:
images.append({"url": url[:2048], "thumbnailUrl": url[:2048], "objectKey": ""})
continue
if not isinstance(item, dict):
continue
url = str(item.get("url") or "").strip()
if not url:
continue
images.append(
{
"url": url[:2048],
"thumbnailUrl": str(
item.get("thumbnailUrl") or item.get("thumbnail_url") or url
)[:2048],
"objectKey": str(
item.get("objectKey") or item.get("object_key") or ""
)[:512],
}
)
task.customer_home_images = images or None
task.customer_profile_updated_at = _parse_snapshot_datetime(
_snapshot_value(
snapshot,
"customerProfileUpdatedAt",
"customer_profile_updated_at",
default=None,
)
)
def _upsert_order_snapshot(
db: Session,
snapshot: dict[str, Any],
@@ -150,6 +216,7 @@ def _upsert_order_snapshot(
task.required_points,
task.issue_code,
)
_apply_customer_profile(task, snapshot)
if task.execution_status in {"completed", "cancelled"}:
task.source_snapshot_json = _jsonable(snapshot)
return task, created, False
@@ -481,6 +548,7 @@ def _sync_pending_tasks(
task.target_platform = TARGET_PLATFORM
task.scope = SCOPE_ELITE
task.source_snapshot_json = _jsonable(row)
_apply_customer_profile(task, row)
task.updated_at = _now()
if task.account_id is None and task.execution_status in {
"pending_mapping",
@@ -67,6 +67,7 @@ from .customer import (
customer_refresh_bindings,
customer_start_binding,
ensure_customer_session,
save_customer_profile,
)
from .lifecycle import (
cancel_task,
@@ -173,6 +174,7 @@ __all__ = [
"reset_task_binding",
"restore_cancelled_task",
"restore_mapping",
"save_customer_profile",
"set_mapping_enabled",
"start_binding",
"sync_active_task_from_account_binding",
@@ -506,6 +506,7 @@ def _task_out(
if include_qr and bind_task is not None
else None
),
"customerProfile": _customer_profile_out(task, account, binding),
"exchangeTaskId": exchange_task_id,
"exchangeResultImage": (
{
@@ -690,6 +691,58 @@ def _customer_binding_queue_state(
return "preparing", 1000
def _customer_profile_out(
task: PlatformFulfillmentTask,
account: Account | None,
binding,
) -> dict[str, Any] | None:
"""客户资料 + 与平台当前/确认角色的绑定比对。"""
role = str(task.customer_role_name or "").strip()
images = list(task.customer_home_images or [])
if not role and not images:
return None
platform_role = str(binding.effective_role_name or "").strip()
if not platform_role and account is not None:
platform_role = str(account.game_name or "").strip()
role_matched: bool | None = None
if role and platform_role:
role_matched = role == platform_role
channel_matched: bool | None = None
customer_channel = " ".join(
[
"微信" if task.customer_platform == "wechat" else ("QQ" if task.customer_platform == "qq" else ""),
"苹果" if task.customer_system == "ios" else ("安卓" if task.customer_system == "android" else ""),
]
).strip()
account_channel = str(
binding.effective_channel
or (account.game_channel if account is not None else "")
or ""
)
if customer_channel and account_channel:
channel_matched = all(part in account_channel for part in customer_channel.split())
if role and not platform_role:
message = "客户已填角色,平台还未识别绑定角色"
elif role_matched is True and channel_matched is not False:
message = "客户资料与平台角色一致"
elif role_matched is False or channel_matched is False:
message = "客户资料与平台角色不一致,请核对绑定"
else:
message = "客户资料已收到,等待平台识别角色"
return {
"roleName": role,
"platform": str(task.customer_platform or ""),
"system": str(task.customer_system or ""),
"homeImages": images,
"updatedAt": _jsonable(task.customer_profile_updated_at),
"platformRoleName": platform_role,
"platformChannel": account_channel,
"roleMatched": role_matched,
"channelMatched": channel_matched,
"message": message,
}
def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
"""面向前端客户页的安全绑定快照,不包含账号、CK、积分和租约。"""
task = _load_task(db, task_id)
@@ -754,6 +807,7 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
"queueLane": bind_task.queue_lane if bind_task else "",
"accepted": False,
"stateVersion": int(task.state_version or 0),
"customerProfile": _customer_profile_out(task, account, binding),
"completedAt": _jsonable(task.completed_at),
"updatedAt": _jsonable(task.updated_at),
}