fix(fulfillment): 恢复 cjm 新角色客户确认
- 回滚 QYOOB-only 门禁,扫到新角色即进入 role_ready - 客户页 readiness 与平台任务 role_ready 对齐,避免确认接口 409 - 绑定证据 baseline 不再冒充本次扫码 - 新增人工确认绑定接口与按钮(角色、备注、操作人事件) - 补充客户确认、证据口径和人工确认回归测试
This commit is contained in:
@@ -13,6 +13,7 @@ from ..models import User
|
||||
from ..schemas import (
|
||||
PlatformFulfillmentCollabSyncRequest,
|
||||
PlatformFulfillmentCompleteRequest,
|
||||
PlatformFulfillmentManualBindingRequest,
|
||||
PlatformFulfillmentMappingUpsert,
|
||||
PlatformFulfillmentMappingWrite,
|
||||
PlatformFulfillmentReasonRequest,
|
||||
@@ -464,6 +465,36 @@ async def confirm_binding(
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/tasks/{task_id}/bind/manual-confirm")
|
||||
def manual_confirm_binding(
|
||||
task_id: int,
|
||||
req: PlatformFulfillmentManualBindingRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("platform_fulfillment:use")),
|
||||
) -> dict[str, Any]:
|
||||
"""运营人工确认绑定:用于客户已在别处绑定、或账号当前角色已确认的场景。"""
|
||||
result = service.manual_confirm_binding_sync(
|
||||
db,
|
||||
task_id=task_id,
|
||||
current=current,
|
||||
role_name=req.role_name,
|
||||
note=req.note,
|
||||
)
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action="platform_fulfillment:binding:manual_confirm",
|
||||
target=f"platform_fulfillment_task:{task_id}",
|
||||
detail={
|
||||
"roleName": req.role_name,
|
||||
"note": req.note,
|
||||
"bindingStatus": result.get("task", {}).get("bindingStatus"),
|
||||
},
|
||||
)
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
@router.post("/tasks/{task_id}/eligibility/refresh")
|
||||
def refresh_eligibility(
|
||||
task_id: int,
|
||||
|
||||
@@ -1710,6 +1710,11 @@ class PlatformFulfillmentCompleteRequest(BaseModel):
|
||||
note: str = Field(default="", max_length=1000)
|
||||
|
||||
|
||||
class PlatformFulfillmentManualBindingRequest(BaseModel):
|
||||
role_name: str = Field(default="", max_length=128)
|
||||
note: str = Field(default="", max_length=500)
|
||||
|
||||
|
||||
class PlatformFulfillmentSettingsUpdate(BaseModel):
|
||||
lease_duration_minutes: int | None = Field(..., ge=1, le=525600)
|
||||
completed_release_delay_minutes: int | None = Field(
|
||||
|
||||
@@ -246,37 +246,10 @@ def _lease_for_task(
|
||||
return fulfillment_lease_for_task(db, task)
|
||||
|
||||
|
||||
def _actionable_pending_role(
|
||||
result: dict[str, Any] | None,
|
||||
role_name: str,
|
||||
) -> str:
|
||||
"""只有动作 alias 上的 pending 才能作为订单目标。
|
||||
|
||||
cjm/legacy 上先出现的角色只能提示,不能直接确认;否则 actBind 会对
|
||||
QYOOB 调一个未生效的 pending(11.22 抓包 -200001 的根因)。
|
||||
"""
|
||||
role = str(role_name or "").strip()
|
||||
if not role or not isinstance(result, dict):
|
||||
return ""
|
||||
raw_aliases = result.get("action_act_aliases")
|
||||
action_aliases = (
|
||||
[str(value).strip() for value in raw_aliases if str(value).strip()]
|
||||
if isinstance(raw_aliases, (list, tuple))
|
||||
else []
|
||||
)
|
||||
selected_alias = str(result.get("act_alias") or "").strip()
|
||||
if action_aliases and selected_alias and selected_alias not in action_aliases:
|
||||
return ""
|
||||
return role
|
||||
|
||||
|
||||
def _pending_role_of(task: DouyuTask | None) -> str:
|
||||
if task is None or not isinstance(task.result, dict):
|
||||
return ""
|
||||
return _actionable_pending_role(
|
||||
task.result,
|
||||
pending_binding_role_from_result(task.result),
|
||||
)
|
||||
return pending_binding_role_from_result(task.result)
|
||||
|
||||
|
||||
def _owned_binding_tasks(
|
||||
@@ -625,6 +598,99 @@ def _mark_reused_pair(
|
||||
return True
|
||||
|
||||
|
||||
def manual_confirm_binding_sync(
|
||||
db: Session,
|
||||
*,
|
||||
task_id: int,
|
||||
current: User,
|
||||
role_name: str = "",
|
||||
note: str = "",
|
||||
) -> dict[str, Any]:
|
||||
"""运营人工确认绑定:不依赖本次扫码/actBind,直接落订单绑定事实。
|
||||
|
||||
用于客户已在别处完成绑定、或账号当前角色就是客户角色,但履约工作台没有
|
||||
本次扫码证据的场景。必须由运营确认角色并记录操作人/备注;不做自动复用。
|
||||
"""
|
||||
task = _load_task_for_update(db, task_id)
|
||||
if _frozen(task):
|
||||
# 已 bound / completed / cancelled:幂等返回,不重复写事件。
|
||||
return get_task_detail(db, task.id)
|
||||
if not task.account_id or not task.lease_id:
|
||||
raise HTTPException(status_code=409, detail="请先分配账号")
|
||||
lease = db.query(AccountLease).filter(AccountLease.id == task.lease_id).first()
|
||||
if lease is None or lease.status != "active":
|
||||
raise HTTPException(status_code=409, detail="账号租约已失效,请重新分配")
|
||||
account = db.get(Account, int(task.account_id))
|
||||
if account is None:
|
||||
raise HTTPException(status_code=409, detail="账号不存在")
|
||||
confirmed_role = str(role_name or account.game_name or "").strip()
|
||||
if not confirmed_role:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail="账号没有可确认的角色,请先查询角色或填写确认角色",
|
||||
)
|
||||
note = str(note or "").strip()[:500]
|
||||
previous_binding = str(task.binding_status or "")
|
||||
previous_execution = str(task.execution_status or "")
|
||||
|
||||
bind_task = (
|
||||
db.query(DouyuTask).filter(DouyuTask.id == int(task.bind_task_id)).first()
|
||||
if task.bind_task_id
|
||||
else None
|
||||
)
|
||||
if bind_task is not None and str(bind_task.status or "") in {
|
||||
"planned",
|
||||
"pending",
|
||||
"running",
|
||||
}:
|
||||
result = dict(bind_task.result or {})
|
||||
result.update(
|
||||
{
|
||||
"manual_confirmed": True,
|
||||
"manual_confirmed_role": confirmed_role,
|
||||
"manual_confirmed_by": int(current.id),
|
||||
"manual_confirm_note": note,
|
||||
"bind_phase": "manual_confirmed",
|
||||
"bind_polling": False,
|
||||
"bind_ready_for_confirm": False,
|
||||
}
|
||||
)
|
||||
bind_task.status = "stopped"
|
||||
bind_task.message = f"人工确认绑定: {confirmed_role}"[:512]
|
||||
bind_task.finished_at = _now()
|
||||
bind_task.result = result
|
||||
|
||||
task.binding_status = "bound"
|
||||
task.execution_status = "bound"
|
||||
task.issue_code = ""
|
||||
task.last_error = ""
|
||||
task.operator_id = int(current.id)
|
||||
task.updated_at = _now()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"binding_manually_confirmed",
|
||||
actor_type="user",
|
||||
actor_id=str(current.id),
|
||||
detail={
|
||||
"roleName": confirmed_role[:128],
|
||||
"accountId": int(task.account_id),
|
||||
"accountName": (account.nickname or account.username),
|
||||
"operatorId": int(current.id),
|
||||
"note": note,
|
||||
"sourceTaskId": int(bind_task.id) if bind_task is not None else None,
|
||||
"sourceTaskStatus": str(bind_task.status or "") if bind_task else "",
|
||||
"previousBindingStatus": previous_binding,
|
||||
"previousExecutionStatus": previous_execution,
|
||||
"baselineRole": str(account.game_name or "").strip(),
|
||||
},
|
||||
)
|
||||
_apply_task_lease_policy(db, task, lease=lease)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
|
||||
def _apply_binding_task(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
@@ -809,9 +875,9 @@ def _apply_binding_task(
|
||||
role_name = binding.pending_role_name
|
||||
if not role_name:
|
||||
return False
|
||||
if not _actionable_pending_role(source.result, role_name):
|
||||
# cjm/legacy 先出现的新角色:订单继续保持等待动作通道。
|
||||
return False
|
||||
# cjm/legacy 先出现的新角色也进入 role_ready:客户页需要能看到角色并
|
||||
# 发起确认;确认执行器会在 actBind 前重新查询 QYOOB,若动作通道尚未
|
||||
# 同步会返回可重试失败,而不是让客户卡在“没有待确认角色”。
|
||||
if phase == "confirming":
|
||||
# 确认任务执行中,后到的扫码结果不能把订单打回待确认、丢掉确认任务指针。
|
||||
return False
|
||||
@@ -1162,8 +1228,7 @@ def _latest_pending_binding_task(
|
||||
binding = resolve_douyu_binding(row, account=account)
|
||||
if binding.status != BINDING_VIEW_PENDING:
|
||||
continue
|
||||
if _actionable_pending_role(row.result, binding.pending_role_name):
|
||||
return row
|
||||
return row
|
||||
return None
|
||||
|
||||
|
||||
@@ -1185,10 +1250,6 @@ def _attach_pending_binding_task(
|
||||
or int(bind_task.account_id or 0) != int(task.account_id)
|
||||
or bind_task.task_type not in PENDING_BINDING_TASK_TYPES
|
||||
or binding.status != BINDING_VIEW_PENDING
|
||||
or not _actionable_pending_role(
|
||||
bind_task.result if isinstance(bind_task.result, dict) else {},
|
||||
binding.pending_role_name,
|
||||
)
|
||||
or not lease_is_active(lease)
|
||||
or not source_created_within_lease(lease, bind_task)
|
||||
or (
|
||||
@@ -1426,14 +1487,6 @@ def confirm_binding_sync(db: Session, *, task_id: int, current: User) -> dict[st
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
expected_role_name = str(current_bind_result.get("role_name") or "").strip()
|
||||
if expected_role_name and not _actionable_pending_role(
|
||||
current_bind_result,
|
||||
expected_role_name,
|
||||
):
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail="当前动作通道没有待确认角色,请客户重新扫码后再确认",
|
||||
)
|
||||
baseline_role_name = str(current_bind_result.get("current_role_name") or "").strip()
|
||||
batch_id = f"pfconfirm-{task.id}-{uuid.uuid4().hex[:8]}"
|
||||
confirm_task = DouyuTask(
|
||||
|
||||
@@ -208,13 +208,29 @@ def build_binding_evidence(
|
||||
legacy_latest = selected_info_candidate
|
||||
|
||||
pending_role = pending_binding_role_from_result(result)
|
||||
action_pending_role = (
|
||||
_text((action_pending or {}).get("roleName")) if action_pending else ""
|
||||
)
|
||||
# scannedRole 只认“本次新扫码”:
|
||||
# - 新 pending(QYOOB bindAct=0 / 可确认角色);
|
||||
# - legacy(cjm) 上出现的扫码角色;
|
||||
# - decision 明确可确认时的角色。
|
||||
# 不能用 result.role_name / before_bind_info 的 baseline 兜底,否则会把
|
||||
# 账号上一单的旧角色误报成本次扫码,详情就会和列表的“待扫码”冲突。
|
||||
scanned_role = _text(
|
||||
decision.get("role_name")
|
||||
or result.get("role_name")
|
||||
pending_role
|
||||
or (legacy_latest or {}).get("roleName")
|
||||
or pending_role
|
||||
or action_pending_role
|
||||
or (decision.get("role_name") if decision.get("confirmable") else "")
|
||||
)
|
||||
# 取消 QYOOB-only 门禁后,cjm 的新角色同样可以成为客户确认目标;
|
||||
# 这里展示真实本次扫码角色,避免详情说“不可确认”但客户页可以确认。
|
||||
target_role = scanned_role
|
||||
baseline_role = _text(
|
||||
result.get("baseline_role_name")
|
||||
or (selected_info or {}).get("role_name")
|
||||
or (result.get("before_bind_info") or {}).get("role_name")
|
||||
)
|
||||
target_role = pending_role if selected_actionable else ""
|
||||
account_current_role = _text(account.game_name) if account is not None else ""
|
||||
same_pair = bool(
|
||||
scanned_role
|
||||
@@ -262,6 +278,7 @@ def build_binding_evidence(
|
||||
"confirmable": confirmable,
|
||||
"targetRole": target_role,
|
||||
"scannedRole": scanned_role,
|
||||
"baselineRole": baseline_role,
|
||||
"samePair": same_pair,
|
||||
"actionCurrent": action_current,
|
||||
"actionPending": action_pending,
|
||||
|
||||
@@ -29,6 +29,7 @@ from .allocation import ( # noqa: F401
|
||||
from .binding import ( # noqa: F401
|
||||
confirm_binding,
|
||||
confirm_binding_sync,
|
||||
manual_confirm_binding_sync,
|
||||
reconcile_binding_observations,
|
||||
reconcile_binding_state_outbox,
|
||||
refresh_binding,
|
||||
@@ -152,6 +153,7 @@ __all__ = [
|
||||
"get_task_detail",
|
||||
"list_mappings",
|
||||
"list_tasks",
|
||||
"manual_confirm_binding_sync",
|
||||
"mapping_impact",
|
||||
"mark_manual_required",
|
||||
"prepare_manual_allocation",
|
||||
|
||||
@@ -488,7 +488,13 @@ def _task_out(
|
||||
"areaName": binding.effective_area_name,
|
||||
"platName": binding.effective_plat_name,
|
||||
"bindPhase": binding.bind_phase,
|
||||
"bindReadyForConfirm": binding.status == BINDING_VIEW_PENDING,
|
||||
# 客户页的“可确认”必须与确认接口读的同一层事实一致:
|
||||
# 绑定任务 pending 且平台任务已经进入 role_ready,才显示确认按钮。
|
||||
# 否则会出现客户页让确认、确认接口报“没有待确认角色”的错位。
|
||||
"bindReadyForConfirm": (
|
||||
binding.status == BINDING_VIEW_PENDING
|
||||
and str(task.binding_status or "") == "role_ready"
|
||||
),
|
||||
"bindConfirmed": binding.bind_confirmed,
|
||||
}
|
||||
if bind_task
|
||||
@@ -687,7 +693,12 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
||||
"roleName": binding.effective_role_name,
|
||||
"areaName": binding.effective_area_name,
|
||||
"platName": binding.effective_plat_name,
|
||||
"bindReadyForConfirm": binding.status == BINDING_VIEW_PENDING,
|
||||
# 客户页的“可确认”必须与确认接口读的同一层事实一致:
|
||||
# 绑定任务 pending 且平台任务已经进入 role_ready,才显示确认按钮。
|
||||
"bindReadyForConfirm": (
|
||||
binding.status == BINDING_VIEW_PENDING
|
||||
and str(task.binding_status or "") == "role_ready"
|
||||
),
|
||||
"bindConfirmed": binding.bind_confirmed,
|
||||
"queueState": queue_state,
|
||||
"retryAfterMs": retry_after_ms,
|
||||
|
||||
Reference in New Issue
Block a user