修复(履约绑定): 修复候选快照丢失与全链路绑定校验一致性

This commit is contained in:
yml
2026-11-26 13:50:10 +08:00
parent 49498e88f2
commit 554d399358
10 changed files with 851 additions and 70 deletions
@@ -280,6 +280,43 @@ def _owned_binding_tasks(
)
def sync_customer_role_to_bind_task(
db: Session,
task: PlatformFulfillmentTask,
) -> bool:
"""把客户最新提交的目标角色安全写入当前绑定任务并立即重算订单状态。"""
customer_role = str(task.customer_role_name or "").strip()
if not task.bind_task_id or not customer_role:
return False
bind_task = (
db.query(DouyuTask)
.filter(DouyuTask.id == int(task.bind_task_id))
.with_for_update()
.first()
)
if bind_task is None or bind_task.task_type not in {
"get_bind_qr",
"confirm_bind",
}:
return False
result = dict(bind_task.result or {})
result["expected_role_name"] = customer_role
result["expected_role_required"] = True
bind_task.result = result
if (
not _frozen(task)
and task.binding_status in ACTIVE_BINDING_STATUSES
and bind_task.status == "success"
):
return _apply_binding_task(
db,
task,
bind_task,
owned=bind_task.platform_task_id is not None,
)
return False
def _order_target_role(
db: Session,
task: PlatformFulfillmentTask,
@@ -1014,10 +1051,14 @@ def _apply_binding_task(
task,
before_task_id=int(source.id) if source.task_type == "confirm_bind" else None,
)
customer_role = str(task.customer_role_name or "").strip()
expected_role = target_role or customer_role
if is_current:
# 订单自己的当前任务(订单创建的或兼容路径挂上的)回查到 bindAct=1。
# 查询类任务不会走到这里:resolve_fulfillment_binding 会把观测降级。
accepted = source.task_type in BINDING_EVIDENCE_TASK_TYPES
accepted = source.task_type in BINDING_EVIDENCE_TASK_TYPES and (
not expected_role or role_names_match(confirmed_role, expected_role)
)
elif not owned:
accepted = False
elif phase == "confirming":
@@ -1026,13 +1067,13 @@ def _apply_binding_task(
elif source.task_type == "confirm_bind":
accepted = (
phase in CONFIRMABLE_BINDING_STATUSES
and bool(target_role)
and role_names_match(confirmed_role, target_role)
and bool(expected_role)
and role_names_match(confirmed_role, expected_role)
)
else:
# 订单自己的绑码任务直接被官网确认:有目标时必须一致,无目标视为全新绑定。
accepted = phase in ACTIVE_BINDING_STATUSES and (
not target_role or role_names_match(confirmed_role, target_role)
not expected_role or role_names_match(confirmed_role, expected_role)
)
if not accepted:
_record_binding_rejection(
@@ -1041,11 +1082,11 @@ def _apply_binding_task(
source,
reason=(
"target_mismatch"
if target_role and not role_names_match(confirmed_role, target_role)
if expected_role and not role_names_match(confirmed_role, expected_role)
else "untrusted_confirmation"
),
confirmed_role=confirmed_role,
target_role=target_role,
target_role=expected_role,
)
return False
task.bind_task_id = int(source.id)
@@ -1504,6 +1545,9 @@ def _attach_pending_binding_task(
)
):
return False
customer_role = str(task.customer_role_name or "").strip()
if customer_role and not role_names_match(binding.pending_role_name, customer_role):
return False
current_id = int(task.bind_task_id or 0)
if int(bind_task.id) == current_id:
return False
@@ -1738,7 +1782,9 @@ 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()
expected_role_name = str(
task.customer_role_name or target_role_name or current_bind_result.get("role_name") or ""
).strip()
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(