fix(fulfillment): 通过状态转换识别最新角色
This commit is contained in:
@@ -514,6 +514,37 @@ def list_tasks(
|
||||
}
|
||||
|
||||
|
||||
def _pending_binding_role_name(result: dict[str, Any]) -> str:
|
||||
if not bool(result.get("bind_ready_for_confirm")):
|
||||
return ""
|
||||
role_name = str(result.get("role_name") or "").strip()
|
||||
if not role_name:
|
||||
return ""
|
||||
baseline = str(
|
||||
result.get("baseline_role_name") or result.get("current_role_name") or ""
|
||||
).strip()
|
||||
if baseline and role_name.casefold() == baseline.casefold():
|
||||
# 同名角色仍可能是本次新绑定:活动状态必须出现从已绑定到
|
||||
# needBindAct 的待确认转换。仅凭 cjm 中残留的同名 bindRole 不算。
|
||||
transitioned_to_pending = bool(result.get("need_bind_act")) and not bool(
|
||||
result.get("is_bound_act")
|
||||
)
|
||||
if not transitioned_to_pending:
|
||||
return ""
|
||||
return role_name
|
||||
|
||||
|
||||
def _binding_role_out(result: dict[str, Any]) -> str:
|
||||
pending_role = _pending_binding_role_name(result)
|
||||
if pending_role:
|
||||
return pending_role
|
||||
if bool(result.get("bind_confirmed")):
|
||||
return str(
|
||||
result.get("role_name") or result.get("current_role_name") or ""
|
||||
).strip()
|
||||
return ""
|
||||
|
||||
|
||||
def _task_out(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
@@ -536,6 +567,7 @@ def _task_out(
|
||||
else None
|
||||
)
|
||||
bind_result = (bind_task.result or {}) if bind_task else {}
|
||||
pending_role = _pending_binding_role_name(bind_result)
|
||||
return {
|
||||
"id": task.id,
|
||||
"sourceSystem": task.source_system,
|
||||
@@ -598,15 +630,11 @@ def _task_out(
|
||||
"status": bind_task.status,
|
||||
"message": bind_task.message,
|
||||
"qrUrl": str(bind_result.get("url") or "") if include_qr else "",
|
||||
"roleName": str(
|
||||
bind_result.get("role_name")
|
||||
or bind_result.get("current_role_name")
|
||||
or ""
|
||||
),
|
||||
"roleName": _binding_role_out(bind_result),
|
||||
"areaName": str(bind_result.get("area_name") or ""),
|
||||
"platName": str(bind_result.get("plat_name") or ""),
|
||||
"bindPhase": str(bind_result.get("bind_phase") or ""),
|
||||
"bindReadyForConfirm": bool(bind_result.get("bind_ready_for_confirm")),
|
||||
"bindReadyForConfirm": bool(pending_role),
|
||||
"bindConfirmed": bool(bind_result.get("bind_confirmed")),
|
||||
}
|
||||
if bind_task
|
||||
@@ -685,6 +713,7 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
||||
else None
|
||||
)
|
||||
bind_result = (bind_task.result or {}) if bind_task else {}
|
||||
pending_role = _pending_binding_role_name(bind_result)
|
||||
lease = (
|
||||
db.query(AccountLease).filter(AccountLease.id == task.lease_id).first()
|
||||
if task.lease_id
|
||||
@@ -711,14 +740,10 @@ def customer_binding_out(db: Session, task_id: int) -> dict[str, Any]:
|
||||
"qrUrl": str(bind_result.get("url") or ""),
|
||||
"qrGeneratedAt": str(bind_result.get("qr_generated_at") or ""),
|
||||
"qrExpiresAt": str(bind_result.get("qr_expires_at") or ""),
|
||||
"roleName": str(
|
||||
bind_result.get("role_name") or bind_result.get("current_role_name") or ""
|
||||
),
|
||||
"roleName": _binding_role_out(bind_result),
|
||||
"areaName": str(bind_result.get("area_name") or ""),
|
||||
"platName": str(bind_result.get("plat_name") or ""),
|
||||
"bindReadyForConfirm": (
|
||||
bool(bind_result.get("bind_ready_for_confirm")) and not binding_failed
|
||||
),
|
||||
"bindReadyForConfirm": (bool(pending_role) and not binding_failed),
|
||||
"bindConfirmed": bool(bind_result.get("bind_confirmed")) and binding_confirmed,
|
||||
"completedAt": _jsonable(task.completed_at),
|
||||
"updatedAt": _jsonable(task.updated_at),
|
||||
@@ -1218,13 +1243,19 @@ async def start_binding(
|
||||
existing = db.query(DouyuTask).filter(DouyuTask.id == task.bind_task_id).first()
|
||||
if existing:
|
||||
result = existing.result or {}
|
||||
if bool(result.get("bind_confirmed")) or bool(
|
||||
result.get("bind_ready_for_confirm")
|
||||
):
|
||||
pending_role = _pending_binding_role_name(result)
|
||||
stale_pending = (
|
||||
bool(result.get("bind_ready_for_confirm")) and not pending_role
|
||||
)
|
||||
if bool(result.get("bind_confirmed")) or pending_role:
|
||||
return get_task_detail(db, task.id)
|
||||
if existing.status in {"planned", "pending", "running"}:
|
||||
return get_task_detail(db, task.id)
|
||||
if existing.status == "success" and not _binding_qr_expired(existing):
|
||||
if (
|
||||
existing.status == "success"
|
||||
and not _binding_qr_expired(existing)
|
||||
and not stale_pending
|
||||
):
|
||||
return get_task_detail(db, task.id)
|
||||
if not force_refresh and existing.status not in {
|
||||
"success",
|
||||
@@ -1296,6 +1327,7 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
||||
task.last_error,
|
||||
)
|
||||
result = bind_task.result or {}
|
||||
pending_role = _pending_binding_role_name(result)
|
||||
if bind_task.status in {"failed", "error", "stopped"}:
|
||||
task.binding_status = "failed"
|
||||
task.execution_status = "manual_required"
|
||||
@@ -1306,11 +1338,38 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
||||
task.execution_status = "bound"
|
||||
task.issue_code = ""
|
||||
task.last_error = ""
|
||||
elif bool(result.get("bind_ready_for_confirm")) or result.get("role_name"):
|
||||
elif pending_role:
|
||||
task.binding_status = "role_ready"
|
||||
task.execution_status = "role_ready"
|
||||
task.issue_code = ""
|
||||
task.last_error = ""
|
||||
elif bool(result.get("bind_ready_for_confirm")):
|
||||
# 兼容旧快照:同名旧角色曾被误标为待确认。清掉绑定指针后,客户页会
|
||||
# 回到已分配状态,并在下一次刷新时自动生成新二维码、重新开始轮询。
|
||||
previous_bind_task_id = task.bind_task_id
|
||||
task.bind_task_id = None
|
||||
task.binding_status = "not_started"
|
||||
task.execution_status = "allocated"
|
||||
task.issue_code = ""
|
||||
task.last_error = ""
|
||||
task.updated_at = _now()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"binding_stale_role_ignored",
|
||||
detail={
|
||||
"bindTaskId": previous_bind_task_id,
|
||||
"roleName": str(result.get("role_name") or "")[:100],
|
||||
"baselineRoleName": str(
|
||||
result.get("baseline_role_name")
|
||||
or result.get("current_role_name")
|
||||
or ""
|
||||
)[:100],
|
||||
},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
elif result.get("url"):
|
||||
task.binding_status = "qr_pending"
|
||||
task.execution_status = "waiting_bind"
|
||||
|
||||
Reference in New Issue
Block a user