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:
yml2213
2026-10-04 16:28:06 +08:00
parent 6e729e7a80
commit a64380fbcc
21 changed files with 899 additions and 158 deletions
+25 -5
View File
@@ -146,11 +146,18 @@ export interface AccountOverview {
exchange_balance: number | null;
game_name: string;
game_channel: string;
bind_status: string;
/** 三列各管一个作用域:unknown / unbound / pending / bound */
role_bind_status: BindStatus;
role_bind_label: string;
elite_opened: boolean | null;
elite_expire_time: number | null;
esports_points: number | null;
esports_bind_status: string;
esports_bind_status: BindStatus;
esports_bind_label: string;
/** 电竞手册是否已开通(与 elite_opened 对称,S6 从垃圾场列里救出来的) */
esports_opened: boolean | null;
xpd_bind_status: BindStatus;
xpd_bind_label: string;
esports_game_name: string;
esports_can_change_time: number | null;
stats: AccountOverviewStats;
@@ -441,6 +448,16 @@ export interface CookieDuplicateResponse {
// ==================== Douyu Activity ====================
/** 角色绑定状态(见后端 services/account_binding)。`bind_status` 旧列已废弃。 */
export type BindStatus = 'unknown' | 'unbound' | 'pending' | 'bound';
export const BIND_LABELS: Record<BindStatus, string> = {
unknown: '未知',
unbound: '未绑定',
pending: '待确认',
bound: '已绑定',
};
export interface DouyuTaskAccountItem {
id: number;
username: string;
@@ -452,14 +469,16 @@ export interface DouyuTaskAccountItem {
game_channel: string;
gold_balance: number | null;
exchange_balance: number | null;
bind_status: string;
role_bind_status: BindStatus;
role_bind_label: string;
change_role_wait_time: number | null;
elite_opened: boolean | null;
elite_expire_time: number | null;
esports_points: number | null;
esports_game_name: string;
esports_game_channel: string;
esports_bind_status: string;
esports_bind_status: BindStatus;
esports_bind_label: string;
esports_change_role_wait_time: number | null;
esports_can_change_time: number | null;
xpd_game_name: string;
@@ -469,7 +488,8 @@ export interface DouyuTaskAccountItem {
xpd_area_id: number | null;
xpd_balance: number | null;
xpd_fragments: number | null;
xpd_bind_status: string;
xpd_bind_status: BindStatus;
xpd_bind_label: string;
assigned_to: number | null;
assigned_username: string | null;
}
+23 -2
View File
@@ -55,6 +55,14 @@ const STATUS_TONES: Record<string, string> = {
archived: 'default',
};
/** 绑定状态 → 展示色(与后端 account_binding 的四个枚举对应)。 */
const BIND_TONES: Record<string, string> = {
bound: 'green',
pending: 'orange',
unbound: 'red',
unknown: 'default',
};
function eventTag(type: string) {
const style = EVENT_STYLE[type] ?? { label: type, color: 'default' };
return <Tag color={style.color}>{style.label}</Tag>;
@@ -276,7 +284,20 @@ export default function AccountDetailPage() {
{overview?.game_name || '-'}
{overview?.game_channel ? <Text type="secondary"> ({overview.game_channel})</Text> : null}
</Descriptions.Item>
<Descriptions.Item label="绑定状态">{overview?.bind_status || '-'}</Descriptions.Item>
<Descriptions.Item label="角色绑定">
{/* 三列各管一个作用域,取值 unknown/unbound/pending/bound */}
<Space size={6} wrap>
<Tag color={BIND_TONES[overview?.role_bind_status ?? 'unknown']}>
精英:{overview?.role_bind_label || '未知'}
</Tag>
<Tag color={BIND_TONES[overview?.esports_bind_status ?? 'unknown']}>
电竞:{overview?.esports_bind_label || '未知'}
</Tag>
<Tag color={BIND_TONES[overview?.xpd_bind_status ?? 'unknown']}>
小店:{overview?.xpd_bind_label || '未知'}
</Tag>
</Space>
</Descriptions.Item>
<Descriptions.Item label="备注">
{remarkEditing ? (
<Space.Compact style={{ width: '100%' }}>
@@ -331,7 +352,7 @@ export default function AccountDetailPage() {
/>
<Statistic
title="电竞手册"
value={overview?.esports_bind_status === 'esports_opened' ? '已开通' : (overview?.esports_bind_status || '未开通')}
value={overview?.esports_opened ? '已开通' : '未开通'}
valueStyle={{ fontSize: 20 }}
/>
</div>
@@ -72,6 +72,16 @@ function parseAccountNames(text: string): string[] {
.filter((name) => name.length > 0 && !seen.has(name) && seen.add(name));
}
/** 绑定状态小标签:取值 unknown / unbound / pending / bound。 */
function BindStatusTag({ status, label }: { status?: string; label?: string }) {
if (!status) return null;
const tone = status === 'bound' ? 'green'
: status === 'pending' ? 'orange'
: status === 'unbound' ? 'red'
: 'default';
return <Tag color={tone} style={{ marginInlineEnd: 0 }}>{label || status}</Tag>;
}
export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind }) {
const isPeaceHandbook = handbook === 'peace';
const isEsportsHandbook = handbook === 'esports';
@@ -167,17 +177,6 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
return map;
}, [tasks]);
const latestConfirmBindTaskByAccount = useMemo(() => {
const map = new Map<number, DouyuTaskItem>();
for (const task of tasks) {
if (task.task_type !== 'confirm_bind') continue;
if (!task.result) continue;
const previous = map.get(task.account_id);
if (!previous || task.id > previous.id) map.set(task.account_id, task);
}
return map;
}, [tasks]);
const latestXpdQueryTaskByAccount = useMemo(() => {
const map = new Map<number, DouyuTaskItem>();
for (const task of tasks) {
@@ -198,18 +197,6 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
return map;
}, [tasks]);
const latestConfirmBindFailureByAccount = useMemo(() => {
const map = new Map<number, DouyuTaskItem>();
for (const task of tasks) {
if (task.task_type !== 'confirm_bind') continue;
if (!['failed', 'error'].includes(task.status)) continue;
if (!(task.message || '').includes('待绑定游戏账号')) continue;
const previous = map.get(task.account_id);
if (!previous || task.id > previous.id) map.set(task.account_id, task);
}
return map;
}, [tasks]);
const latestEsportsStateTaskByAccount = useMemo(() => {
const map = new Map<number, DouyuTaskItem>();
for (const task of tasks) {
@@ -1159,50 +1146,50 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
title: '游戏名', dataIndex: 'game_name', width: 170,
render: (_, record) => {
if (isPeaceHandbook) {
return record.xpd_game_name
? <Text ellipsis title={record.xpd_game_name}>{record.xpd_game_name}</Text>
: <Text type="secondary">未查</Text>;
// 和平小店:角色名与绑定状态都读**账号字段**(不再从任务日志反推)。
return (
<Space direction="vertical" size={2} style={{ lineHeight: 1.35 }}>
{record.xpd_game_name
? <Text ellipsis title={record.xpd_game_name}>{record.xpd_game_name}</Text>
: <Text type="secondary">未查</Text>}
<BindStatusTag status={record.xpd_bind_status} label={record.xpd_bind_label} />
</Space>
);
}
// ⚠️ 账号状态一律读**账号字段**;任务日志只用来补「扫码流程中的瞬时信息」。
//
// 以前这里是反过来做的:从当前加载的任务列表里挑每个账号最新的一条
// (`latestConfirmBindTaskByAccount` 等)反推绑定状态。那样有两个问题:
// 1. 只看得见当前这一页的任务 —— 支撑任务一旦翻出窗口,显示的状态就是错的;
// 2. 让任务日志变成了「账号状态库」,于是它永远不敢清理(历史包袱)。
// 账号上的 role_bind_status / esports_bind_status / xpd_bind_status 才是权威。
const scopeState = isEsportsHandbook ? record.esports_bind_status : record.role_bind_status;
const scopeLabel = isEsportsHandbook ? record.esports_bind_label : record.role_bind_label;
const roleName = (isEsportsHandbook ? record.esports_game_name : record.game_name) || '';
const channel = (isEsportsHandbook ? record.esports_game_channel : record.game_channel) || '';
// 「待确认的新角色」是扫码后、确认前才存在的瞬时信息,账号上没有这个字段,
// 只能取刚跑完的那条查询任务 —— 这不影响「是否已绑定」的判断(那个读账号)。
const queryTask = isEsportsHandbook
? latestEsportsStateTaskByAccount.get(record.id) || null
: latestQueryGameTaskByAccount.get(record.id) || null;
const confirmTask = isEsportsHandbook ? null : latestConfirmBindTaskByAccount.get(record.id) || null;
const confirmSucceeded = Boolean(
confirmTask
&& confirmTask.status === 'success'
&& (
resultFlag(confirmTask.result, 'bind_confirmed')
|| resultFlag(confirmTask.result, 'is_bound_act')
),
);
const stateTask = (
confirmSucceeded
&& (!queryTask || confirmTask!.id > queryTask.id)
) ? confirmTask : queryTask;
const confirmFailureTask = latestConfirmBindFailureByAccount.get(record.id);
const clearPending = Boolean(
!isEsportsHandbook
&& confirmFailureTask
&& stateTask
&& confirmFailureTask.id > stateTask.id,
);
const stateResult = stateTask?.result || null;
const stateRoleConfirmed = resultFlag(stateResult, 'is_bound_act') || resultFlag(stateResult, 'bind_confirmed');
const stateRoleName = clearPending && !stateRoleConfirmed ? '' : resultText(stateResult, 'role_name');
// role_name = 当前已生效绑定角色;pending_role_name = 扫码后待确认的新角色
const roleName = stateRoleName
|| (isEsportsHandbook ? record.esports_game_name : record.game_name);
const stateChannel = clearPending && !stateRoleConfirmed
const queryResult = queryTask?.result || null;
const pendingRoleName = scopeState === 'bound' ? '' : resultText(queryResult, 'pending_role_name');
const pendingChannel = scopeState === 'bound'
? ''
: [resultText(stateResult, 'area_name'), resultText(stateResult, 'plat_name')]
: [resultText(queryResult, 'pending_area_name'), resultText(queryResult, 'pending_plat_name')]
.filter(Boolean)
.join(' / ');
const pendingRoleName = clearPending || confirmSucceeded ? '' : resultText(stateResult, 'pending_role_name');
const channel = stateChannel || (isEsportsHandbook ? record.esports_game_channel : record.game_channel);
const pendingChannel = clearPending || confirmSucceeded ? '' : [resultText(stateResult, 'pending_area_name'), resultText(stateResult, 'pending_plat_name')]
.filter(Boolean)
.join(' / ');
if (!roleName && !pendingRoleName) return <Text type="secondary">未查</Text>;
if (!roleName && !pendingRoleName) {
return (
<Space direction="vertical" size={2}>
<Text type="secondary">未查</Text>
<BindStatusTag status={scopeState} label={scopeLabel} />
</Space>
);
}
return (
<Space direction="vertical" size={2} style={{ lineHeight: 1.35 }}>
{roleName ? (
@@ -1229,6 +1216,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
) : null}
</Space>
) : null}
<BindStatusTag status={scopeState} label={scopeLabel} />
</Space>
);
},