修复(履约绑定): 修复候选快照丢失与全链路绑定校验一致性
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -20,7 +20,10 @@ from typing import Any
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ...models import Account, DouyuTask, PlatformFulfillmentTask
|
||||
from ..account_binding import pending_binding_role_from_result
|
||||
from ..account_binding import (
|
||||
hydrate_bind_candidates_from_result,
|
||||
pending_binding_role_from_result,
|
||||
)
|
||||
from .role_lock import AccountLock, RoleLock, find_account_lock, find_role_lock
|
||||
|
||||
|
||||
@@ -133,26 +136,39 @@ def build_binding_evidence(
|
||||
]
|
||||
candidates = [
|
||||
_candidate(raw)
|
||||
for raw in (result.get("bind_candidates") or [])
|
||||
if isinstance(raw, dict)
|
||||
for raw in hydrate_bind_candidates_from_result(result)
|
||||
]
|
||||
candidates = [item for item in candidates if item["alias"]]
|
||||
decision = result.get("bind_decision")
|
||||
decision = decision if isinstance(decision, dict) else {}
|
||||
if not action_aliases:
|
||||
action_aliases = [
|
||||
item["alias"] for item in candidates if item["actionableAlias"]
|
||||
]
|
||||
selected_info = result.get("bind_info") or result.get("before_bind_info")
|
||||
selected_info_candidate = _candidate_from_info(selected_info, action_aliases)
|
||||
if not candidates and selected_info_candidate is not None:
|
||||
candidates = [selected_info_candidate]
|
||||
|
||||
selected_alias = _text(
|
||||
result.get("act_alias")
|
||||
or (selected_info or {}).get("act_alias")
|
||||
or (candidates[0]["alias"] if candidates else "")
|
||||
)
|
||||
decision = result.get("bind_decision")
|
||||
decision = decision if isinstance(decision, dict) else {}
|
||||
if (
|
||||
not action_aliases
|
||||
and selected_alias
|
||||
and bool(decision.get("actionable_alias"))
|
||||
):
|
||||
action_aliases = [selected_alias]
|
||||
selected_info_candidate = _candidate_from_info(selected_info, action_aliases)
|
||||
if not candidates and selected_info_candidate is not None:
|
||||
candidates = [selected_info_candidate]
|
||||
if action_aliases:
|
||||
for item in candidates:
|
||||
if item["alias"] in action_aliases:
|
||||
item["actionableAlias"] = True
|
||||
if (
|
||||
selected_info_candidate is not None
|
||||
and selected_info_candidate["alias"] in action_aliases
|
||||
):
|
||||
selected_info_candidate["actionableAlias"] = True
|
||||
selected_actionable = bool(
|
||||
decision.get("actionable_alias")
|
||||
or (selected_alias and selected_alias in action_aliases)
|
||||
@@ -173,6 +189,7 @@ def build_binding_evidence(
|
||||
account,
|
||||
action_aliases[0] if action_aliases else "",
|
||||
)
|
||||
pending_role = pending_binding_role_from_result(result)
|
||||
action_pending = next(
|
||||
(
|
||||
item
|
||||
@@ -180,6 +197,14 @@ def build_binding_evidence(
|
||||
if item["alias"] in action_aliases
|
||||
and item["roleName"]
|
||||
and not item["bindAct"]
|
||||
and (
|
||||
item["needBindAct"]
|
||||
or item["confirmable"]
|
||||
or (
|
||||
pending_role
|
||||
and item["roleName"].casefold() == pending_role.casefold()
|
||||
)
|
||||
)
|
||||
),
|
||||
None,
|
||||
)
|
||||
@@ -189,6 +214,15 @@ def build_binding_evidence(
|
||||
and selected_info_candidate["alias"] in action_aliases
|
||||
and selected_info_candidate["roleName"]
|
||||
and not selected_info_candidate["bindAct"]
|
||||
and (
|
||||
selected_info_candidate["needBindAct"]
|
||||
or selected_info_candidate["confirmable"]
|
||||
or (
|
||||
pending_role
|
||||
and selected_info_candidate["roleName"].casefold()
|
||||
== pending_role.casefold()
|
||||
)
|
||||
)
|
||||
):
|
||||
action_pending = selected_info_candidate
|
||||
legacy_latest = next(
|
||||
@@ -207,7 +241,6 @@ def build_binding_evidence(
|
||||
):
|
||||
legacy_latest = selected_info_candidate
|
||||
|
||||
pending_role = pending_binding_role_from_result(result)
|
||||
legacy_role = _text((legacy_latest or {}).get("roleName"))
|
||||
account_current_role = _text(account.game_name) if account is not None else ""
|
||||
# scannedRole 只认“本次新扫码”:
|
||||
@@ -231,10 +264,10 @@ def build_binding_evidence(
|
||||
# 取消 QYOOB-only 门禁后,cjm 的新角色同样可以成为客户确认目标;
|
||||
# 这里展示真实本次扫码角色,避免详情说“不可确认”但客户页可以确认。
|
||||
target_role = scanned_role
|
||||
before_info = result.get("before_bind_info")
|
||||
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")
|
||||
or (before_info if isinstance(before_info, dict) else {}).get("role_name")
|
||||
)
|
||||
same_pair = bool(
|
||||
scanned_role
|
||||
|
||||
@@ -10,13 +10,13 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from ...models import (
|
||||
AccountLease,
|
||||
DouyuTask,
|
||||
PlatformFulfillmentTask,
|
||||
)
|
||||
from .binding import (
|
||||
confirm_binding,
|
||||
refresh_binding,
|
||||
start_binding,
|
||||
sync_customer_role_to_bind_task,
|
||||
)
|
||||
from .common import FLOW_VERSION_CLEAN, _has_game_home_profile
|
||||
from .order_sync import sync_order_item_ids
|
||||
@@ -100,16 +100,7 @@ def save_customer_profile(
|
||||
task.customer_home_images = images
|
||||
task.customer_profile_updated_at = datetime.now(UTC)
|
||||
task.updated_at = datetime.now(UTC)
|
||||
if task.bind_task_id and task.customer_role_name:
|
||||
bind_task = db.get(DouyuTask, int(task.bind_task_id))
|
||||
if bind_task is not None and bind_task.task_type in {
|
||||
"get_bind_qr",
|
||||
"confirm_bind",
|
||||
}:
|
||||
result = dict(bind_task.result or {})
|
||||
result["expected_role_name"] = task.customer_role_name
|
||||
result["expected_role_required"] = True
|
||||
bind_task.result = result
|
||||
sync_customer_role_to_bind_task(db, task)
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
|
||||
@@ -11,7 +11,6 @@ from fastapi import HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ...models import (
|
||||
DouyuTask,
|
||||
PlatformCommodityMapping,
|
||||
PlatformFulfillmentTask,
|
||||
)
|
||||
@@ -112,15 +111,9 @@ def _apply_customer_profile(
|
||||
)
|
||||
)
|
||||
if db is not None and task.bind_task_id and task.customer_role_name:
|
||||
bind_task = db.get(DouyuTask, int(task.bind_task_id))
|
||||
if bind_task is not None and bind_task.task_type in {
|
||||
"get_bind_qr",
|
||||
"confirm_bind",
|
||||
}:
|
||||
result = dict(bind_task.result or {})
|
||||
result["expected_role_name"] = task.customer_role_name
|
||||
result["expected_role_required"] = True
|
||||
bind_task.result = result
|
||||
from .binding import sync_customer_role_to_bind_task
|
||||
|
||||
sync_customer_role_to_bind_task(db, task)
|
||||
|
||||
|
||||
def _upsert_order_snapshot(
|
||||
|
||||
Reference in New Issue
Block a user