修复(履约绑定): 收敛活动别名中的瞬时历史角色
This commit is contained in:
@@ -370,6 +370,7 @@ def binding_pending_decision(
|
||||
baseline_is_bound_act: bool = False,
|
||||
baseline_info: dict | None = None,
|
||||
actionable_aliases: Sequence[str] = (),
|
||||
expected_role_name: str = "",
|
||||
) -> dict[str, Any]:
|
||||
"""解释一个绑定候选是否可进入确认阶段。
|
||||
|
||||
@@ -398,6 +399,7 @@ def binding_pending_decision(
|
||||
"same_alias": False,
|
||||
"role_changed": False,
|
||||
"actionable_alias": False,
|
||||
"expected_role_name": str(expected_role_name or "").strip(),
|
||||
"confirmable": False,
|
||||
"reason": "no_candidate",
|
||||
}
|
||||
@@ -423,6 +425,10 @@ def binding_pending_decision(
|
||||
if not role_name:
|
||||
result["reason"] = "no_role"
|
||||
return result
|
||||
expected = str(expected_role_name or "").strip()
|
||||
if expected and role_name.casefold() != expected.casefold():
|
||||
result["reason"] = "expected_role_mismatch"
|
||||
return result
|
||||
if snapshot["is_bound_act"]:
|
||||
# 2026-11-22 换绑失败 HAR:历史角色会长期返回 bindAct=1 + needBindAct=1。
|
||||
# 这表示当前绑定仍生效,不是待确认;若官方在扫码后直接完成绑定,轮询层
|
||||
@@ -500,6 +506,7 @@ def is_pending_binding_role(
|
||||
baseline_is_bound_act: bool = False,
|
||||
baseline_info: dict | None = None,
|
||||
actionable_aliases: Sequence[str] = (),
|
||||
expected_role_name: str = "",
|
||||
) -> bool:
|
||||
"""兼容布尔调用;详细原因统一由 :func:`binding_pending_decision` 给出。"""
|
||||
return bool(
|
||||
@@ -509,6 +516,7 @@ def is_pending_binding_role(
|
||||
baseline_is_bound_act=baseline_is_bound_act,
|
||||
baseline_info=baseline_info,
|
||||
actionable_aliases=actionable_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
)["confirmable"]
|
||||
)
|
||||
|
||||
@@ -538,12 +546,36 @@ def pending_binding_role_from_result(result: dict[str, Any] | None) -> str:
|
||||
]
|
||||
if not action_aliases and result.get("act_alias"):
|
||||
action_aliases = [str(result["act_alias"])]
|
||||
action_alias_set = set(action_aliases)
|
||||
selected_alias = str(result.get("act_alias") or "").strip()
|
||||
action_evidence = [
|
||||
item
|
||||
for item in [
|
||||
*(result.get("bind_candidates") or []),
|
||||
*(result.get("bind_candidate_decisions") or []),
|
||||
]
|
||||
if isinstance(item, dict)
|
||||
and str(item.get("act_alias") or item.get("alias") or "").strip()
|
||||
in action_alias_set
|
||||
and str(item.get("role_name") or "").strip()
|
||||
]
|
||||
if (
|
||||
action_alias_set
|
||||
and selected_alias
|
||||
and selected_alias not in action_alias_set
|
||||
and action_evidence
|
||||
):
|
||||
# legacy(cjm) 可能返回同一斗鱼账号历史绑过的另一个游戏角色。
|
||||
# 动作 alias 仍有明确角色时,legacy 的 role_name 不能成为本单 pending。
|
||||
return ""
|
||||
expected_role_name = str(result.get("expected_role_name") or "").strip()
|
||||
if is_pending_binding_role(
|
||||
result,
|
||||
baseline_role_name=baseline,
|
||||
baseline_is_bound_act=bool(result.get("baseline_is_bound_act")),
|
||||
baseline_info=baseline_info,
|
||||
actionable_aliases=action_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
):
|
||||
return role_name
|
||||
return ""
|
||||
|
||||
@@ -303,9 +303,18 @@ def resolve_elite_role(
|
||||
pending_info = None
|
||||
pending_decision: dict[str, Any] | None = None
|
||||
baseline_role = str((bound_info or {}).get("role_name") or "").strip()
|
||||
action_alias_has_role = any(
|
||||
str(info.get("act_alias") or "") in action_aliases
|
||||
and str(info.get("role_name") or "").strip()
|
||||
for info in candidates_info
|
||||
)
|
||||
|
||||
def find_pending(*, changed_role_only: bool) -> tuple[dict | None, dict | None]:
|
||||
for alias in ordered_aliases:
|
||||
if action_alias_has_role and alias not in action_aliases:
|
||||
# cjm 可能返回同斗鱼账号历史上另一个 QQ/WX 游戏角色,不能盖过
|
||||
# 当前动作 alias 已经存在的绑定角色。
|
||||
continue
|
||||
for info in candidates_info:
|
||||
if str(info.get("act_alias") or "") != alias:
|
||||
continue
|
||||
|
||||
@@ -174,6 +174,7 @@ class BindMixin:
|
||||
baseline_is_bound_act: bool = False,
|
||||
baseline_info: dict | None = None,
|
||||
actionable_aliases: tuple[str, ...] | list[str] = (),
|
||||
expected_role_name: str = "",
|
||||
) -> bool:
|
||||
"""判断是否出现了可确认的扫码角色。
|
||||
|
||||
@@ -190,6 +191,7 @@ class BindMixin:
|
||||
baseline_is_bound_act=baseline_is_bound_act,
|
||||
baseline_info=baseline_info,
|
||||
actionable_aliases=actionable_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
@@ -216,9 +218,23 @@ class BindMixin:
|
||||
*,
|
||||
baseline_role_name: str,
|
||||
baseline_is_bound_act: bool,
|
||||
expected_role_name: str = "",
|
||||
actionable_aliases: tuple[str, ...] | list[str] = (),
|
||||
) -> dict | None:
|
||||
"""Find a scan result that the official API already confirmed."""
|
||||
expected = expected_role_name.strip().casefold()
|
||||
aliases = {
|
||||
str(value or "").strip()
|
||||
for value in actionable_aliases
|
||||
if str(value or "").strip()
|
||||
}
|
||||
for candidate in candidates:
|
||||
alias = str(candidate.get("act_alias") or "").strip()
|
||||
if aliases and alias not in aliases:
|
||||
continue
|
||||
role_name = str(candidate.get("role_name") or "").strip()
|
||||
if expected and role_name.casefold() != expected:
|
||||
continue
|
||||
if cls._is_newly_confirmed_binding(
|
||||
binding_snapshot(candidate),
|
||||
baseline_role_name=baseline_role_name,
|
||||
@@ -332,18 +348,48 @@ class BindMixin:
|
||||
baseline_is_bound_act: bool = False,
|
||||
baseline_info: dict | None = None,
|
||||
actionable_aliases: list[str] | tuple[str, ...] = (),
|
||||
expected_role_name: str = "",
|
||||
) -> list[dict[str, Any]]:
|
||||
"""按同一规则解释每个 actAlias 候选,供任务结果和日志复盘。"""
|
||||
return [
|
||||
decisions = [
|
||||
binding_pending_decision(
|
||||
info,
|
||||
baseline_role_name=baseline_role_name,
|
||||
baseline_is_bound_act=baseline_is_bound_act,
|
||||
baseline_info=baseline_info,
|
||||
actionable_aliases=actionable_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
)
|
||||
for info in candidates
|
||||
]
|
||||
alias_set = {
|
||||
str(value or "").strip()
|
||||
for value in actionable_aliases
|
||||
if str(value or "").strip()
|
||||
}
|
||||
action_alias_has_role = any(
|
||||
str(info.get("act_alias") or "").strip() in alias_set
|
||||
and str(info.get("role_name") or "").strip()
|
||||
for info in candidates
|
||||
)
|
||||
action_roles = {
|
||||
str(info.get("role_name") or "").strip()
|
||||
for info in candidates
|
||||
if str(info.get("act_alias") or "").strip() in alias_set
|
||||
and str(info.get("role_name") or "").strip()
|
||||
}
|
||||
if action_alias_has_role:
|
||||
for info, decision in zip(candidates, decisions, strict=True):
|
||||
alias = str(info.get("act_alias") or "").strip()
|
||||
role_name = str(info.get("role_name") or "").strip()
|
||||
if alias not in alias_set and role_name not in action_roles:
|
||||
decision.update(
|
||||
{
|
||||
"confirmable": False,
|
||||
"reason": "legacy_superseded_by_action_alias",
|
||||
}
|
||||
)
|
||||
return decisions
|
||||
|
||||
def _push_binding_decision_log(
|
||||
self,
|
||||
@@ -582,6 +628,7 @@ class BindMixin:
|
||||
prefer_pending: bool = True,
|
||||
prefer_aliases: list[str] | None = None,
|
||||
actionable_aliases: list[str] | tuple[str, ...] = (),
|
||||
expected_role_name: str = "",
|
||||
) -> dict | None:
|
||||
"""从多个 alias 结果里挑最有用的绑定信息。
|
||||
|
||||
@@ -601,6 +648,7 @@ class BindMixin:
|
||||
baseline_is_bound_act=baseline_is_bound_act,
|
||||
baseline_info=baseline_info,
|
||||
actionable_aliases=actionable_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
)
|
||||
if pending is not None:
|
||||
return pending
|
||||
@@ -640,6 +688,7 @@ class BindMixin:
|
||||
baseline_is_bound_act: bool = False,
|
||||
baseline_info: dict | None = None,
|
||||
actionable_aliases: list[str] | tuple[str, ...] = (),
|
||||
expected_role_name: str = "",
|
||||
) -> dict | None:
|
||||
"""唯一的新角色候选选择入口。
|
||||
|
||||
@@ -648,26 +697,50 @@ class BindMixin:
|
||||
"""
|
||||
ordered = self._ordered_bind_candidates(candidates, prefer_aliases)
|
||||
baseline = baseline_role_name.strip().casefold()
|
||||
expected = expected_role_name.strip().casefold()
|
||||
alias_set = {
|
||||
str(value or "").strip()
|
||||
for value in actionable_aliases
|
||||
if str(value or "").strip()
|
||||
}
|
||||
action_alias_has_role = any(
|
||||
str(info.get("act_alias") or "").strip() in alias_set
|
||||
and str(info.get("role_name") or "").strip()
|
||||
for info in candidates
|
||||
)
|
||||
if baseline:
|
||||
for info in ordered:
|
||||
role_name = str(info.get("role_name") or "").strip()
|
||||
if not role_name or role_name.casefold() == baseline:
|
||||
continue
|
||||
alias = str(info.get("act_alias") or "").strip()
|
||||
if action_alias_has_role and alias not in alias_set:
|
||||
continue
|
||||
if expected and role_name.casefold() != expected:
|
||||
continue
|
||||
if self._is_pending_role(
|
||||
info,
|
||||
baseline_role_name=baseline_role_name,
|
||||
baseline_is_bound_act=baseline_is_bound_act,
|
||||
baseline_info=baseline_info,
|
||||
actionable_aliases=actionable_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
):
|
||||
return info
|
||||
for info in ordered:
|
||||
role_name = str(info.get("role_name") or "").strip()
|
||||
alias = str(info.get("act_alias") or "").strip()
|
||||
if action_alias_has_role and alias not in alias_set:
|
||||
continue
|
||||
if expected and role_name.casefold() != expected:
|
||||
continue
|
||||
if self._is_pending_role(
|
||||
info,
|
||||
baseline_role_name=baseline_role_name,
|
||||
baseline_is_bound_act=baseline_is_bound_act,
|
||||
baseline_info=baseline_info,
|
||||
actionable_aliases=actionable_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
):
|
||||
return info
|
||||
return None
|
||||
@@ -743,6 +816,7 @@ class BindMixin:
|
||||
result["baseline_role_name"] = baseline_role_name
|
||||
result["baseline_is_bound_act"] = baseline_is_bound_act
|
||||
result["query_act_aliases"] = query_aliases
|
||||
expected_role_name = str(result.get("expected_role_name") or "").strip()
|
||||
action_aliases = [
|
||||
str(value)
|
||||
for value in (result.get("action_act_aliases") or [])
|
||||
@@ -782,6 +856,7 @@ class BindMixin:
|
||||
prefer_pending=True,
|
||||
prefer_aliases=query_aliases,
|
||||
actionable_aliases=action_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
)
|
||||
or candidates[0]
|
||||
)
|
||||
@@ -792,6 +867,7 @@ class BindMixin:
|
||||
baseline_is_bound_act=baseline_is_bound_act,
|
||||
baseline_info=baseline_info,
|
||||
actionable_aliases=action_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
)
|
||||
query_alias = str(bind_info.get("act_alias") or "")
|
||||
summary = (
|
||||
@@ -909,6 +985,7 @@ class BindMixin:
|
||||
task.last_run_at = now
|
||||
task.attempt_count = int(task.attempt_count or 0) + 1
|
||||
result = dict(task.result or {})
|
||||
expected_role_name = str(result.get("expected_role_name") or "").strip()
|
||||
query_aliases = [
|
||||
str(value)
|
||||
for value in (result.get("query_act_aliases") or [])
|
||||
@@ -1043,6 +1120,7 @@ class BindMixin:
|
||||
prefer_pending=True,
|
||||
prefer_aliases=query_aliases,
|
||||
actionable_aliases=action_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
)
|
||||
or candidates[0]
|
||||
)
|
||||
@@ -1053,6 +1131,7 @@ class BindMixin:
|
||||
baseline_is_bound_act=baseline_is_bound_act,
|
||||
baseline_info=baseline_info,
|
||||
actionable_aliases=action_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
)
|
||||
selected_decision = binding_pending_decision(
|
||||
bind_info,
|
||||
@@ -1060,6 +1139,7 @@ class BindMixin:
|
||||
baseline_is_bound_act=baseline_is_bound_act,
|
||||
baseline_info=baseline_info,
|
||||
actionable_aliases=action_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
)
|
||||
is_pending_role = bool(selected_decision["confirmable"])
|
||||
query_alias = str(bind_info.get("act_alias") or "")
|
||||
@@ -1081,6 +1161,8 @@ class BindMixin:
|
||||
candidates,
|
||||
baseline_role_name=baseline_role_name,
|
||||
baseline_is_bound_act=baseline_is_bound_act,
|
||||
expected_role_name=expected_role_name,
|
||||
actionable_aliases=action_aliases,
|
||||
)
|
||||
if directly_confirmed is not None:
|
||||
bind_info = directly_confirmed
|
||||
@@ -1215,6 +1297,9 @@ class BindMixin:
|
||||
self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict
|
||||
):
|
||||
client = self._client(cookie)
|
||||
expected_role_name = str(
|
||||
(task.result or {}).get("expected_role_name") or ""
|
||||
).strip()
|
||||
qr_act_alias = self._bind_qr_act_alias(config)
|
||||
query_aliases = self._query_bind_act_aliases(config)
|
||||
action_aliases = self._action_act_aliases(config)
|
||||
@@ -1253,6 +1338,7 @@ class BindMixin:
|
||||
prefer_pending=True,
|
||||
prefer_aliases=query_aliases,
|
||||
actionable_aliases=action_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
)
|
||||
or before
|
||||
or before_candidates[0]
|
||||
@@ -1263,6 +1349,7 @@ class BindMixin:
|
||||
baseline_is_bound_act=self._is_bound_act(before),
|
||||
baseline_info=before,
|
||||
actionable_aliases=action_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
)
|
||||
pending_decision = binding_pending_decision(
|
||||
pending_before,
|
||||
@@ -1270,6 +1357,7 @@ class BindMixin:
|
||||
baseline_is_bound_act=self._is_bound_act(before),
|
||||
baseline_info=before,
|
||||
actionable_aliases=action_aliases,
|
||||
expected_role_name=expected_role_name,
|
||||
)
|
||||
before_snapshot = self._bind_snapshot(before)
|
||||
cooldown_snapshot = self._bind_snapshot(cooldown_info)
|
||||
@@ -1382,6 +1470,7 @@ class BindMixin:
|
||||
).isoformat(),
|
||||
"baseline_role_name": current_role_name,
|
||||
"baseline_is_bound_act": before_snapshot["is_bound_act"],
|
||||
"expected_role_name": expected_role_name,
|
||||
"action_act_aliases": action_aliases,
|
||||
"bind_decision": pending_decision,
|
||||
"bind_candidate_decisions": candidate_decisions,
|
||||
|
||||
@@ -156,7 +156,10 @@ def start_binding_sync(
|
||||
handbook_scope="elite",
|
||||
status="planned",
|
||||
message="等待生成绑定二维码",
|
||||
result={"payload": {}},
|
||||
result={
|
||||
"payload": {},
|
||||
"expected_role_name": str(task.customer_role_name or "").strip(),
|
||||
},
|
||||
created_by=current.id,
|
||||
platform_task_id=task.id,
|
||||
)
|
||||
@@ -305,6 +308,147 @@ def _order_target_role(
|
||||
return ""
|
||||
|
||||
|
||||
def _sync_confirmed_observation(
|
||||
db: Session,
|
||||
observation: DouyuTask,
|
||||
) -> int:
|
||||
"""用租期内更新的“当前已绑定”观测修正 stale 待确认订单。
|
||||
|
||||
查询任务本身仍不能替代 actBind;这里只接受同时满足以下条件的结果:
|
||||
- 动作 alias 已返回 bindAct=1;
|
||||
- 账号当前角色和角色绑定状态都已落到该角色;
|
||||
- 订单客户资料明确填写了同一角色;
|
||||
- 观测创建于该订单当前租约内,且晚于订单当前绑定任务。
|
||||
"""
|
||||
if (
|
||||
observation.status != "success"
|
||||
or observation.task_type not in BINDING_OBSERVATION_TASK_TYPES
|
||||
or not observation.account_id
|
||||
):
|
||||
return 0
|
||||
account = db.get(Account, int(observation.account_id))
|
||||
if account is None:
|
||||
return 0
|
||||
binding = resolve_douyu_binding(observation, account=account)
|
||||
if binding.status != BINDING_VIEW_CONFIRMED:
|
||||
return 0
|
||||
confirmed_role = str(binding.confirmed_role_name or "").strip()
|
||||
if not confirmed_role:
|
||||
return 0
|
||||
result = observation.result if isinstance(observation.result, dict) else {}
|
||||
action_aliases = {
|
||||
str(value or "").strip()
|
||||
for value in (result.get("action_act_aliases") or [])
|
||||
if str(value or "").strip()
|
||||
}
|
||||
selected_alias = str(result.get("act_alias") or "").strip()
|
||||
if action_aliases and selected_alias not in action_aliases:
|
||||
return 0
|
||||
account_role = str(account.game_name or "").strip()
|
||||
if (
|
||||
str(account.role_bind_status or "") != "bound"
|
||||
or not account_role
|
||||
or not role_names_match(account_role, confirmed_role)
|
||||
):
|
||||
return 0
|
||||
|
||||
query = db.query(PlatformFulfillmentTask).filter(
|
||||
PlatformFulfillmentTask.scope == "elite",
|
||||
PlatformFulfillmentTask.account_id == int(observation.account_id),
|
||||
PlatformFulfillmentTask.binding_status == "role_ready",
|
||||
PlatformFulfillmentTask.execution_status.notin_(
|
||||
("completed", "cancelled", "manual_required")
|
||||
),
|
||||
PlatformFulfillmentTask.customer_role_name != "",
|
||||
)
|
||||
if observation.platform_task_id:
|
||||
query = query.filter(
|
||||
PlatformFulfillmentTask.id == int(observation.platform_task_id)
|
||||
)
|
||||
tasks = query.order_by(PlatformFulfillmentTask.id.desc()).limit(20).all()
|
||||
changed = 0
|
||||
for task in tasks:
|
||||
target_role = str(task.customer_role_name or "").strip()
|
||||
if not target_role or not role_names_match(target_role, confirmed_role):
|
||||
if observation.platform_task_id:
|
||||
_record_binding_rejection(
|
||||
db,
|
||||
task,
|
||||
observation,
|
||||
reason="target_mismatch"
|
||||
if target_role
|
||||
else "missing_target",
|
||||
confirmed_role=confirmed_role,
|
||||
target_role=target_role,
|
||||
)
|
||||
continue
|
||||
lease = _lease_for_task(db, task)
|
||||
if (
|
||||
not lease_is_active(lease)
|
||||
or not source_created_within_lease(lease, observation)
|
||||
or int(observation.id) <= int(task.bind_task_id or 0)
|
||||
):
|
||||
continue
|
||||
if _block_on_role_lock(db, task, confirmed_role, observation):
|
||||
changed += 1
|
||||
continue
|
||||
if _block_on_account_lock(db, task, confirmed_role, observation):
|
||||
changed += 1
|
||||
continue
|
||||
previous_bind_task_id = task.bind_task_id
|
||||
task.bind_task_id = int(observation.id)
|
||||
task.binding_status = "bound"
|
||||
task.execution_status = "bound"
|
||||
task.issue_code = ""
|
||||
task.last_error = ""
|
||||
task.updated_at = _now()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"binding_confirmed_from_observation",
|
||||
detail={
|
||||
"sourceTaskId": int(observation.id),
|
||||
"sourceTaskType": str(observation.task_type or ""),
|
||||
"previousBindTaskId": previous_bind_task_id,
|
||||
"confirmedRoleName": confirmed_role[:128],
|
||||
"targetRoleName": target_role[:128],
|
||||
"accountId": int(account.id),
|
||||
"accountName": account.nickname or account.username,
|
||||
"accountRoleName": account_role[:128],
|
||||
},
|
||||
)
|
||||
_apply_task_lease_policy(db, task, lease=lease)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
changed += 1
|
||||
return changed
|
||||
|
||||
|
||||
def _latest_observations_for_task(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
*,
|
||||
limit: int = 20,
|
||||
) -> list[DouyuTask]:
|
||||
if not task.account_id:
|
||||
return []
|
||||
return (
|
||||
db.query(DouyuTask)
|
||||
.filter(
|
||||
DouyuTask.account_id == int(task.account_id),
|
||||
DouyuTask.task_type.in_(BINDING_OBSERVATION_TASK_TYPES),
|
||||
DouyuTask.status == "success",
|
||||
DouyuTask.id > int(task.bind_task_id or 0),
|
||||
(
|
||||
(DouyuTask.platform_task_id == int(task.id))
|
||||
| DouyuTask.platform_task_id.is_(None)
|
||||
),
|
||||
)
|
||||
.order_by(DouyuTask.id.desc())
|
||||
.limit(max(1, int(limit)))
|
||||
.all()
|
||||
)
|
||||
|
||||
|
||||
def _record_binding_rejection(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
@@ -684,24 +828,37 @@ def manual_confirm_binding_sync(
|
||||
if task.bind_task_id
|
||||
else None
|
||||
)
|
||||
if bind_task is not None and str(bind_task.status or "") in {
|
||||
"planned",
|
||||
"pending",
|
||||
"running",
|
||||
}:
|
||||
if bind_task is not None:
|
||||
result = dict(bind_task.result or {})
|
||||
previous_pending_role = str(
|
||||
result.get("pending_role_name") or result.get("role_name") or ""
|
||||
).strip()
|
||||
account_channel = str(account.game_channel or "").strip()
|
||||
area_name, _, plat_name = account_channel.partition(" · ")
|
||||
result.update(
|
||||
{
|
||||
"manual_confirmed": True,
|
||||
"manual_confirmed_role": confirmed_role,
|
||||
"manual_confirmed_by": int(current.id),
|
||||
"manual_confirm_note": note,
|
||||
"previous_pending_role_name": previous_pending_role[:128],
|
||||
"bind_phase": "manual_confirmed",
|
||||
"bind_polling": False,
|
||||
"bind_ready_for_confirm": False,
|
||||
"bind_confirmed": True,
|
||||
"pending_role_name": "",
|
||||
"role_name": confirmed_role,
|
||||
"current_role_name": confirmed_role,
|
||||
"area_name": area_name,
|
||||
"plat_name": plat_name,
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
}
|
||||
)
|
||||
bind_task.status = "stopped"
|
||||
bind_task.status = "success"
|
||||
bind_task.queue_lane = ""
|
||||
bind_task.next_run_at = None
|
||||
bind_task.run_deadline_at = None
|
||||
bind_task.message = f"人工确认绑定: {confirmed_role}"[:512]
|
||||
bind_task.finished_at = _now()
|
||||
bind_task.result = result
|
||||
@@ -962,11 +1119,21 @@ def _apply_binding_task(
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
return True
|
||||
|
||||
decision = result.get("bind_decision")
|
||||
decision_reason = (
|
||||
str(decision.get("reason") or "") if isinstance(decision, dict) else ""
|
||||
)
|
||||
same_role_stale = decision_reason.startswith("same_role_stale") or (
|
||||
not decision_reason
|
||||
and str(result.get("role_name") or "").strip()
|
||||
== str(result.get("baseline_role_name") or "").strip()
|
||||
)
|
||||
if (
|
||||
is_current
|
||||
and str(source.task_type or "") == "get_bind_qr"
|
||||
and bool(result.get("bind_ready_for_confirm"))
|
||||
and not _pending_role_of(source)
|
||||
and same_role_stale
|
||||
):
|
||||
# 兼容旧快照:同名旧角色曾被误标为待确认。清掉绑定指针,让客户页重新出码。
|
||||
task.bind_task_id = None
|
||||
@@ -1040,6 +1207,15 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]:
|
||||
)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
if (
|
||||
task.binding_status == "role_ready"
|
||||
and task.account_id
|
||||
and str(task.customer_role_name or "").strip()
|
||||
):
|
||||
for observation in _latest_observations_for_task(db, task):
|
||||
if _sync_confirmed_observation(db, observation):
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
owned = bind_task.platform_task_id is not None
|
||||
_apply_binding_task(db, task, bind_task, owned=owned)
|
||||
db.commit()
|
||||
@@ -1069,7 +1245,11 @@ def sync_task_from_binding_runner(db: Session, *, douyu_task_id: int) -> int:
|
||||
refresh_binding(db, task_id=task_id)
|
||||
changed = len(linked_task_ids)
|
||||
if douyu_task.task_type in BINDING_OBSERVATION_TASK_TYPES:
|
||||
# 查询任务只用于展示账号当前角色,永远不能结算订单。
|
||||
# 普通观测不能代替确认;只有“动作 alias 已绑 + 账号当前角色 + 客户目标”
|
||||
# 三者同时一致的租期内观测,才允许修正 stale 待确认订单。
|
||||
changed += _sync_confirmed_observation(db, douyu_task)
|
||||
if changed:
|
||||
db.commit()
|
||||
return changed
|
||||
if douyu_task.platform_task_id:
|
||||
task = (
|
||||
@@ -1178,6 +1358,19 @@ def reconcile_binding_observations(
|
||||
if _frozen(task):
|
||||
continue
|
||||
changed = False
|
||||
if (
|
||||
task.binding_status == "role_ready"
|
||||
and task.account_id
|
||||
and str(task.customer_role_name or "").strip()
|
||||
and any(
|
||||
_sync_confirmed_observation(db, observation)
|
||||
for observation in _latest_observations_for_task(db, task)
|
||||
)
|
||||
):
|
||||
changed = True
|
||||
if changed:
|
||||
reconciled += 1
|
||||
continue
|
||||
current = (
|
||||
db.get(DouyuTask, int(task.bind_task_id))
|
||||
if task.bind_task_id
|
||||
|
||||
@@ -211,18 +211,43 @@ def build_binding_evidence(
|
||||
action_pending_role = (
|
||||
_text((action_pending or {}).get("roleName")) if action_pending else ""
|
||||
)
|
||||
legacy_role = _text((legacy_latest or {}).get("roleName"))
|
||||
action_alias_has_role = any(
|
||||
item["alias"] in action_aliases and item["roleName"]
|
||||
for item in candidates
|
||||
)
|
||||
account_current_role = _text(account.game_name) if account is not None else ""
|
||||
# scannedRole 只认“本次新扫码”:
|
||||
# - 新 pending(QYOOB bindAct=0 / 可确认角色);
|
||||
# - legacy(cjm) 上出现的扫码角色;
|
||||
# - decision 明确可确认时的角色。
|
||||
# 不能用 result.role_name / before_bind_info 的 baseline 兜底,否则会把
|
||||
# 账号上一单的旧角色误报成本次扫码,详情就会和列表的“待扫码”冲突。
|
||||
decision_role = (
|
||||
_text(decision.get("role_name"))
|
||||
if decision.get("confirmable")
|
||||
and (
|
||||
selected_alias in action_aliases
|
||||
or not action_alias_has_role
|
||||
)
|
||||
else ""
|
||||
)
|
||||
scanned_role = _text(
|
||||
pending_role
|
||||
or (legacy_latest or {}).get("roleName")
|
||||
or action_pending_role
|
||||
or (decision.get("role_name") if decision.get("confirmable") else "")
|
||||
or decision_role
|
||||
)
|
||||
if (
|
||||
not scanned_role
|
||||
and action_current is not None
|
||||
and account_current_role
|
||||
and legacy_role
|
||||
and legacy_role == account_current_role
|
||||
and _text(action_current.get("roleName")) == account_current_role
|
||||
):
|
||||
scanned_role = account_current_role
|
||||
if not scanned_role and not action_alias_has_role:
|
||||
scanned_role = legacy_role
|
||||
# 取消 QYOOB-only 门禁后,cjm 的新角色同样可以成为客户确认目标;
|
||||
# 这里展示真实本次扫码角色,避免详情说“不可确认”但客户页可以确认。
|
||||
target_role = scanned_role
|
||||
@@ -231,7 +256,6 @@ def build_binding_evidence(
|
||||
or (selected_info or {}).get("role_name")
|
||||
or (result.get("before_bind_info") or {}).get("role_name")
|
||||
)
|
||||
account_current_role = _text(account.game_name) if account is not None else ""
|
||||
same_pair = bool(
|
||||
scanned_role
|
||||
and account_current_role
|
||||
|
||||
@@ -10,6 +10,7 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from ...models import (
|
||||
AccountLease,
|
||||
DouyuTask,
|
||||
PlatformFulfillmentTask,
|
||||
)
|
||||
from .binding import (
|
||||
@@ -99,6 +100,15 @@ 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
|
||||
bind_task.result = result
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
|
||||
@@ -11,6 +11,7 @@ from fastapi import HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ...models import (
|
||||
DouyuTask,
|
||||
PlatformCommodityMapping,
|
||||
PlatformFulfillmentTask,
|
||||
)
|
||||
@@ -47,6 +48,8 @@ _AUTO_SYNC_LAST_ATTEMPT = float("-inf")
|
||||
def _apply_customer_profile(
|
||||
task: PlatformFulfillmentTask,
|
||||
snapshot: dict[str, Any],
|
||||
*,
|
||||
db: Session | None = None,
|
||||
) -> None:
|
||||
"""order_site contractVersion=3 快照里的客户资料和主页截图。"""
|
||||
keys = {
|
||||
@@ -108,6 +111,15 @@ def _apply_customer_profile(
|
||||
default=None,
|
||||
)
|
||||
)
|
||||
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
|
||||
bind_task.result = result
|
||||
|
||||
|
||||
def _upsert_order_snapshot(
|
||||
@@ -227,7 +239,7 @@ def _upsert_order_snapshot(
|
||||
task.required_points,
|
||||
task.issue_code,
|
||||
)
|
||||
_apply_customer_profile(task, snapshot)
|
||||
_apply_customer_profile(task, snapshot, db=db)
|
||||
terminal = (
|
||||
terminal_order
|
||||
or _snapshot_value(
|
||||
@@ -583,7 +595,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)
|
||||
_apply_customer_profile(task, row, db=db)
|
||||
task.updated_at = _now()
|
||||
if task.account_id is None and task.execution_status in {
|
||||
"pending_mapping",
|
||||
|
||||
Reference in New Issue
Block a user