fix(douyu): 支持同名角色重新绑定
This commit is contained in:
@@ -88,14 +88,18 @@
|
||||
`existing_actionable_pending`;
|
||||
3. 没有扫码前 baseline:可确认,原因 `first_role_without_baseline`;
|
||||
4. 角色名与 baseline 不同:可确认,原因 `role_changed`;
|
||||
5. 角色名相同且 `bindAct=1`:已经绑定,不可重复确认;
|
||||
6. 同 alias 从 `bindAct=1` 转为 `bindAct=0 + needBindAct=1`:可确认,原因
|
||||
`same_alias_after_bound_transition`;
|
||||
7. 其他同名角色:视为旧残留,原因 `same_role_stale`。
|
||||
5. 角色名相同,但同 alias 仍返回 `needBindAct=1`:可确认,即使当前
|
||||
`bindAct=1`,原因 `same_alias_after_bound_transition`;
|
||||
6. 动作 alias 上 `bindAct=1 + needBindAct=1 + 同名角色`:可确认;
|
||||
7. 角色名相同且接口不再要求绑定:视为已经绑定,原因 `already_bound`;
|
||||
8. 其他同名角色:视为旧残留,原因 `same_role_stale`。
|
||||
|
||||
规则 2 是 2026-11-21 线上修复:动作 alias 已经存在同名 pending 时,它仍是可执行
|
||||
确认的 pending,不应因为“角色名没变化”一直灰掉确认按钮。
|
||||
|
||||
规则 5、6 解决“重新绑定同名角色”:业务不要求每次都必须选择一个不同的角色。
|
||||
只要官方动作 alias 仍要求绑定,同名角色也必须能进入确认流程。
|
||||
|
||||
### 完整流程
|
||||
|
||||
```text
|
||||
@@ -282,11 +286,22 @@ docker compose logs douyu-login | grep '确认前状态'
|
||||
周期/末轮补查;直接 `bindAct=1` 立即收敛;前端自动轮询期间仍允许“立即查询角色”;
|
||||
关键接口响应进入 `douyu_role_api_logs`。
|
||||
|
||||
### 2026-11-21:同名角色被误判为已经绑定
|
||||
|
||||
生产接口留档显示,活动 alias 可以同时返回 `bindAct=1`、`needBindAct=1` 和与上次
|
||||
相同的角色名。旧判定先看 `bindAct=1` 并直接返回 `already_bound`,导致用户重新
|
||||
绑定同名角色时任务一直等待。
|
||||
|
||||
修复:只要动作 alias 仍返回 `needBindAct=1`,同名角色也进入可确认状态;候选选择
|
||||
仍先选真正变化的新角色,再选择同名可确认角色,避免活动 alias 的旧快照压掉
|
||||
`cjm` 上的新角色。
|
||||
|
||||
### 历史固定规则
|
||||
|
||||
- 不要把 `bindRole=1` 当成已绑定;
|
||||
- 不要把未确认时的换绑倒计时当成冷却;
|
||||
- 不要用 `cjm` 的旧角色覆盖活动 alias pending;
|
||||
- 不要要求每次绑定都必须换一个不同的角色,同名重绑是正常流程;
|
||||
- 不要在页面层重新实现 `bindAct/needBindAct` 判断;
|
||||
- 不要把原始 `bind_summary` 当业务文案,它只用于排障。
|
||||
|
||||
@@ -294,4 +309,4 @@ docker compose logs douyu-login | grep '确认前状态'
|
||||
|
||||
| 日期 | 变更 |
|
||||
| --- | --- |
|
||||
| 2026-11-21 | 建立斗鱼协议总览;补充多 alias 职责、pending 真值表、结构化判定日志、轮询退避、直接绑定识别和角色接口原始响应留档 |
|
||||
| 2026-11-21 | 建立斗鱼协议总览;补充多 alias 职责、pending 真值表、同名重绑、结构化判定日志、轮询退避、直接绑定识别和角色接口原始响应留档 |
|
||||
|
||||
@@ -238,7 +238,34 @@ def test_same_role_after_activity_bind_transition_is_pending():
|
||||
)
|
||||
|
||||
|
||||
def test_pending_candidate_picker_ignores_legacy_same_role_residual():
|
||||
def test_same_bound_role_with_need_bind_act_is_confirmable():
|
||||
role = "少和我较劲"
|
||||
baseline = _role_candidate(
|
||||
ACT_ALIAS,
|
||||
role,
|
||||
bound_act=True,
|
||||
need_bind_act=True,
|
||||
)
|
||||
candidate = _role_candidate(
|
||||
ACT_ALIAS,
|
||||
role,
|
||||
bound_act=True,
|
||||
need_bind_act=True,
|
||||
)
|
||||
|
||||
decision = binding_pending_decision(
|
||||
candidate,
|
||||
baseline_role_name=role,
|
||||
baseline_is_bound_act=True,
|
||||
baseline_info=baseline,
|
||||
actionable_aliases=[ACT_ALIAS],
|
||||
)
|
||||
|
||||
assert decision["confirmable"] is True
|
||||
assert decision["reason"] == "same_alias_after_bound_transition"
|
||||
|
||||
|
||||
def test_pending_candidate_picker_uses_action_alias_same_role():
|
||||
old_role = "少和我较劲"
|
||||
baseline = _role_candidate(ACT_ALIAS, old_role, bound_act=True)
|
||||
legacy = _role_candidate(LEGACY_ALIAS, old_role, bound_act=False)
|
||||
@@ -252,7 +279,8 @@ def test_pending_candidate_picker_ignores_legacy_same_role_residual():
|
||||
baseline_info=baseline,
|
||||
)
|
||||
|
||||
assert selected is None
|
||||
assert selected is not None
|
||||
assert selected["act_alias"] == ACT_ALIAS
|
||||
|
||||
|
||||
def test_task_result_helpers_use_shared_same_role_transition_rule():
|
||||
|
||||
@@ -815,6 +815,75 @@ def test_qr_preflight_accepts_existing_action_alias_pending_same_role(
|
||||
assert task.result["bind_decision"]["reason"] == "existing_actionable_pending"
|
||||
|
||||
|
||||
def test_qr_preflight_accepts_same_bound_role_that_still_needs_binding(
|
||||
db, monkeypatch
|
||||
):
|
||||
"""用户重新绑定同名角色时,bindAct=1 不应阻断可确认状态。"""
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
role = "你哥奢皇"
|
||||
legacy = {
|
||||
"act_alias": "cjm",
|
||||
"role_name": role,
|
||||
"area_name": "微信",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_account": True,
|
||||
"is_bound_role": True,
|
||||
"is_bound_act": False,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": True,
|
||||
"change_role_wait_time": 0,
|
||||
"can_change_time": 0,
|
||||
"can_change_role": True,
|
||||
}
|
||||
activity = {
|
||||
**legacy,
|
||||
"act_alias": "20260120QYOOB",
|
||||
"is_bound_act": True,
|
||||
"need_bind_act": True,
|
||||
"can_change_time": 1_790_870_400,
|
||||
}
|
||||
client = type(
|
||||
"Client",
|
||||
(),
|
||||
{"get_bind_qr": lambda self, _alias: {"url": "https://example.test/qr"}},
|
||||
)()
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"get_bind_qr",
|
||||
close_db=False,
|
||||
)
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: client)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_fetch_bind_info_candidates",
|
||||
lambda *_args: [legacy, activity],
|
||||
)
|
||||
|
||||
runner._execute_get_bind_qr(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
"PHPSESSID=x",
|
||||
{
|
||||
"bind_act_alias": "20260120QYOOB",
|
||||
"confirm_act_alias": "20260120QYOOB",
|
||||
"legacy_act_alias": "cjm",
|
||||
},
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert task.status == "success"
|
||||
assert task.result["url"] == "https://example.test/qr"
|
||||
assert task.result["pending_role_name"] == role
|
||||
assert task.result["bind_ready_for_confirm"] is True
|
||||
assert task.result["bind_decision"]["alias"] == "20260120QYOOB"
|
||||
assert (
|
||||
task.result["bind_decision"]["reason"]
|
||||
== "same_alias_after_bound_transition"
|
||||
)
|
||||
|
||||
|
||||
def test_binding_poll_accepts_existing_action_alias_pending_same_role(
|
||||
db, monkeypatch
|
||||
):
|
||||
|
||||
@@ -295,10 +295,9 @@ def binding_pending_decision(
|
||||
) -> dict[str, Any]:
|
||||
"""解释一个绑定候选是否可进入确认阶段。
|
||||
|
||||
角色名不是唯一事实:用户可能换绑到同名角色。判断还要对比扫码前后的
|
||||
活动绑定状态,尤其是活动 alias 从 ``bindAct=1`` 转为
|
||||
``bindAct=0 + needBindAct=1`` 的待确认转换。活动 alias 上已经存在的
|
||||
同名 pending 也不是“旧角色残留”,它仍然可以调用确认接口。
|
||||
角色名不是唯一事实,也不能要求每次都必须换新角色。用户重新绑定同名角色
|
||||
时,判断还要对比扫码前后的活动绑定状态。活动 alias 只要仍要求
|
||||
``needBindAct=1``,即使当前 ``bindAct=1``,同名角色也可以进入确认流程。
|
||||
"""
|
||||
result: dict[str, Any] = {
|
||||
"alias": str((info or {}).get("act_alias") or ""),
|
||||
@@ -354,10 +353,10 @@ def binding_pending_decision(
|
||||
if role_name.casefold() != baseline.casefold():
|
||||
result.update({"confirmable": True, "reason": "role_changed"})
|
||||
return result
|
||||
if snapshot["is_bound_act"]:
|
||||
result["reason"] = "already_bound"
|
||||
return result
|
||||
if not isinstance(baseline_info, dict):
|
||||
if snapshot["is_bound_act"]:
|
||||
result["reason"] = "already_bound"
|
||||
return result
|
||||
result["reason"] = "same_role_without_baseline_info"
|
||||
return result
|
||||
|
||||
@@ -387,6 +386,9 @@ def binding_pending_decision(
|
||||
}
|
||||
)
|
||||
return result
|
||||
if snapshot["is_bound_act"]:
|
||||
result["reason"] = "already_bound"
|
||||
return result
|
||||
result["reason"] = "same_role_stale"
|
||||
return result
|
||||
|
||||
|
||||
@@ -290,23 +290,34 @@ def resolve_elite_role(
|
||||
)
|
||||
pending_info = None
|
||||
pending_decision: dict[str, Any] | None = None
|
||||
for alias in ordered_aliases:
|
||||
for info in candidates_info:
|
||||
if str(info.get("act_alias") or "") != alias:
|
||||
continue
|
||||
decision = binding_pending_decision(
|
||||
info,
|
||||
baseline_role_name=str((bound_info or {}).get("role_name") or ""),
|
||||
baseline_is_bound_act=is_bound_act(bound_info),
|
||||
baseline_info=bound_info,
|
||||
actionable_aliases=action_aliases,
|
||||
)
|
||||
if decision["confirmable"]:
|
||||
pending_info = info
|
||||
pending_decision = decision
|
||||
break
|
||||
if pending_info is not None:
|
||||
break
|
||||
baseline_role = str((bound_info or {}).get("role_name") or "").strip()
|
||||
|
||||
def find_pending(*, changed_role_only: bool) -> tuple[dict | None, dict | None]:
|
||||
for alias in ordered_aliases:
|
||||
for info in candidates_info:
|
||||
if str(info.get("act_alias") or "") != alias:
|
||||
continue
|
||||
role_name = str(info.get("role_name") or "").strip()
|
||||
if (
|
||||
changed_role_only
|
||||
and baseline_role
|
||||
and role_name.casefold() == baseline_role.casefold()
|
||||
):
|
||||
continue
|
||||
decision = binding_pending_decision(
|
||||
info,
|
||||
baseline_role_name=baseline_role,
|
||||
baseline_is_bound_act=is_bound_act(bound_info),
|
||||
baseline_info=bound_info,
|
||||
actionable_aliases=action_aliases,
|
||||
)
|
||||
if decision["confirmable"]:
|
||||
return info, decision
|
||||
return None, None
|
||||
|
||||
pending_info, pending_decision = find_pending(changed_role_only=True)
|
||||
if pending_info is None:
|
||||
pending_info, pending_decision = find_pending(changed_role_only=False)
|
||||
if bound_info is not None:
|
||||
return _role_result(
|
||||
status=ROLE_BOUND,
|
||||
|
||||
@@ -637,7 +637,22 @@ class BindMixin:
|
||||
“是否 pending”只调用 ``_is_pending_role``;二维码轮询、查询角色和换绑
|
||||
时间查询都从这里选候选,避免各自用角色名或 bindRole 再写一套判断。
|
||||
"""
|
||||
for info in self._ordered_bind_candidates(candidates, prefer_aliases):
|
||||
ordered = self._ordered_bind_candidates(candidates, prefer_aliases)
|
||||
baseline = baseline_role_name.strip().casefold()
|
||||
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
|
||||
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,
|
||||
):
|
||||
return info
|
||||
for info in ordered:
|
||||
if self._is_pending_role(
|
||||
info,
|
||||
baseline_role_name=baseline_role_name,
|
||||
|
||||
Reference in New Issue
Block a user