feat(account): S6 bind_status 三列拆解 —— 四值枚举、唯一写入口、前端改读账号
`accounts.bind_status` 是个**语义垃圾场**:一个自由文本列,6 个文件、**29 处写入**、
20 多种取值,混了三种东西 —— 真状态(`bind_confirmed`)、动作痕迹(`goods_locked`)、
纯查询痕迹(`points_queried`)。它是**覆盖式**的,所以 `bind_confirmed` 会被之后
任意一次查询盖掉 —— 「这个账号到底绑没绑」根本查不出来。
`esports_bind_status` / `xpd_bind_status` 是同一个病。
## 收敛成四值 + 三列一义
`unknown` / `unbound` / `pending` / `bound`,每个作用域一列:
| 列 | 作用域 |
| --- | --- |
| `accounts.role_bind_status`(**新增**) | 精英宝典角色 |
| `accounts.esports_bind_status`(收敛) | 电竞手册角色 |
| `accounts.xpd_bind_status`(收敛) | 和平小店角色 |
- 新增 `services/account_binding.py`:唯一写入口 `set_bind_status()`,
传旧自由文本直接 `ValueError`(否则垃圾会被写回去);`map_legacy_bind_status()` 负责旧值映射。
- **29 处自由文本写入全部删除或改走枚举**:真状态走枚举,动作/查询痕迹**不再写状态列**
(动作已由 S5 的直发事件记录,查询痕迹本来就不该是状态)。
- `accounts.bind_status` 保留原值便于比对,**已无任何写入方**,待 S8 drop。
- 一条**源码扫描用例**禁止任何地方再出现 `account.bind_status = "..."` 这类写入。
## 回填不猜(设计文档 §5.5)
只映射**明确陈述了绑定关系**的取值(`bind_confirmed`/`esports_bound`/`xpd_bound` → bound,
`*_qr_*`/`*_ready` → pending,`game_not_bound`/`xpd_not_bound` → unbound),
**其余一律 unknown**。因为列是覆盖式的,「最后一件事是查询」并不代表没绑定。
迁移 SQL 与 Python 映射有一致性用例:**解析迁移源码里的 CASE 逐条比对**
(已反向验证:故意改错一条映射,用例立刻红)。
## ⚠️ 顺带救回一个差点被我自己丢掉的状态
「**电竞手册是否已开通**」以前**只寄生在 `esports_bind_status` 的 `esports_opened`
取值里**(`elite_opened` 有独立布尔列,电竞没有)。归一化会把那个取值抹成 `unknown`
→ **静默丢状态**。所以:
- 新增 `accounts.esports_opened` 布尔列(与 `elite_opened` 对称),开通任务成功时置位;
- 迁移里**必须先 `save_esports_opened()` 再归一化**,顺序用例钉住;
- 详情页「电竞手册」开通状态改读 `esports_opened`。
## 前端不再从任务日志反推账号状态
工作台原来从**当前加载的那一页任务**里挑每个账号最新的一条来推绑定状态
(`latestConfirmBindTaskByAccount` 等)。两个问题:① 支撑任务一旦翻出窗口,
**显示的状态就是错的**;② 这让任务日志变成「账号状态库」,于是它永远不敢清理。
现在状态读账号字段,任务日志只补「扫码流程中的瞬时信息」(如确认前的
`pending_role_name`)。删掉了两个不再需要的派生与对应的 `clearPending` 绕行逻辑。
## 踩到并修掉的东西
- **模型与迁移不一致**:我把模型收窄成 `String(16)`,迁移却留着 `String(32)` ——
测试库按模型建、生产按迁移建,两边列宽不同。已让迁移也收窄,且**收窄必须在
归一化之后**(旧取值最长 19 字符,先收窄会截断成废值)。downgrade 也补齐了反向操作,
已实测 `downgrade -1 && upgrade head` 干净。
- **parity 用例第一版是抄了一份 SQL**(迁移改了用例也不会红,等于没测)——
改成解析迁移源码;后来又因为列已收窄、19 字符的旧值塞不进测试库,
干脆改成源码级解析比对(不造数据)。
## 验证
- `TZ=Asia/Shanghai`:**490 passed / 6 skipped**(S5 后 450 → 净增 40)
- `TZ=UTC`(相关文件)106 passed;迁移链 3 passed;ruff 全绿
- 前端 `tsc -b && vite build` 通过、`npm run lint` **0 errors**
- 真实开发库:迁移重放后三列均为 `varchar(16) NOT NULL DEFAULT 'unknown'`;
12 个账号的旧值 `elite_status_queried`(纯查询痕迹)→ **unknown**(没有猜成 bound)
未做(后续):S6 的**账号页重构**(列表关键列 / 生命周期主页)保留;
`bind_status` 旧列待 S8 drop;保留策略里「状态派生类查询」现在技术上可下探到 7 天档,
保守起见留待确认(影响的是在用页面显示,不是历史)。
This commit is contained in:
@@ -11,6 +11,14 @@ from sqlalchemy.orm import Session
|
||||
from core.douyu import DouyuActivityClient, DouyuActivityError
|
||||
|
||||
from ..models import Account, DouyuTask
|
||||
from .account_binding import (
|
||||
BIND_BOUND,
|
||||
BIND_PENDING,
|
||||
BIND_UNBOUND,
|
||||
SCOPE_ELITE,
|
||||
SCOPE_ESPORTS,
|
||||
set_bind_status,
|
||||
)
|
||||
from .business_events import record_bind
|
||||
|
||||
DOUYU_LEGACY_BIND_ACT_ALIAS = "20250213NQCYX"
|
||||
@@ -228,19 +236,26 @@ class BindMixin:
|
||||
)
|
||||
|
||||
def _apply_bind_info_to_account(
|
||||
self, account: Account, bind_info: dict, status: str
|
||||
self, account: Account, bind_info: dict, state: str | None = None
|
||||
) -> None:
|
||||
"""写精英角色的**当前值**。
|
||||
|
||||
`state` 传 `None` 表示「只更新角色信息,不动绑定状态」—— 纯查询任务
|
||||
(`query_game_name` 等)就该这样:它们**没有资格**改变绑定状态。
|
||||
传值时只能是 `account_binding` 的四个枚举。
|
||||
"""
|
||||
role_name = str(bind_info.get("role_name") or "")
|
||||
account.game_name = role_name or account.game_name
|
||||
account.game_channel = self._role_channel(bind_info) or account.game_channel
|
||||
account.change_role_wait_time = self._to_int(
|
||||
bind_info.get("change_role_wait_time")
|
||||
)
|
||||
account.bind_status = status
|
||||
if state is not None:
|
||||
set_bind_status(account, SCOPE_ELITE, state)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
|
||||
def _apply_esports_bind_info_to_account(
|
||||
self, account: Account, bind_info: dict, status: str
|
||||
self, account: Account, bind_info: dict, state: str | None = None
|
||||
) -> None:
|
||||
"""将电竞手册角色状态写入专属字段,避免覆盖精英宝典数据。"""
|
||||
role_name = str(bind_info.get("role_name") or "")
|
||||
@@ -252,7 +267,8 @@ class BindMixin:
|
||||
bind_info.get("change_role_wait_time")
|
||||
)
|
||||
account.esports_can_change_time = self._to_int(bind_info.get("can_change_time"))
|
||||
account.esports_bind_status = status
|
||||
if state is not None:
|
||||
set_bind_status(account, SCOPE_ESPORTS, state)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
|
||||
def _esports_bind_state(
|
||||
@@ -503,7 +519,6 @@ class BindMixin:
|
||||
)
|
||||
role_name = snapshot["role_name"]
|
||||
# 待确认角色只回传前端展示,不写入账号表,避免“未换绑成功但角色信息已变新”
|
||||
account.bind_status = "game_queried"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
self._push_log(
|
||||
"success", f"识别到待确认角色: {role_name} (act={query_alias})"
|
||||
@@ -651,7 +666,7 @@ class BindMixin:
|
||||
self._apply_bind_info_to_account(
|
||||
account,
|
||||
cooldown if cooldown_snapshot["role_name"] else before,
|
||||
"bind_confirmed" if result["bind_confirmed"] else "game_queried",
|
||||
BIND_BOUND if result["bind_confirmed"] else None,
|
||||
)
|
||||
self._mark_task(
|
||||
db,
|
||||
@@ -688,7 +703,6 @@ class BindMixin:
|
||||
"bind_phase": "waiting_scan",
|
||||
"bind_polling": True,
|
||||
}
|
||||
account.bind_status = "bind_qr_generated"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
# 关键:先把二维码 progress 出去,前端 running 期间即可弹窗扫码。
|
||||
self._update_task_progress(
|
||||
@@ -792,7 +806,7 @@ class BindMixin:
|
||||
and role_name == str(before_bound.get("role_name") or "")
|
||||
)
|
||||
if before_is_current_bound and before_bound is not None:
|
||||
self._apply_bind_info_to_account(account, before_bound, "bind_confirmed")
|
||||
self._apply_bind_info_to_account(account, before_bound, BIND_BOUND)
|
||||
self._mark_task(
|
||||
db,
|
||||
task,
|
||||
@@ -820,9 +834,8 @@ class BindMixin:
|
||||
self._push_log("warning", f"确认绑定接口失败: {confirm_msg}")
|
||||
# 待绑定游戏账号侧换绑限制(未到换绑时间等)导致确认失败:保留原绑定并给出明确提示
|
||||
if before_bound is not None:
|
||||
self._apply_bind_info_to_account(account, before_bound, "game_queried")
|
||||
self._apply_bind_info_to_account(account, before_bound)
|
||||
else:
|
||||
account.bind_status = "game_queried"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
self._mark_task(
|
||||
db,
|
||||
@@ -878,11 +891,8 @@ class BindMixin:
|
||||
if after is None:
|
||||
# 已生效绑定始终未出现:确认未生效(或同步延迟超时),保留原绑定
|
||||
if before_bound is not None:
|
||||
self._apply_bind_info_to_account(
|
||||
account, before_bound, "game_queried"
|
||||
)
|
||||
self._apply_bind_info_to_account(account, before_bound)
|
||||
else:
|
||||
account.bind_status = "game_queried"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
self._mark_task(
|
||||
db,
|
||||
@@ -913,11 +923,8 @@ class BindMixin:
|
||||
# 查询接口异常:确认接口已成功时按成功处理,但保留错误信息。
|
||||
# 写库优先确认前已生效绑定,避免待确认角色被误写入。
|
||||
if before_bound is not None:
|
||||
self._apply_bind_info_to_account(
|
||||
account, before_bound, "bind_confirmed"
|
||||
)
|
||||
self._apply_bind_info_to_account(account, before_bound, BIND_BOUND)
|
||||
else:
|
||||
account.bind_status = "bind_confirmed"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
# 确认接口成功了,但回查抛异常 —— 没能亲眼看到绑定生效。
|
||||
# 仍然记(确认请求确实发出且成功),但 weak=True 标出来。
|
||||
@@ -953,7 +960,7 @@ class BindMixin:
|
||||
|
||||
after_snapshot = self._bind_snapshot(after)
|
||||
final_role_name = after_snapshot["role_name"] or role_name
|
||||
self._apply_bind_info_to_account(account, after, "bind_confirmed")
|
||||
self._apply_bind_info_to_account(account, after, BIND_BOUND)
|
||||
# 强事实:确认接口成功 + 回查命中已生效绑定。
|
||||
# ⚠️ 上面还有一条 success 是「检测到账号**本来就已经绑定**」(什么都没做),
|
||||
# 那条**不发事件** —— 否则用户每点一次「确认绑定」时间线就多一条假的绑定记录。
|
||||
@@ -1006,9 +1013,9 @@ class BindMixin:
|
||||
self._apply_esports_bind_info_to_account(
|
||||
account,
|
||||
state,
|
||||
"esports_bound"
|
||||
BIND_BOUND
|
||||
if esports_bound
|
||||
else ("esports_bind_ready" if role_text else "game_not_bound"),
|
||||
else (BIND_PENDING if role_text else BIND_UNBOUND),
|
||||
)
|
||||
# 二维码用于重新选择角色,不应因为已有角色或换绑冷却而隐藏。
|
||||
# 冷却是否允许最终由 actBind 返回结果决定。
|
||||
@@ -1051,7 +1058,6 @@ class BindMixin:
|
||||
"can_open_role_selector": True,
|
||||
"bind_phase": "switching_role",
|
||||
}
|
||||
account.esports_bind_status = "esports_role_switching"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
self._mark_task(
|
||||
db,
|
||||
@@ -1076,7 +1082,6 @@ class BindMixin:
|
||||
self._mark_task(db, task, "failed", "请先配置电竞手册活动 actAlias")
|
||||
return
|
||||
|
||||
account.esports_bind_status = "esports_bind_confirming"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
self._update_task_progress(
|
||||
db,
|
||||
@@ -1101,9 +1106,7 @@ class BindMixin:
|
||||
else "confirm_failed",
|
||||
}
|
||||
if not after_state["esports_bound"]:
|
||||
self._apply_esports_bind_info_to_account(
|
||||
account, after_state, "esports_bind_ready"
|
||||
)
|
||||
self._apply_esports_bind_info_to_account(account, after_state, BIND_PENDING)
|
||||
self._mark_task(
|
||||
db,
|
||||
task,
|
||||
@@ -1113,7 +1116,7 @@ class BindMixin:
|
||||
)
|
||||
return
|
||||
|
||||
self._apply_esports_bind_info_to_account(account, after_state, "esports_bound")
|
||||
self._apply_esports_bind_info_to_account(account, after_state, BIND_BOUND)
|
||||
# 业务事实在这一刻成立:确认接口成功 + 回查 esports_bound 为真。
|
||||
# 必须在 _mark_task 之前发 —— 它内部 commit,而 emit 只 RELEASE SAVEPOINT、
|
||||
# 不提交外层事务,runner 的 finally 只 close(未提交即回滚),之后发会被静默丢弃。
|
||||
@@ -1155,7 +1158,7 @@ class BindMixin:
|
||||
self._apply_esports_bind_info_to_account(
|
||||
account,
|
||||
state,
|
||||
"esports_bound"
|
||||
BIND_BOUND
|
||||
if is_bound
|
||||
else ("esports_bind_ready" if role_name else "game_not_bound"),
|
||||
)
|
||||
@@ -1225,9 +1228,8 @@ class BindMixin:
|
||||
)
|
||||
# 只有已生效绑定才写账号表;待确认角色不污染 game_name
|
||||
if bind_info and is_bound:
|
||||
self._apply_bind_info_to_account(account, bind_info, "game_queried")
|
||||
self._apply_bind_info_to_account(account, bind_info)
|
||||
else:
|
||||
account.bind_status = "game_queried" if has_pending else "game_not_bound"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
result = {
|
||||
"act_alias": source_alias,
|
||||
@@ -1317,7 +1319,6 @@ class BindMixin:
|
||||
snapshot = self._bind_snapshot(bind_info)
|
||||
wait_time = snapshot["change_role_wait_time"]
|
||||
account.change_role_wait_time = wait_time
|
||||
account.bind_status = "change_time_queried"
|
||||
account.updated_at = datetime.now(UTC)
|
||||
source_alias = str(bind_info.get("act_alias") or "")
|
||||
wait_text = self._format_wait_time(wait_time)
|
||||
|
||||
Reference in New Issue
Block a user