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
+153
View File
@@ -0,0 +1,153 @@
"""角色绑定状态:把三个「垃圾场」列收敛成明确枚举。
## 背景:`bind_status` 崩掉的方式
历史上账号的绑定状态存在**一个自由文本列** `accounts.bind_status` 里,写入方遍布
6 个文件、29 处,取值 20 多种,而且**语义混杂**:
- 真正的状态:`bind_confirmed` / `game_not_bound`
- 动作痕迹:`bind_qr_generated` / `goods_locked` / `elite_opened`
- 纯查询痕迹:`points_queried` / `change_time_queried` / `csrf_token_ready`
因为它是**覆盖式**的,`bind_confirmed` 会被之后任意一次查询(例如
`limited_goods_queried`)盖掉 —— 于是「这个账号到底绑没绑」查不出来。
`esports_bind_status` / `xpd_bind_status` 是同一个病。
## 收敛规则(设计文档 §3.6)
1. **状态列只放「现在是什么」**:`unknown` / `unbound` / `pending` / `bound`;
2. **动作与查询痕迹一律不再写状态列** —— 动作已经进 `account_events`
(S5 的直发事件),查询痕迹本来就不该是状态;
3. 三列各管一个作用域,**一列一义**:
| 列 | 作用域 |
| --- | --- |
| `accounts.role_bind_status` | 精英宝典角色绑定 |
| `accounts.esports_bind_status` | 电竞手册角色绑定 |
| `accounts.xpd_bind_status` | 和平小店角色绑定 |
> ⚠️ `pending` 的含义是「角色已就绪 / 二维码已出,但**还没确认生效**」。
> 斗鱼侧的疑点宁可为 `unknown`,也不要冒充 `bound`。
"""
from __future__ import annotations
from ..models import Account
# ── 作用域 ──────────────────────────────────────────────
SCOPE_ELITE = "elite"
SCOPE_ESPORTS = "esports"
SCOPE_PEACE = "peace"
ALL_SCOPES = (SCOPE_ELITE, SCOPE_ESPORTS, SCOPE_PEACE)
# ── 取值域(**只有这四个**)────────────────────────────
BIND_UNKNOWN = "unknown" # 还没查过 / 旧数据不可信
BIND_UNBOUND = "unbound" # 明确没绑定
BIND_PENDING = "pending" # 角色就绪或二维码已出,尚未确认生效
BIND_BOUND = "bound" # 确认生效
ALL_BIND_STATES = (BIND_UNKNOWN, BIND_UNBOUND, BIND_PENDING, BIND_BOUND)
BIND_LABELS = {
BIND_UNKNOWN: "未知",
BIND_UNBOUND: "未绑定",
BIND_PENDING: "待确认",
BIND_BOUND: "已绑定",
}
#: 作用域 → `accounts` 上的列名
SCOPE_COLUMNS = {
SCOPE_ELITE: "role_bind_status",
SCOPE_ESPORTS: "esports_bind_status",
SCOPE_PEACE: "xpd_bind_status",
}
SCOPE_LABELS = {
SCOPE_ELITE: "精英宝典角色",
SCOPE_ESPORTS: "电竞手册角色",
SCOPE_PEACE: "和平小店角色",
}
# ── 旧自由文本 → 新枚举 ─────────────────────────────────
#
# 只映射**无歧义**的取值;其余一律 `unknown`。判定标准是「这个旧值是不是
# 明确陈述了绑定关系」:
# - `bind_confirmed` 是「确认绑定成功」那一刻写的,而绑定不会被之后的查询撤销
# → 可以确信是 bound;
# - `game_not_bound` 明确说没绑 → unbound;
# - `bind_qr_generated` / `xpd_bind_qr_ready` 只是出了二维码 → pending;
# - `game_queried` / `points_queried` / `goods_locked` 之类**什么都没说**
# → unknown(设计文档 §5.5:不猜测性回填)。
#
# ⚠️ 这三张表**必须与迁移 `20261004_0044` 里的 CASE 逐字对应**,有一条用例
# 真跑那段 SQL 与它们比对(分叉了不会报错,只会让回填结果与运行时不一致)。
LEGACY_ROLE_BIND_MAP: dict[str, str] = {
"bind_confirmed": BIND_BOUND,
"bind_qr_generated": BIND_PENDING,
"game_not_bound": BIND_UNBOUND,
}
LEGACY_ESPORTS_BIND_MAP: dict[str, str] = {
"esports_bound": BIND_BOUND,
"esports_bind_ready": BIND_PENDING,
"esports_bind_confirming": BIND_PENDING,
"esports_qr_created": BIND_PENDING,
"esports_role_switching": BIND_PENDING,
}
LEGACY_XPD_BIND_MAP: dict[str, str] = {
"xpd_bound": BIND_BOUND,
"xpd_bind_qr_ready": BIND_PENDING,
"xpd_not_bound": BIND_UNBOUND,
}
LEGACY_MAPS = {
SCOPE_ELITE: LEGACY_ROLE_BIND_MAP,
SCOPE_ESPORTS: LEGACY_ESPORTS_BIND_MAP,
SCOPE_PEACE: LEGACY_XPD_BIND_MAP,
}
def map_legacy_bind_status(scope: str, legacy: str | None) -> str:
"""旧自由文本 → 新枚举;认不出来就是 `unknown`(**不猜**)。"""
return LEGACY_MAPS.get(scope, {}).get((legacy or "").strip(), BIND_UNKNOWN)
def get_bind_status(account: Account, scope: str) -> str:
column = SCOPE_COLUMNS.get(scope)
if not column:
raise ValueError(f"未知的绑定作用域: {scope}")
value = getattr(account, column, "") or ""
# 兼容:迁移之前的库或测试构造的对象可能还没有新列的值
return value if value in ALL_BIND_STATES else BIND_UNKNOWN
def set_bind_status(account: Account, scope: str, state: str) -> bool:
"""设置某个作用域的绑定状态,返回是否真的变化。
**这是三个绑定状态列的唯一写入口**(同 `account_lifecycle` 之于 `status`)。
`state` 必须是四个枚举值之一;传旧自由文本会直接报错,避免又把垃圾写回去。
"""
if state not in ALL_BIND_STATES:
raise ValueError(
f"绑定状态只能是 {ALL_BIND_STATES} 之一,收到 {state!r}。"
"动作/查询痕迹请进 account_events,不要写状态列。"
)
column = SCOPE_COLUMNS.get(scope)
if not column:
raise ValueError(f"未知的绑定作用域: {scope}")
if getattr(account, column, None) == state:
return False
setattr(account, column, state)
return True
def bind_payload(account: Account) -> dict[str, str]:
"""给接口/前端用的三列快照(值 + 中文标签)。"""
payload: dict[str, str] = {}
for scope in ALL_SCOPES:
state = get_bind_status(account, scope)
payload[f"{scope}_bind_status"] = state
payload[f"{scope}_bind_label"] = BIND_LABELS[state]
return payload
+32 -31
View File
@@ -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)
@@ -177,7 +177,6 @@ class DonateMixin:
if refresh_errors:
result["refresh_errors"] = refresh_errors
account.esports_bind_status = "esports_gift_donated"
account.updated_at = datetime.now(UTC)
confirmed = bool(result.get("esports_points_changed"))
message = f"赠送{gift_name}成功: {gift_count}"
@@ -285,7 +284,6 @@ class DonateMixin:
refresh_errors.append(f"积分: {exc}")
if refresh_errors:
result["refresh_errors"] = refresh_errors
account.bind_status = "gift_donated"
account.updated_at = datetime.now(UTC)
message = f"赠送精英令成功: {gift_count}"
if account.gold_balance is not None:
@@ -270,7 +270,6 @@ class GoldMixin:
result = client.create_gold_qr(
amount=amount, pay_type=int(config["gold_pay_type"])
)
account.bind_status = "gold_qr_created"
account.updated_at = datetime.now(UTC)
self._update_task_progress(
db, task, "running", f"鱼翅 {amount} 元支付码已生成,等待到账", result
@@ -282,7 +281,6 @@ class GoldMixin:
self._mark_task(db, task, "stopped", "任务已停止", result)
return
if recharged:
account.bind_status = "gold_recharged"
account.updated_at = datetime.now(UTC)
# 金额用**本地变量 amount**:二维码链路的 result 里没有金额
# (create_gold_qr 只返回 pay_url/token/raw),旧收集器在这里恒写
@@ -430,7 +428,6 @@ class GoldMixin:
result,
)
return
account.bind_status = "gold_api_order_created"
account.updated_at = datetime.now(UTC)
self._update_task_progress(
db, task, "running", "供应商直充订单已创建,等待到账", result
@@ -441,7 +438,6 @@ class GoldMixin:
self._mark_task(db, task, "stopped", "任务已停止", result)
return
if status == 2:
account.bind_status = "gold_recharged"
account.updated_at = datetime.now(UTC)
# 幂等键与**供应商回调**(routers/douyu.py)完全一致 —— 两条路径都可能
# 观测到同一笔成功(_wait_supplier_gold_order 会采纳回调结果再 mark 一次),
@@ -471,7 +467,6 @@ class GoldMixin:
):
client = self._client(cookie)
result = self._refresh_account_gold_balance(client, account)
account.bind_status = "gold_balance_queried"
account.updated_at = datetime.now(UTC)
self._mark_task(
db,
@@ -65,7 +65,6 @@ class GoodsMixin:
result = client.list_goods(manual_id=config["manual_id"], rid=config["rid"])
goods = result["goods"]
self._upsert_goods(db, goods)
account.bind_status = account.bind_status or "active"
account.updated_at = datetime.now(UTC)
self._mark_task(
db,
@@ -91,7 +90,6 @@ class GoodsMixin:
)
goods = result["goods"]
self._upsert_esports_goods(db, goods)
account.esports_bind_status = "esports_goods_refreshed"
account.updated_at = datetime.now(UTC)
self._mark_task(
db,
@@ -131,7 +129,6 @@ class GoodsMixin:
.filter(DouyuGoodsSnapshot.commodity_id == commodity_id)
.first()
)
account.bind_status = "goods_locked"
account.updated_at = datetime.now(UTC)
expire_seconds = result.get("expire_seconds")
expire_text = f",{expire_seconds} 秒内有效" if expire_seconds else ""
@@ -181,7 +178,6 @@ class GoodsMixin:
.filter(DouyuGoodsSnapshot.commodity_id == commodity_id)
.first()
)
account.bind_status = "goods_exchanged"
account.updated_at = datetime.now(UTC)
result = {
"commodity_id": commodity_id,
@@ -369,7 +365,6 @@ class GoodsMixin:
except DouyuActivityError as exc:
finish_failed(f"预约到货失败: {exc}")
return
account.bind_status = "goods_subscribed"
account.updated_at = datetime.now(UTC)
self._mark_task(
db,
@@ -406,7 +401,6 @@ class GoodsMixin:
except DouyuActivityError as exc:
finish_failed(f"预兑失败: {exc}")
return
account.bind_status = "goods_pre_exchanged"
account.updated_at = datetime.now(UTC)
self._mark_task(
db,
@@ -508,7 +502,6 @@ class GoodsMixin:
"payment": payment,
}
)
account.bind_status = "goods_exchanged"
account.updated_at = datetime.now(UTC)
# 兑换成功后自动刷新积分,更新账号最新积分信息(失败不阻断兑换成功)
points_refresh = None
@@ -616,7 +609,6 @@ class GoodsMixin:
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
result["esports_points_refresh_error"] = str(exc)
account.esports_bind_status = "esports_goods_exchanged"
account.updated_at = datetime.now(UTC)
name = (goods.name if goods else "") or commodity_id
message = f"兑换电竞皮肤成功: {name}"
@@ -671,7 +663,6 @@ class GoodsMixin:
if not names
else f"限兑 {len(names)} 个: {', '.join(names[:5])}"
)
account.bind_status = "limited_goods_queried"
account.updated_at = datetime.now(UTC)
self._mark_task(
db,
@@ -687,7 +678,6 @@ class GoodsMixin:
client = self._client(cookie)
result = client.exchange_records(manual_id=str(config["manual_id"]))
records = result["records"]
account.bind_status = "exchange_records_queried"
account.updated_at = datetime.now(UTC)
self._mark_task(
db,
@@ -702,7 +692,6 @@ class GoodsMixin:
):
client = self._client(cookie)
token = client.csrf_token()
account.bind_status = "csrf_token_ready"
account.updated_at = datetime.now(UTC)
self._mark_task(
db,
+2 -9
View File
@@ -113,7 +113,6 @@ class ManualMixin:
manual_id=manual_id,
rid=str(config.get("rid") or ""),
)
account.bind_status = "elite_status_queried"
account.updated_at = datetime.now(UTC)
state = "已开通" if result["elite_opened"] else "未开通"
expire_text = self._format_expire_text(result["elite_expire_time"])
@@ -321,7 +320,6 @@ class ManualMixin:
baseline["elite_expire_time"]
)
points = baseline["elite_manual_score"]
account.bind_status = "elite_opened"
account.updated_at = datetime.now(UTC)
self._mark_task(
db,
@@ -355,7 +353,6 @@ class ManualMixin:
amount=int(config["elite_amount"]),
room_id=str(config["room_id"]),
)
account.bind_status = "elite_qr_created"
account.updated_at = datetime.now(UTC)
self._update_task_progress(
db, task, "running", "精英宝典支付码已生成,等待开通到账", result
@@ -374,7 +371,6 @@ class ManualMixin:
self._mark_task(db, task, "stopped", "任务已停止", result)
return
if opened:
account.bind_status = "elite_opened"
account.elite_opened = True
account.updated_at = datetime.now(UTC)
# elite_opened 是**覆盖式**布尔值:开通之后再操作也看不出「什么时候开的」。
@@ -441,7 +437,8 @@ class ManualMixin:
self._push_log("warning", f"生成电竞手册支付码前查询活动状态失败: {exc}")
if baseline_manual_type is not None and baseline_manual_type >= 1:
account.esports_bind_status = "esports_opened"
# 这里已确认开通:写独立状态列(以前只把它塞进 esports_bind_status)
account.esports_opened = True
account.updated_at = datetime.now(UTC)
self._mark_task(
db,
@@ -459,7 +456,6 @@ class ManualMixin:
room_id=str(config["room_id"]),
)
result.update(baseline_result)
account.esports_bind_status = "esports_qr_created"
account.updated_at = datetime.now(UTC)
self._update_task_progress(
db, task, "running", "电竞手册支付码已生成,等待开通到账", result
@@ -478,7 +474,6 @@ class ManualMixin:
self._mark_task(db, task, "stopped", "任务已停止", result)
return
if opened:
account.esports_bind_status = "esports_opened"
account.updated_at = datetime.now(UTC)
# 与精英宝典同理:状态字段是覆盖式的,这里补上「什么时候开的」。
# 注意只在**真正开通**这一支发事件;上面「基线查到已开通」那一支是检测,
@@ -528,7 +523,6 @@ class ManualMixin:
return
client = self._client(cookie)
result = self._refresh_esports_handbook(client, account, manual_id=manual_id)
account.esports_bind_status = "esports_points_queried"
account.updated_at = datetime.now(UTC)
points = result["esports_points"]
self._mark_task(
@@ -545,7 +539,6 @@ class ManualMixin:
client = self._client(cookie)
ctn = client.acf_ccn(refresh_subscribe=False)
result = self._refresh_account_points(client, account, cookie, ctn=ctn)
account.bind_status = "points_queried"
account.updated_at = datetime.now(UTC)
points = result["points"]
self._mark_task(
+11 -9
View File
@@ -11,6 +11,13 @@ from sqlalchemy.orm import Session
from core.douyu import DouyuActivityClient, DouyuActivityError
from ..models import Account, DouyuTask, DouyuXpdGoodsSnapshot
from .account_binding import (
BIND_BOUND,
BIND_PENDING,
BIND_UNBOUND,
SCOPE_PEACE,
set_bind_status,
)
from .business_events import record_exchange
DOUYU_XPD_BIND_POLL_SECONDS = 300
@@ -92,7 +99,7 @@ class XpdMixin:
return
area_id = self._xpd_area_id(role, account)
self._apply_xpd_role_to_account(account, role, area_id)
account.xpd_bind_status = "xpd_bound"
set_bind_status(account, SCOPE_PEACE, BIND_BOUND)
db.commit()
role_text = str(role.get("role_name") or "-")
channel = (
@@ -126,7 +133,7 @@ class XpdMixin:
self._mark_task(db, task, "failed", "请先配置小店活动代号 actAlias")
return
result = client.xpd_bind_qr(act_alias=act_alias)
account.xpd_bind_status = "xpd_bind_qr_ready"
set_bind_status(account, SCOPE_PEACE, BIND_PENDING)
account.updated_at = datetime.now(UTC)
db.commit()
# 记录绑定前状态:已绑定账号生成二维码后必须等扫码换绑,不能立即成功
@@ -242,7 +249,7 @@ class XpdMixin:
result = client.xpd_bind_info(act_alias=act_alias)
role_name = str(result.get("role_name") or "")
if not result.get("bind_role") or not role_name:
account.xpd_bind_status = "xpd_not_bound"
set_bind_status(account, SCOPE_PEACE, BIND_UNBOUND)
account.updated_at = datetime.now(UTC)
db.commit()
self._mark_task(
@@ -263,7 +270,7 @@ class XpdMixin:
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
account.xpd_game_name = role_name
account.updated_at = datetime.now(UTC)
account.xpd_bind_status = "xpd_bound"
set_bind_status(account, SCOPE_PEACE, BIND_BOUND)
account.updated_at = datetime.now(UTC)
db.commit()
result["xpd_pending_confirm"] = False
@@ -324,7 +331,6 @@ class XpdMixin:
goods = result["goods"]
self._upsert_xpd_goods(db, goods)
self._apply_xpd_role_to_account(account, role, area_id)
account.xpd_bind_status = "xpd_goods_refreshed"
db.commit()
self._mark_task(
db,
@@ -363,7 +369,6 @@ class XpdMixin:
balance = result.get("balance")
self._apply_xpd_role_to_account(account, role, area_id)
account.xpd_balance = balance
account.xpd_bind_status = "xpd_balance_queried"
db.commit()
if balance is None:
self._mark_task(db, task, "failed", "未获取到小店点券余额")
@@ -427,7 +432,6 @@ class XpdMixin:
)
fragments = result.get("fragments")
account.xpd_fragments = fragments
account.xpd_bind_status = "xpd_fragments_queried"
account.updated_at = datetime.now(UTC)
db.commit()
if fragments is None:
@@ -459,7 +463,6 @@ class XpdMixin:
embed_query=embed["query"],
act_id=str(config["xpd_act_id"]),
)
account.xpd_bind_status = "xpd_purchase_records_queried"
account.updated_at = datetime.now(UTC)
total = result.get("total") or len(result.get("records") or [])
self._mark_task(
@@ -561,7 +564,6 @@ class XpdMixin:
account.xpd_balance = result["new_balance"]
if pay_type == 5 and result.get("new_balance") is not None:
account.xpd_fragments = result["new_balance"]
account.xpd_bind_status = "xpd_goods_exchanged"
account.updated_at = datetime.now(UTC)
db.commit()