fix(fulfillment): 拆分绑定事实并复用同账号角色
- 详情页增加 QYOOB / cjm / 7 天锁绑定判定面板 - 订单目标只认动作 alias pending,cjm-only 不再推进订单 - 同账号同角色直接复用已有绑定,不再等待二维码或调 actBind - 补账号侧 7 天锁本地检查 - 补充绑定证据、同对复用和 7 天锁回归测试
This commit is contained in:
@@ -51,7 +51,14 @@ from .repository import (
|
||||
_load_task,
|
||||
_load_task_for_update,
|
||||
)
|
||||
from .role_lock import ROLE_LOCK_ISSUE, RoleLock, find_role_lock
|
||||
from .role_lock import (
|
||||
ACCOUNT_LOCK_ISSUE,
|
||||
ROLE_LOCK_ISSUE,
|
||||
AccountLock,
|
||||
RoleLock,
|
||||
find_account_lock,
|
||||
find_role_lock,
|
||||
)
|
||||
from .runtime import (
|
||||
_append_event,
|
||||
_apply_task_lease_policy,
|
||||
@@ -239,10 +246,37 @@ 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 pending_binding_role_from_result(task.result)
|
||||
return _actionable_pending_role(
|
||||
task.result,
|
||||
pending_binding_role_from_result(task.result),
|
||||
)
|
||||
|
||||
|
||||
def _owned_binding_tasks(
|
||||
@@ -376,6 +410,55 @@ def _mark_role_locked(
|
||||
return True
|
||||
|
||||
|
||||
def _mark_account_locked(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
source: DouyuTask | None,
|
||||
lock: AccountLock,
|
||||
) -> bool:
|
||||
"""当前分配的账号 7 天内绑过别的角色:转人工,不调 actBind。"""
|
||||
if str(task.issue_code or "") == ACCOUNT_LOCK_ISSUE:
|
||||
return False
|
||||
message = lock.message()
|
||||
lease = _lease_for_task(db, task)
|
||||
if source is not None and int(source.id) > int(task.bind_task_id or 0):
|
||||
task.bind_task_id = int(source.id)
|
||||
task.binding_status = "failed"
|
||||
task.execution_status = "manual_required"
|
||||
task.issue_code = ACCOUNT_LOCK_ISSUE
|
||||
task.last_error = message
|
||||
task.updated_at = _now()
|
||||
detail = {
|
||||
"accountRoleName": lock.role_name,
|
||||
"accountBoundAt": lock.bound_at.isoformat(),
|
||||
"accountReusableAt": lock.reusable_at.isoformat(),
|
||||
"allocatedAccountId": task.account_id,
|
||||
"allocatedAccountName": lock.account_name,
|
||||
"message": message,
|
||||
}
|
||||
if source is not None:
|
||||
detail["sourceTaskId"] = int(source.id)
|
||||
detail["sourceTaskType"] = str(source.task_type or "")
|
||||
_append_event(db, task.id, "binding_account_locked_elsewhere", detail=detail)
|
||||
_apply_task_lease_policy(db, task, lease=lease)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
return True
|
||||
|
||||
|
||||
def _block_on_account_lock(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
role_name: str,
|
||||
source: DouyuTask | None = None,
|
||||
) -> bool:
|
||||
if str(task.issue_code or "") == ACCOUNT_LOCK_ISSUE:
|
||||
return False
|
||||
lock = find_account_lock(db, task.account_id, role_name)
|
||||
if lock is None:
|
||||
return False
|
||||
return _mark_account_locked(db, task, source, lock)
|
||||
|
||||
|
||||
def _block_on_role_lock(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
@@ -427,6 +510,121 @@ def _apply_binding_qr_display(
|
||||
return True
|
||||
|
||||
|
||||
def _same_pair_reuse_role(
|
||||
result: dict[str, Any] | None,
|
||||
account: Account | None,
|
||||
) -> str:
|
||||
"""本次扫码角色已经绑在当前账号上:同一对,直接复用。
|
||||
|
||||
只认 **cjm/legacy 上出现的扫码角色**,并且要求动作 alias 上也是同一个
|
||||
角色且 bindAct=1。动作 alias 上只要出现 bindAct=0 的 pending,就说明客户
|
||||
扫的是新角色,必须走正常确认,不能复用旧角色。
|
||||
"""
|
||||
if not isinstance(result, dict) or account is None:
|
||||
return ""
|
||||
if str(account.role_bind_status or "") != "bound":
|
||||
return ""
|
||||
current = str(account.game_name or "").strip()
|
||||
if not current:
|
||||
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 []
|
||||
)
|
||||
candidates = [
|
||||
raw
|
||||
for raw in (result.get("bind_candidates") or [])
|
||||
if isinstance(raw, dict)
|
||||
]
|
||||
action_candidates = [
|
||||
raw
|
||||
for raw in candidates
|
||||
if str(raw.get("act_alias") or raw.get("alias") or "").strip()
|
||||
in action_aliases
|
||||
]
|
||||
if any(
|
||||
str(raw.get("role_name") or "").strip()
|
||||
and not bool(raw.get("is_bound_act"))
|
||||
for raw in action_candidates
|
||||
):
|
||||
# 动作 alias 上已有新的 pending:客户扫了新角色,交给正常确认。
|
||||
return ""
|
||||
selected_alias = str(result.get("act_alias") or "").strip()
|
||||
legacy_role = next(
|
||||
(
|
||||
str(raw.get("role_name") or "").strip()
|
||||
for raw in candidates
|
||||
if str(raw.get("act_alias") or raw.get("alias") or "").strip()
|
||||
not in action_aliases
|
||||
and str(raw.get("role_name") or "").strip()
|
||||
),
|
||||
"",
|
||||
)
|
||||
if selected_alias and selected_alias in action_aliases:
|
||||
scanned_role = legacy_role
|
||||
else:
|
||||
scanned_role = str(
|
||||
result.get("pending_role_name")
|
||||
or result.get("role_name")
|
||||
or legacy_role
|
||||
or ""
|
||||
).strip()
|
||||
if not scanned_role or scanned_role != current:
|
||||
return ""
|
||||
if action_candidates:
|
||||
action_bound = any(
|
||||
str(raw.get("role_name") or "").strip() == current
|
||||
and bool(raw.get("is_bound_act"))
|
||||
for raw in action_candidates
|
||||
)
|
||||
return current if action_bound else ""
|
||||
if (
|
||||
(not action_aliases or selected_alias not in action_aliases)
|
||||
and str(result.get("current_role_name") or "").strip() == current
|
||||
):
|
||||
return current
|
||||
return ""
|
||||
|
||||
|
||||
def _mark_reused_pair(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
source: DouyuTask,
|
||||
*,
|
||||
role_name: str,
|
||||
account: Account | None,
|
||||
) -> bool:
|
||||
lease = _lease_for_task(db, task)
|
||||
task.bind_task_id = int(source.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_reused_existing_pair",
|
||||
detail={
|
||||
"roleName": role_name[:128],
|
||||
"accountId": task.account_id,
|
||||
"accountName": (
|
||||
(account.nickname or account.username)
|
||||
if account is not None
|
||||
else ""
|
||||
),
|
||||
"sourceTaskId": int(source.id),
|
||||
"sourceTaskType": str(source.task_type or ""),
|
||||
"reason": "same_account_same_role",
|
||||
},
|
||||
)
|
||||
_apply_task_lease_policy(db, task, lease=lease)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
return True
|
||||
|
||||
|
||||
def _apply_binding_task(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
@@ -441,8 +639,8 @@ def _apply_binding_task(
|
||||
"""
|
||||
if _frozen(task):
|
||||
return False
|
||||
if str(task.issue_code or "") == ROLE_LOCK_ISSUE:
|
||||
# 角色 7 天锁只能由人工重置后重新进入流程。
|
||||
if str(task.issue_code or "") in {ROLE_LOCK_ISSUE, ACCOUNT_LOCK_ISSUE}:
|
||||
# 7 天锁只能由人工重置后重新进入流程。
|
||||
return False
|
||||
if not task.account_id or not source.account_id:
|
||||
return False
|
||||
@@ -471,6 +669,16 @@ def _apply_binding_task(
|
||||
return False
|
||||
|
||||
account = db.get(Account, int(task.account_id))
|
||||
if str(source.task_type or "") == "get_bind_qr" and phase != "confirming":
|
||||
reused_role = _same_pair_reuse_role(source.result, account)
|
||||
if reused_role:
|
||||
return _mark_reused_pair(
|
||||
db,
|
||||
task,
|
||||
source,
|
||||
role_name=reused_role,
|
||||
account=account,
|
||||
)
|
||||
binding = resolve_fulfillment_binding(
|
||||
source,
|
||||
account=account,
|
||||
@@ -601,6 +809,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
|
||||
if phase == "confirming":
|
||||
# 确认任务执行中,后到的扫码结果不能把订单打回待确认、丢掉确认任务指针。
|
||||
return False
|
||||
@@ -615,6 +826,8 @@ def _apply_binding_task(
|
||||
return False
|
||||
if _block_on_role_lock(db, task, role_name, source):
|
||||
return True
|
||||
if _block_on_account_lock(db, task, role_name, source):
|
||||
return True
|
||||
task.bind_task_id = int(source.id)
|
||||
task.binding_status = "role_ready"
|
||||
task.execution_status = "role_ready"
|
||||
@@ -891,11 +1104,19 @@ def reconcile_binding_observations(
|
||||
if not changed and task.binding_status == "role_ready":
|
||||
# 部署前已经 role_ready 的订单:启动时补一次角色 7 天锁检查。
|
||||
target_role_name = _order_target_role(db, task)
|
||||
if target_role_name and _block_on_role_lock(
|
||||
db,
|
||||
task,
|
||||
target_role_name,
|
||||
current,
|
||||
if target_role_name and (
|
||||
_block_on_role_lock(
|
||||
db,
|
||||
task,
|
||||
target_role_name,
|
||||
current,
|
||||
)
|
||||
or _block_on_account_lock(
|
||||
db,
|
||||
task,
|
||||
target_role_name,
|
||||
current,
|
||||
)
|
||||
):
|
||||
changed = True
|
||||
if changed:
|
||||
@@ -938,7 +1159,10 @@ def _latest_pending_binding_task(
|
||||
for row in rows:
|
||||
if not source_created_within_lease(lease, row):
|
||||
continue
|
||||
if resolve_douyu_binding(row, account=account).status == BINDING_VIEW_PENDING:
|
||||
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 None
|
||||
|
||||
@@ -961,6 +1185,10 @@ 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 (
|
||||
@@ -978,6 +1206,8 @@ def _attach_pending_binding_task(
|
||||
return False
|
||||
if _block_on_role_lock(db, task, binding.pending_role_name, bind_task):
|
||||
return True
|
||||
if _block_on_account_lock(db, task, binding.pending_role_name, bind_task):
|
||||
return True
|
||||
previous_bind_task_id = task.bind_task_id
|
||||
task.bind_task_id = int(bind_task.id)
|
||||
task.binding_status = "role_ready"
|
||||
@@ -1187,7 +1417,23 @@ def confirm_binding_sync(db: Session, *, task_id: int, current: User) -> dict[st
|
||||
):
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
if target_role_name and _block_on_account_lock(
|
||||
db,
|
||||
task,
|
||||
target_role_name,
|
||||
current_bind_task,
|
||||
):
|
||||
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(
|
||||
|
||||
@@ -0,0 +1,278 @@
|
||||
"""履约绑定判定的只读证据。
|
||||
|
||||
把一个 DouyuTask 的结果拆成互不混淆的几层事实:
|
||||
|
||||
- ``actionCurrent``:动作 alias(QYOOB)上已经 bindAct=1 的角色;
|
||||
- ``actionPending``:动作 alias 上 bindAct=0、可确认的本次待确认角色;
|
||||
- ``legacyLatest``:legacy(cjm)上的最新角色,只能提示,不能单独确认;
|
||||
- ``accountCurrentRole``:账号表当前生效角色(覆盖式,不代表本次);
|
||||
- ``roleLock`` / ``accountLock``:7 天锁,是“能不能绑”的资格,不是角色识别。
|
||||
|
||||
这个模块只读,不改变订单状态。页面用它把“新扫的 / 旧的 / cjm 的 / 7 天锁的”
|
||||
分开显示,避免再把它们混成一个“待确认角色”。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
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 .role_lock import AccountLock, RoleLock, find_account_lock, find_role_lock
|
||||
|
||||
|
||||
def _text(value: Any) -> str:
|
||||
return str(value or "").strip()
|
||||
|
||||
|
||||
def _bool(value: Any) -> bool:
|
||||
return bool(value)
|
||||
|
||||
|
||||
def _channel(area: str, plat: str) -> str:
|
||||
return " / ".join(value for value in (area, plat) if value)
|
||||
|
||||
|
||||
def _candidate(raw: dict[str, Any]) -> dict[str, Any]:
|
||||
alias = _text(raw.get("act_alias") or raw.get("alias"))
|
||||
area = _text(raw.get("area_name"))
|
||||
plat = _text(raw.get("plat_name"))
|
||||
return {
|
||||
"alias": alias,
|
||||
"roleName": _text(raw.get("role_name")),
|
||||
"areaName": area,
|
||||
"platName": plat,
|
||||
"channel": _channel(area, plat),
|
||||
"bindAct": _bool(raw.get("is_bound_act") or raw.get("bind_act")),
|
||||
"bindRole": _bool(raw.get("is_bound_role") or raw.get("bind_role")),
|
||||
"needBindAct": _bool(raw.get("need_bind_act")),
|
||||
"actionableAlias": _bool(
|
||||
raw.get("actionable_alias") or raw.get("actionableAlias")
|
||||
),
|
||||
"confirmable": _bool(raw.get("confirmable")),
|
||||
"reason": _text(raw.get("reason")),
|
||||
}
|
||||
|
||||
|
||||
def _candidate_from_info(
|
||||
info: Any,
|
||||
action_aliases: list[str],
|
||||
) -> dict[str, Any] | None:
|
||||
if not isinstance(info, dict):
|
||||
return None
|
||||
raw = {
|
||||
**info,
|
||||
"act_alias": info.get("act_alias") or info.get("alias"),
|
||||
}
|
||||
candidate = _candidate(raw)
|
||||
candidate["actionableAlias"] = candidate["alias"] in action_aliases
|
||||
return candidate if candidate["roleName"] else None
|
||||
|
||||
|
||||
def _lock_out(lock: RoleLock | AccountLock | None) -> dict[str, Any] | None:
|
||||
if lock is None:
|
||||
return None
|
||||
data = {
|
||||
"roleName": lock.role_name,
|
||||
"accountId": lock.account_id,
|
||||
"accountName": lock.account_name,
|
||||
"boundAt": lock.bound_at.isoformat(),
|
||||
"reusableAt": lock.reusable_at.isoformat(),
|
||||
"message": lock.message(),
|
||||
}
|
||||
if isinstance(lock, RoleLock):
|
||||
data["leasePurpose"] = lock.lease_purpose
|
||||
data["leaseBatchId"] = lock.lease_batch_id
|
||||
return data
|
||||
|
||||
|
||||
def _account_current_candidate(
|
||||
account: Account | None,
|
||||
action_alias: str,
|
||||
) -> dict[str, Any] | None:
|
||||
if account is None or not _text(account.game_name):
|
||||
return None
|
||||
if str(account.role_bind_status or "") != "bound":
|
||||
return None
|
||||
channel = _text(account.game_channel)
|
||||
area, _, plat = channel.partition(" · ")
|
||||
return {
|
||||
"alias": action_alias,
|
||||
"roleName": _text(account.game_name),
|
||||
"areaName": area,
|
||||
"platName": plat,
|
||||
"channel": channel,
|
||||
"bindAct": True,
|
||||
"bindRole": True,
|
||||
"needBindAct": False,
|
||||
"actionableAlias": bool(action_alias),
|
||||
"confirmable": False,
|
||||
"reason": "account_current_bound",
|
||||
}
|
||||
|
||||
|
||||
def build_binding_evidence(
|
||||
db: Session,
|
||||
task: PlatformFulfillmentTask,
|
||||
bind_task: DouyuTask | None,
|
||||
account: Account | None,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
"""构建页面用的绑定判定快照;无绑定任务时返回 None。"""
|
||||
if bind_task is None:
|
||||
return None
|
||||
result = bind_task.result if isinstance(bind_task.result, dict) else {}
|
||||
action_aliases = [
|
||||
_text(value)
|
||||
for value in (result.get("action_act_aliases") or [])
|
||||
if _text(value)
|
||||
]
|
||||
candidates = [
|
||||
_candidate(raw)
|
||||
for raw in (result.get("bind_candidates") or [])
|
||||
if isinstance(raw, dict)
|
||||
]
|
||||
candidates = [item for item in candidates if item["alias"]]
|
||||
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 {}
|
||||
selected_actionable = bool(
|
||||
decision.get("actionable_alias")
|
||||
or (selected_alias and selected_alias in action_aliases)
|
||||
)
|
||||
|
||||
action_current = next(
|
||||
(
|
||||
item
|
||||
for item in candidates
|
||||
if item["alias"] in action_aliases
|
||||
and item["bindAct"]
|
||||
and item["roleName"]
|
||||
),
|
||||
None,
|
||||
)
|
||||
if action_current is None:
|
||||
action_current = _account_current_candidate(
|
||||
account,
|
||||
action_aliases[0] if action_aliases else "",
|
||||
)
|
||||
action_pending = next(
|
||||
(
|
||||
item
|
||||
for item in candidates
|
||||
if item["alias"] in action_aliases
|
||||
and item["roleName"]
|
||||
and not item["bindAct"]
|
||||
),
|
||||
None,
|
||||
)
|
||||
if (
|
||||
action_pending is None
|
||||
and selected_info_candidate is not None
|
||||
and selected_info_candidate["alias"] in action_aliases
|
||||
and selected_info_candidate["roleName"]
|
||||
and not selected_info_candidate["bindAct"]
|
||||
):
|
||||
action_pending = selected_info_candidate
|
||||
legacy_latest = next(
|
||||
(
|
||||
item
|
||||
for item in candidates
|
||||
if item["alias"] not in action_aliases and item["roleName"]
|
||||
),
|
||||
None,
|
||||
)
|
||||
if (
|
||||
legacy_latest is None
|
||||
and selected_info_candidate is not None
|
||||
and selected_info_candidate["alias"] not in action_aliases
|
||||
and selected_info_candidate["roleName"]
|
||||
):
|
||||
legacy_latest = selected_info_candidate
|
||||
|
||||
pending_role = pending_binding_role_from_result(result)
|
||||
scanned_role = _text(
|
||||
decision.get("role_name")
|
||||
or result.get("role_name")
|
||||
or (legacy_latest or {}).get("roleName")
|
||||
or pending_role
|
||||
)
|
||||
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
|
||||
and account_current_role
|
||||
and scanned_role == account_current_role
|
||||
and action_current is not None
|
||||
and action_current.get("bindAct")
|
||||
and action_current.get("roleName") == scanned_role
|
||||
)
|
||||
bound = str(bind_task.status or "") == "success" and bool(
|
||||
result.get("bind_confirmed")
|
||||
)
|
||||
confirmable = bool(
|
||||
target_role
|
||||
and str(bind_task.status or "") == "success"
|
||||
and task.binding_status == "role_ready"
|
||||
and not bound
|
||||
)
|
||||
role_lock = (
|
||||
find_role_lock(
|
||||
db,
|
||||
scanned_role,
|
||||
current_account_id=task.account_id,
|
||||
now=now,
|
||||
)
|
||||
if scanned_role
|
||||
else None
|
||||
)
|
||||
account_lock = find_account_lock(
|
||||
db,
|
||||
task.account_id,
|
||||
scanned_role,
|
||||
now=now,
|
||||
)
|
||||
return {
|
||||
"sourceTaskId": int(bind_task.id),
|
||||
"sourceTaskType": str(bind_task.task_type or ""),
|
||||
"sourceTaskStatus": str(bind_task.status or ""),
|
||||
"actionAliases": action_aliases,
|
||||
"selectedAlias": selected_alias,
|
||||
"selectedActionable": selected_actionable,
|
||||
"decisionReason": _text(
|
||||
decision.get("reason") or result.get("bind_phase")
|
||||
),
|
||||
"confirmable": confirmable,
|
||||
"targetRole": target_role,
|
||||
"scannedRole": scanned_role,
|
||||
"samePair": same_pair,
|
||||
"actionCurrent": action_current,
|
||||
"actionPending": action_pending,
|
||||
"legacyLatest": legacy_latest,
|
||||
"accountCurrentRole": account_current_role,
|
||||
"accountBindStatus": (
|
||||
str(account.role_bind_status or "") if account is not None else ""
|
||||
),
|
||||
"roleLock": _lock_out(role_lock),
|
||||
"accountLock": _lock_out(account_lock),
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["build_binding_evidence"]
|
||||
@@ -27,6 +27,7 @@ def _customer_task_message_for(issue_code: str) -> str:
|
||||
"account_role_cooldown": "当前斗鱼账号未到换绑时间,请截图本页面并联系客服处理",
|
||||
"customer_role_cooldown": "该游戏角色仍在换绑冷却期,请截图本页面并联系客服处理",
|
||||
"customer_role_bound_elsewhere": "该游戏角色 7 天内已绑定其他斗鱼账号,请使用原账号或等待",
|
||||
"account_role_bound_elsewhere": "当前斗鱼账号 7 天内已绑定其他角色,请换号或等待",
|
||||
"eligibility_check_failed": "状态查询异常,请稍后刷新",
|
||||
"manual_required": "当前需要人工处理,请联系客服",
|
||||
}
|
||||
|
||||
@@ -78,6 +78,7 @@ def manual_reason(issue_code: str, exchange_status: str) -> str:
|
||||
"account_role_cooldown",
|
||||
"customer_role_cooldown",
|
||||
"customer_role_bound_elsewhere",
|
||||
"account_role_bound_elsewhere",
|
||||
}:
|
||||
return "binding_failed"
|
||||
if issue in {"lease_expired", "manual_required"}:
|
||||
|
||||
@@ -27,6 +27,9 @@ from ...models import Account, AccountEvent, AccountLease
|
||||
#: 新的履约 issue code:用户角色 7 天内已绑定其他斗鱼账号。
|
||||
ROLE_LOCK_ISSUE = "customer_role_bound_elsewhere"
|
||||
|
||||
#: 新的履约 issue code:当前分配的斗鱼账号 7 天内已绑定其他角色。
|
||||
ACCOUNT_LOCK_ISSUE = "account_role_bound_elsewhere"
|
||||
|
||||
#: 官方规则窗口。
|
||||
ROLE_LOCK_WINDOW = timedelta(days=7)
|
||||
|
||||
@@ -39,6 +42,23 @@ _LEASE_PURPOSE_LABELS = {
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AccountLock:
|
||||
role_name: str
|
||||
account_id: int
|
||||
account_name: str
|
||||
bound_at: datetime
|
||||
reusable_at: datetime
|
||||
|
||||
def message(self) -> str:
|
||||
return (
|
||||
f"该斗鱼账号 7 天内已绑定角色({self.role_name}),"
|
||||
f"绑定时间 {_beijing_text(self.bound_at)},"
|
||||
f"预计 {_beijing_text(self.reusable_at)} 后可再次换绑;"
|
||||
"如需绑定新角色请换号或等待。"
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RoleLock:
|
||||
role_name: str
|
||||
@@ -143,9 +163,68 @@ def find_role_lock(
|
||||
)
|
||||
|
||||
|
||||
def find_account_lock(
|
||||
db: Session,
|
||||
account_id: int | None,
|
||||
scanned_role: str,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
) -> AccountLock | None:
|
||||
"""当前分配的斗鱼账号 7 天内是否已经绑定过别的角色。
|
||||
|
||||
同一角色(同一对)不算锁:复用已有绑定即可。
|
||||
"""
|
||||
role = str(scanned_role or "").strip()
|
||||
if account_id is None or not role:
|
||||
return None
|
||||
moment = now or datetime.now(UTC)
|
||||
since = moment - ROLE_LOCK_WINDOW
|
||||
event = (
|
||||
db.query(AccountEvent)
|
||||
.filter(
|
||||
AccountEvent.event_type == "bind_ok",
|
||||
AccountEvent.subject_type == "elite_role",
|
||||
AccountEvent.account_id == int(account_id),
|
||||
AccountEvent.occurred_at >= since,
|
||||
AccountEvent.occurred_at <= moment,
|
||||
)
|
||||
.order_by(AccountEvent.occurred_at.desc(), AccountEvent.id.desc())
|
||||
.first()
|
||||
)
|
||||
if event is None:
|
||||
return None
|
||||
bound_role = str(event.subject_key or "").strip()
|
||||
if not bound_role or bound_role == role:
|
||||
return None
|
||||
account = db.get(Account, int(account_id))
|
||||
account_name = (
|
||||
(
|
||||
str(account.nickname or "").strip()
|
||||
or str(account.username or "").strip()
|
||||
or f"账号#{account.id}"
|
||||
)
|
||||
if account is not None
|
||||
else f"账号#{account_id}"
|
||||
)
|
||||
bound_at = (
|
||||
event.occurred_at
|
||||
if event.occurred_at.tzinfo is not None
|
||||
else event.occurred_at.replace(tzinfo=UTC)
|
||||
)
|
||||
return AccountLock(
|
||||
role_name=bound_role,
|
||||
account_id=int(account_id),
|
||||
account_name=account_name,
|
||||
bound_at=bound_at,
|
||||
reusable_at=bound_at + ROLE_LOCK_WINDOW,
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"ROLE_LOCK_ISSUE",
|
||||
"ROLE_LOCK_WINDOW",
|
||||
"AccountLock",
|
||||
"RoleLock",
|
||||
"find_account_lock",
|
||||
"find_role_lock",
|
||||
]
|
||||
|
||||
@@ -33,6 +33,7 @@ from ..douyu_binding import (
|
||||
)
|
||||
from ..file_service import build_file_url
|
||||
from . import classification
|
||||
from .binding_evidence import build_binding_evidence
|
||||
from .binding_status import (
|
||||
_customer_task_message,
|
||||
_customer_task_message_for,
|
||||
@@ -493,6 +494,11 @@ def _task_out(
|
||||
if bind_task
|
||||
else None
|
||||
),
|
||||
"bindingEvidence": (
|
||||
build_binding_evidence(db, task, bind_task, account)
|
||||
if include_qr and bind_task is not None
|
||||
else None
|
||||
),
|
||||
"exchangeTaskId": exchange_task_id,
|
||||
"exchangeResultImage": (
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user