diff --git a/.env.example b/.env.example index e4d30b4..3a73cb2 100644 --- a/.env.example +++ b/.env.example @@ -211,6 +211,8 @@ PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS=5 # 客户绑定二维码有效期 DOUYU_BIND_ROLE_POLL_SECONDS=1200 PLATFORM_BINDING_QR_TTL_SECONDS=1260 +# 一键查询复用账号本地新鲜快照的时间窗口 +DOUYU_OVERVIEW_CACHE_SECONDS=600 # 首次启动且尚未配置供给目标时,建立保守的高价值/普通号库存目标。 DOUYU_DEFAULT_SUPPLY_TARGETS_ENABLED=true diff --git a/core/douyu/activity_client.py b/core/douyu/activity_client.py index 0c61802..589a9bd 100644 --- a/core/douyu/activity_client.py +++ b/core/douyu/activity_client.py @@ -118,8 +118,17 @@ class DouyuActivityClient: ESPORTS_EXCHANGE_API = ( "https://www.douyu.com/wgapi/ordnc/activity/peacematch/exchange" ) - GOLD_QR_API = "https://cz.douyu.com/m/gold/getQrCode" - GOLD_ACCOUNT_API = "https://cz.douyu.com/item/gold/account" + # 2026-11-21 抓包:鱼翅充值/余额已迁到 www.douyu.com 的 japi/web/gold。 + GOLD_INIT_API = "https://www.douyu.com/japi/web/gold/init" + GOLD_QR_API = "https://www.douyu.com/japi/web/gold/getQrCode" + GOLD_QR_STATE_API = "https://www.douyu.com/japi/web/gold/getQrState" + GOLD_ACCOUNT_API = "https://www.douyu.com/japi/web/gold/account" + CREDIT_BALANCE_DETAIL_API = ( + "https://www.douyu.com/japi/oms/web/peace/credit/balanceDetail" + ) + CREDIT_JOURNAL_API = ( + "https://www.douyu.com/japi/oms/web/peace/credit/colJournal" + ) EXCHANGE_BALANCE_API = "https://www.douyu.com/wjapi/nc/exchange/fim" DONATE_API = "https://www.douyu.com/japi/gift/donate/mainsite/v3" @@ -349,16 +358,21 @@ class DouyuActivityClient: def acf_ccn(self, *, refresh_subscribe: bool = True) -> str: """获取 acf_ccn;必要时通过订阅接口刷新一次。""" - response = self._request( - "get", - self.ACF_CCN_API, - source="获取 acf_ccn", - headers={ - "Origin": "", - "Referer": "https://www.douyu.com/pages/ord-task-center?clientType=web&panelSource=1&rid=0", - }, - ) + # 2026-11-21 抓包:普通活动和积分查询直接复用 Cookie 中的 acf_ccn, + # 不会每次先请求 getCsrfCookie。只有缺失时才补,避免无意义请求和限流。 ctn = cookie_value(self.cookie, "acf_ccn") + response: requests.Response | None = None + if not ctn: + response = self._request( + "get", + self.ACF_CCN_API, + source="获取 acf_ccn", + headers={ + "Origin": "", + "Referer": "https://www.douyu.com/pages/ord-task-center?clientType=web&panelSource=1&rid=0", + }, + ) + ctn = cookie_value(self.cookie, "acf_ccn") if not ctn: response = self._request( "get", @@ -368,7 +382,8 @@ class DouyuActivityClient: ) ctn = cookie_value(self.cookie, "acf_ccn") if not ctn: - raise DouyuActivityError(f"获取 acf_ccn 失败: {response.text[:200]}") + detail = response.text[:200] if response is not None else "Cookie 缺失" + raise DouyuActivityError(f"获取 acf_ccn 失败: {detail}") if refresh_subscribe: payload = self._request_json( @@ -832,33 +847,104 @@ class DouyuActivityClient: "raw": payload, } + @staticmethod + def _gold_qr_product(amount: int) -> tuple[str, int]: + """2026-11-21 新链路按商品面值选择 productId,普通金额退回 1 鱼翅商品。""" + fixed_products = { + 1: "DYTV_Product_Gold_1", + 10: "DYTV_Product_Gold_10", + 50: "DYTV_Product_Gold_50", + 100: "DYTV_Product_Gold_100", + 500: "DYTV_Product_Gold_500", + 1000: "DYTV_Product_Gold_1000", + 5000: "DYTV_Product_Gold_5000", + 10000: "DYTV_Product_Gold_10000", + } + value = max(1, int(amount)) + if value in fixed_products: + return fixed_products[value], 1 + return fixed_products[1], value + def create_gold_qr( - self, *, amount: int, pay_type: int = 1, product_id: str = "DYTV_Product_Gold_1" + self, + *, + amount: int, + pay_type: int = 1, ) -> dict[str, Any]: - """生成鱼翅充值支付二维码。""" - payload = self._request_json( + """按 2026-11-21 最新链路生成鱼翅充值支付二维码。""" + ctn = self.acf_ccn(refresh_subscribe=False) + init_payload = self._request_json( "post", - self.GOLD_QR_API, - "生成鱼翅充值码", + self.GOLD_INIT_API, + "初始化鱼翅充值", data={ - "pay_type": str(pay_type), - "product_id": product_id, - "number": str(amount), - "toname": "", - "source": "dy_box", + "ctn": ctn, + "scene": "web", }, headers={ "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", - "Origin": "https://cz.douyu.com", - "Referer": "https://cz.douyu.com/", + "Referer": "https://www.douyu.com/", }, ) + if init_payload.get("code") not in (0, "0") or not init_payload.get( + "success", True + ): + raise DouyuActivityError(init_payload.get("msg") or "初始化鱼翅充值失败") + + product_id, number = self._gold_qr_product(amount) + payload = self._request_json( + "get", + self.GOLD_QR_API, + "生成鱼翅充值码", + params={ + "payType": str(pay_type), + "productId": product_id, + "number": str(number), + "source": "", + "toName": "", + }, + headers={"Referer": "https://www.douyu.com/"}, + ) + if payload.get("code") not in (0, "0") or not payload.get("success", True): + raise DouyuActivityError(payload.get("msg") or "生成鱼翅充值码失败") data = payload.get("data") or {} pay_url = data.get("code_url") or payload.get("code_url") or "" token = data.get("token") or payload.get("token") or "" if not pay_url: raise DouyuActivityError(payload.get("msg") or "生成鱼翅充值码失败") - return {"pay_url": pay_url, "token": token, "raw": payload} + return { + "pay_url": pay_url, + "token": token, + "polling_url": data.get("polling_url") or "", + "product_id": product_id, + "product_number": number, + "init": { + "uid": (init_payload.get("data") or {}).get("uid"), + "nickname": (init_payload.get("data") or {}).get("nickName"), + "scene": (init_payload.get("data") or {}).get("scene"), + }, + "raw": payload, + } + + def query_gold_qr_state(self, token: str) -> dict[str, Any]: + """查询 2026-11-21 新版鱼翅支付码状态。""" + payload = self._request_json( + "get", + self.GOLD_QR_STATE_API, + "查询鱼翅充值状态", + params={"token": token}, + headers={"Referer": "https://www.douyu.com/"}, + ) + if payload.get("code") not in (0, "0") or not payload.get("success", True): + raise DouyuActivityError(payload.get("msg") or "查询鱼翅充值状态失败") + data = payload.get("data") or {} + return { + "state": data.get("state"), + "nickname": data.get("nickname") or "", + "quantity": data.get("quantity"), + "order_id": data.get("orderId") or "", + "raw": payload, + } def _donate_gift( self, @@ -925,17 +1011,29 @@ class DouyuActivityClient: source=f"赠送{gift_name}", ) - def query_points(self, *, uid: str, ctn: str) -> dict[str, Any]: - """查询手册积分余额。""" + def query_points(self, *, uid: str, ctn: str = "") -> dict[str, Any]: + """查询手册积分余额(2026-11-21 对齐浏览器 multipart 请求)。""" + if ctn: + self._set_cookie_value("acf_ccn", ctn) payload = self._request_json( "post", self.CREDIT_BALANCE_API, "查询手册积分", - json={"uid": str(uid), "ctn": ctn}, - headers={"Content-Type": "application/json;charset=UTF-8"}, + files=self._multipart_fields({"uid": str(uid)}), + headers={ + "Referer": ( + "https://www.douyu.com/pages/live-peace-handbook/web/" + "point-detail?ditchname=pass0" + ) + }, ) data = payload.get("data") or {} - return {"points": data.get("balance"), "raw": payload} + return { + "points": data.get("balance"), + "recently_expiring_points": data.get("recentlyWillExpireBalance"), + "will_expire_time": data.get("willExpireTime"), + "raw": payload, + } def query_points_diff(self, *, uid: str, ctn: str) -> dict[str, Any]: """查询手册积分差额。""" @@ -949,6 +1047,60 @@ class DouyuActivityClient: data = payload.get("data") or {} return {"diff": data.get("diff"), "raw": payload} + def query_points_expirations( + self, + *, + user_id: str, + current: int = 0, + page_size: int = 20, + ) -> dict[str, Any]: + """查询积分到期批次(2026-11-21 页面 point-detail 使用)。""" + payload = self._request_json( + "post", + self.CREDIT_BALANCE_DETAIL_API, + "查询积分到期批次", + json={ + "userId": int(user_id), + "current": max(0, int(current)), + "pageSize": max(1, int(page_size)), + }, + headers={ + "Content-Type": "application/json;charset=UTF-8", + "Referer": ( + "https://www.douyu.com/pages/live-peace-handbook/web/" + "point-detail?ditchname=pass0" + ), + }, + ) + return {"data": payload.get("data") or {}, "raw": payload} + + def query_points_ledger( + self, + *, + uid: str, + page_num: int = 1, + page_size: int = 15, + ) -> dict[str, Any]: + """查询积分收支流水,用于赠送/兑换后的精确核对。""" + payload = self._request_json( + "post", + self.CREDIT_JOURNAL_API, + "查询积分流水", + json={ + "uid": str(uid), + "pageNum": max(1, int(page_num)), + "pageSize": max(1, int(page_size)), + }, + headers={ + "Content-Type": "application/json;charset=UTF-8", + "Referer": ( + "https://www.douyu.com/pages/live-peace-handbook/web/" + "point-detail?ditchname=pass0" + ), + }, + ) + return {"data": payload.get("data") or {}, "raw": payload} + def list_goods(self, *, manual_id: str, rid: str) -> dict[str, Any]: """刷新活动商品列表。""" payload = self._request_json( @@ -1409,18 +1561,26 @@ class DouyuActivityClient: return {"data": payload.get("data") or {}, "raw": payload} def gold_account(self) -> dict[str, Any]: - """查询 cz 侧鱼翅余额。""" + """查询鱼翅余额(2026-11-21 使用 www 新接口)。""" payload = self._request_json( "get", self.GOLD_ACCOUNT_API, "查询鱼翅余额", - headers={ - "Origin": "https://cz.douyu.com", - "Referer": "https://cz.douyu.com/", - }, + headers={"Referer": "https://www.douyu.com/"}, ) data = payload.get("data") or {} - return {"gold": data.get("gold"), "raw": payload} + amount_cents = data.get("amount") + if amount_cents is None: + # 兼容部分旧容器仍返回 gold 字段。 + gold = data.get("gold") + return {"gold": gold, "amount_cents": None, "raw": payload} + try: + cents = int(amount_cents) + except (TypeError, ValueError): + gold = None + else: + gold = cents // 100 if cents % 100 == 0 else cents / 100 + return {"gold": gold, "amount_cents": amount_cents, "raw": payload} def exchange_balance(self) -> dict[str, Any]: """查询钱包兑换中心余额。""" diff --git a/docker-compose.yml b/docker-compose.yml index 4aa1d26..86c5f96 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -123,6 +123,7 @@ services: - PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS=${PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS:-5} - DOUYU_BIND_ROLE_POLL_SECONDS=${DOUYU_BIND_ROLE_POLL_SECONDS:-1200} - PLATFORM_BINDING_QR_TTL_SECONDS=${PLATFORM_BINDING_QR_TTL_SECONDS:-1260} + - DOUYU_OVERVIEW_CACHE_SECONDS=${DOUYU_OVERVIEW_CACHE_SECONDS:-600} - DOUYU_DEFAULT_SUPPLY_TARGETS_ENABLED=${DOUYU_DEFAULT_SUPPLY_TARGETS_ENABLED:-true} # 用户上传图片的桶(在线协作 / P2 资料收集共用;生产用内网 endpoint)。 # OSS_MODE=oss 才走对象存储,否则落容器内 ./data/file_storage。 diff --git a/docs/斗鱼/DOUYU_PROTOCOL_OVERVIEW.md b/docs/斗鱼/DOUYU_PROTOCOL_OVERVIEW.md index b91e3fb..852a3c8 100644 --- a/docs/斗鱼/DOUYU_PROTOCOL_OVERVIEW.md +++ b/docs/斗鱼/DOUYU_PROTOCOL_OVERVIEW.md @@ -246,7 +246,35 @@ docker compose logs douyu-login | grep '确认前状态' ## 鱼翅、积分与支付 -- 鱼翅余额:`https://cz.douyu.com/item/gold/account` +2026-11-21 新抓包确认,鱼翅页面和支付链路已统一迁到 +`https://www.douyu.com/japi/web/gold/*`,不再使用旧的 +`cz.douyu.com/item/gold/account` 查询余额。 + +- 鱼翅初始化:`POST /japi/web/gold/init` + - 表单:`ctn=&scene=web` + - 返回当前账号、支付渠道和可充值商品面值 +- 鱼翅余额:`GET /japi/web/gold/account` + - 返回 `data.amount`,单位是 **0.01 鱼翅** + - 页面余额和本地 `gold_balance` 使用 `amount / 100` +- 鱼翅支付码:`GET /japi/web/gold/getQrCode` + - 参数:`payType`、`productId`、`number`、`source`、`toName` + - 返回 `token`、`code_url`、`polling_url` +- 鱼翅支付状态:`GET /japi/web/gold/getQrState?token=...` + - `state=-2`:等待支付 + - `state=0`:支付处理中 + - `state=2`:支付成功,同时返回 `quantity` 和 `orderId` +- 支付码生成任务保存 `payment_token`;轮询优先使用官方 `state=2` 收口, + 成功后立即刷新鱼翅余额。旧 token 或状态接口不可用时才回退到余额增量判断。 +- 积分余额:`POST /japi/oms/web/peace/credit/balance` + - 2026-11-21 页面使用 `multipart/form-data`,表单只传 `uid` + - `acf_ccn` 从 Cookie 复用;缺失时才请求补 token + - 返回 `balance`、`recentlyWillExpireBalance`、`willExpireTime` +- 积分到期批次:`POST /japi/oms/web/peace/credit/balanceDetail` + - JSON:`userId`、`current`、`pageSize` + - `records[]` 包含 `count` 和 `expireTime` +- 积分流水:`POST /japi/oms/web/peace/credit/colJournal` + - JSON:`uid`、`pageNum`、`pageSize` + - 可用于赠送、兑换后的积分增减精确核对 - 钱包兑换余额:`https://www.douyu.com/wjapi/nc/exchange/fim` - 支付码:`/japi/oms/web/pay/getQrCode` - 供应商鱼翅直充按供应商协议执行;到账必须核对斗鱼余额相对下单前基线增加, @@ -321,3 +349,4 @@ docker compose logs douyu-login | grep '确认前状态' | 2026-11-21 | 建立斗鱼协议总览;补充多 alias 职责、pending 真值表、结构化判定日志和同名 pending 修复记录 | | 2026-11-21 | 补充 `storehome` 单账号全量商品资格、`storedetail`/`batchExchangeLimit` 边界和抓包证据入口 | | 2026-11-21 | 补充同名重绑、轮询退避、直接绑定识别和角色接口原始响应留档 | +| 2026-11-21 | 按最新 HAR 更新鱼翅余额/充值二维码/支付状态、积分 multipart、到期批次和积分流水协议 | diff --git a/docs/斗鱼/斗鱼账号平台最终实施设计.md b/docs/斗鱼/斗鱼账号平台最终实施设计.md index a8cbaed..05a9bf3 100644 --- a/docs/斗鱼/斗鱼账号平台最终实施设计.md +++ b/docs/斗鱼/斗鱼账号平台最终实施设计.md @@ -11,15 +11,16 @@ 更新时间:2026-11-21 -整体完成度约 **84%**。 +阶段 0-4 已全部完成;阶段 5 尚未启动。若把阶段 5 的旧逻辑清理也计入, +整体实施进度约 **85%**。 | 阶段 | 完成度 | 已落地 | 主要缺口 | | --- | ---: | --- | --- | | 阶段 0:止乱 | 100% | 登录批次上限、任务槽隔离、连接及时释放 | 无 | -| 阶段 1:同步基础 | 95% | 新表、迁移、storehome 网关、全量快照、同步运行记录、内部工作队列执行器、session/points/gold/elite/esports/role/exchange 同步状态 | 少量旁路查询仍需继续收口 | -| 阶段 2:账号与 CK | 90% | CK 周期字段、invalid 分流、登录流水、日/小时/分类/单账号预算、login_relogin 队列真实消费与退避回队、预算配置 API | 更精细的动态预算计算 | +| 阶段 1:同步基础 | 100% | 新表、迁移、storehome 网关、全量快照、同步运行记录、内部工作队列执行器、全部账号资料域同步状态、一键查询本地快照复用、过期域后台入队、兑换记录事件补录与可回放投影 | 无;首次无本地投影时保留一次冷启动查询 | +| 阶段 2:账号与 CK | 100% | CK 周期字段、invalid 分流、登录流水、日/小时/分类/单账号预算、login_relogin 队列真实消费与退避回队、预算配置 API、库存与近期成功率驱动的预算建议 | 无 | | 阶段 3:商品与供给 | 100% | 全量商品能力、限时窗口、原始响应、供给库存和目标缺口、定向补货入队、供给目标配置 API 与管理 UI、默认目标、统一平台状态快照、供给看板、阈值告警、7 天查询/登录/CK 趋势 | 无 | -| 阶段 4:安全恢复自动化 | 95% | account_sync_queue 与供给补货作业默认小批量开启,自动重登消费工作队列,硬失败率熔断,登录预算在线配置且熔断阈值实时生效,按 7 天成功率自动扩缩批次,长期低成功率告警,登录预算建议与人工一键应用 | 生产灰度观察 | +| 阶段 4:安全恢复自动化 | 100% | account_sync_queue 与供给补货作业默认小批量开启,自动重登消费工作队列,硬失败率熔断,登录预算在线配置且熔断阈值实时生效,按 7 天成功率自动扩缩批次,长期低成功率告警,登录预算建议与人工一键应用 | 代码已完成;生产灰度效果继续观察 | | 阶段 5:清理旧逻辑 | 20% | 履约补货不再直接发官网请求 | 旧资料刷新、旁路查询和旧候选兜底仍需逐步下线 | ## 为什么重构 @@ -490,6 +491,23 @@ Runner 启动前必须有预算 token;没有 token 时留在工作队列。 - `StorehomeSnapshot` - 查询任务迁移到统一入口 +`query_account_overview` 已开始按本地投影优先执行: + +- 账号快照在 `DOUYU_OVERVIEW_CACHE_SECONDS` 内直接复用,不再请求官网; +- 商品同步状态新鲜时,从 `account_commodity_states.raw_json` 重建完整商品列表; +- 商品同步状态已存在但过期时,只生成 `storehome_sync` 工作项并返回本地旧快照; +- 只有从未同步过商品的账号保留一次实时冷启动查询; +- 任务结果增加 `cache_hit`、`cache_source`、`goods_source` 和 `pending_domains`。 + +账号资料同步已覆盖全部已建模查询域: + +- `points/gold/elite/esports/role/storehome/exchange_records` 均有独立工作类型; +- 已存在但过期的域只返回本地旧投影并入队,不再占用用户任务请求链; +- 只有从未同步过的域保留一次实时冷启动查询,随后写同步状态和可回放原始投影; +- 兑换记录同步统一处理精英宝典、电竞手册和按开关启用的和平小店,并通过事件账本幂等补录; +- 单项资料同步使用 `update_session=False`,不会被派生会话结果误写 CK 生命周期; +- 缓存时间对 MySQL 秒级 DATETIME 四舍五入提供容差,避免同秒重试重复请求官网。 + ### 阶段 2:账号与 CK - CK 周期字段 @@ -649,3 +667,5 @@ Runner 启动前必须有预算 token;没有 token 时留在工作队列。 | 2026-11-21 | 完成阶段 3:增加 7 天查询、登录和 CK 生命周期趋势,并补充同步时间索引 | | 2026-11-21 | 阶段 4:自动重登按 7 天成功率灰度扩缩批次,并增加长期低成功率告警 | | 2026-11-21 | 阶段 4:增加基于库存、积压和近期成功率的登录预算建议及人工一键应用 | +| 2026-11-21 | 阶段 1:一键查询接入本地快照复用,商品过期改为后台同步,避免重复官网查询 | +| 2026-11-21 | 阶段 0-4 完成:全部账号查询域接入本地优先策略、独立同步工作项和兑换记录统一投影;阶段 5 暂不启动 | diff --git a/tests/test_account_overview.py b/tests/test_account_overview.py index 9435158..08de1a2 100644 --- a/tests/test_account_overview.py +++ b/tests/test_account_overview.py @@ -13,7 +13,15 @@ from unittest.mock import Mock import pytest from tests.support.db import get_test_engine, make_test_session -from web.backend.models import Account, AccountEvent, DouyuTask, User +from web.backend.models import ( + Account, + AccountCommodityState, + AccountEvent, + AccountSyncState, + AccountWorkItem, + DouyuTask, + User, +) from web.backend.services.douyu_runner_overview import ( DEFAULT_HIGH_VALUE_GOODS, OverviewMixin, @@ -423,6 +431,176 @@ class TestOverviewAggregation: self.session.refresh(self.account) assert self.account.status == status_before + def test_reuses_fresh_overview_without_calling_official_helpers( + self, + monkeypatch, + ): + monkeypatch.setenv("DOUYU_OVERVIEW_CACHE_SECONDS", "600") + self.account.overview_snapshot = { + "queried_at": datetime.now(UTC).isoformat(), + "assets": {"gold_balance": 7}, + "high_value_goods": [ + {"name": "套装-兔兔白日梦", "owned": True, "missing": False} + ], + } + self.account.overview_queried_at = datetime.now(UTC) + self.session.commit() + probe = _Harness(_FakeClient()) + assert probe._cached_overview( + self.account, + self.task, + now=datetime.now(UTC), + ) is not None + + h = self._run(client=object()) + result = h.marked["result"] + + assert h.marked["status"] == "success" + assert result["cache_hit"] is True + assert result["assets"]["gold_balance"] == 7 + assert "兔兔白日梦" in h.marked["message"] + + def test_stale_storehome_projection_is_queued_without_official_query(self): + now = datetime.now(UTC) + self.account.overview_queried_at = now - timedelta(hours=1) + self.account.overview_snapshot = { + "queried_at": self.account.overview_queried_at.isoformat(), + "high_value_goods": [], + } + self.session.add_all( + [ + AccountSyncState( + account_id=self.account.id, + platform="douyu", + domain="storehome", + status="success", + last_success_at=now - timedelta(hours=1), + valid_until=now - timedelta(minutes=1), + ), + AccountCommodityState( + account_id=self.account.id, + platform="douyu", + scope="elite", + commodity_id=RABBIT_ID, + commodity_name="套装-兔兔白日梦", + availability="available", + observed_at=now - timedelta(hours=1), + valid_until=now - timedelta(minutes=1), + raw_json=GOODS_AVAILABLE, + ), + ] + ) + self.session.commit() + seen: list[str] = [] + + class _Client(_FakeClient): + def list_goods(self, *, manual_id, rid): + seen.append(manual_id) + return super().list_goods(manual_id=manual_id, rid=rid) + + result = self._run(client=_Client()).marked["result"] + work = self.session.query(AccountWorkItem).one() + + assert seen == [] + assert result["pending_domains"] == ["storehome"] + assert result["goods_source"] == "local_stale_projection" + assert result["limited_goods_total"] == 1 + assert work.work_type == "storehome_sync" + assert work.account_id == self.account.id + + def test_fresh_storehome_projection_skips_official_query(self): + now = datetime.now(UTC) + self.session.add_all( + [ + AccountSyncState( + account_id=self.account.id, + platform="douyu", + domain="storehome", + status="success", + last_success_at=now, + valid_until=now + timedelta(minutes=10), + ), + AccountCommodityState( + account_id=self.account.id, + platform="douyu", + scope="elite", + commodity_id=RABBIT_ID, + commodity_name="套装-兔兔白日梦", + availability="available", + observed_at=now, + valid_until=now + timedelta(minutes=10), + raw_json=GOODS_AVAILABLE, + ), + ] + ) + self.session.commit() + seen: list[str] = [] + + class _Client(_FakeClient): + def list_goods(self, *, manual_id, rid): + seen.append(manual_id) + return super().list_goods(manual_id=manual_id, rid=rid) + + result = self._run(client=_Client()).marked["result"] + + assert seen == [] + assert result["goods_source"] == "local_fresh_projection" + assert result["pending_domains"] == [] + assert result["limited_goods_total"] == 1 + assert self.session.query(AccountWorkItem).count() == 0 + + def test_stale_esports_and_exchange_records_use_local_projection_and_queue(self): + now = datetime.now(UTC) + self.account.esports_opened = True + self.account.esports_points = 88 + self.account.esports_expire_time = 1795967999 + self.session.add_all( + [ + AccountSyncState( + account_id=self.account.id, + platform="douyu", + domain="esports", + status="success", + last_success_at=now - timedelta(hours=1), + valid_until=now - timedelta(minutes=1), + raw_payload_json={"manual_type": 2, "manual_score": 88}, + ), + AccountSyncState( + account_id=self.account.id, + platform="douyu", + domain="exchange_records", + status="success", + last_success_at=now - timedelta(hours=1), + valid_until=now - timedelta(minutes=1), + raw_payload_json={ + "elite": [ + { + "commodityId": "cached-item", + "commodityName": "缓存套装", + } + ] + }, + ), + ] + ) + self.session.commit() + + result = self._run().marked["result"] + work_types = { + row.work_type + for row in self.session.query(AccountWorkItem) + .filter_by(account_id=self.account.id) + .all() + } + + assert result["domain_sources"]["esports"] == "queued_stale" + assert result["domain_sources"]["exchange_records"] == "queued_stale" + assert result["esports"]["opened"] is True + assert result["esports"]["points"] == 88 + assert result["exchange_records"]["elite"][0]["commodityId"] == "cached-item" + assert {"esports", "exchange_records"}.issubset(result["pending_domains"]) + assert work_types == {"esports_sync", "exchange_records_sync"} + class TestOverviewTaskTypeWiring: def test_declared_for_all_workbenches(self): diff --git a/tests/test_account_sync_runner.py b/tests/test_account_sync_runner.py index 7b611d2..740d077 100644 --- a/tests/test_account_sync_runner.py +++ b/tests/test_account_sync_runner.py @@ -8,11 +8,14 @@ from tests.support.db import make_test_session from web.backend.models import ( Account, AccountCommodityState, + AccountEvent, AccountSyncState, AccountWorkItem, DouyuConfig, ) from web.backend.services.account_sync_runner import ( + WORK_ESPORTS_SYNC, + WORK_EXCHANGE_RECORDS_SYNC, WORK_STOREHOME_SYNC, execute_account_work_item, run_account_sync_queue, @@ -25,11 +28,12 @@ from web.backend.services.account_work_queue import ( from web.backend.services.douyu_read_gateway import StorehomeSnapshot -def _config(db) -> None: +def _config(db, **overrides) -> None: db.add( DouyuConfig( manual_id="manual", rid="room", + **overrides, ) ) db.commit() @@ -161,6 +165,122 @@ def test_work_item_failure_returns_to_pending_for_retry(db): assert work_row.due_at > datetime.now(UTC) +def test_esports_profile_sync_persists_data_without_rewriting_ck(db): + _config(db, esports_manual_id="esports-manual") + account = _account("sync-esports") + db.add(account) + db.commit() + work = enqueue_account_work( + db, + account_id=account.id, + work_type=WORK_ESPORTS_SYNC, + ) + db.commit() + claim_account_work(db, work_types=(WORK_ESPORTS_SYNC,), limit=1) + db.commit() + seen: dict = {} + + def probe(_account, _config, **kwargs): + seen.update(kwargs) + return { + "probe_id": "work:1", + "identity": {"uid": "100", "username": "sync-esports"}, + # 单项资料同步没有独立执行 CK 探针;即使内部推导出 invalid, + # 也不能覆盖账号主表已有的有效 CK。 + "session": {"status": "invalid", "message": "推导值"}, + "elite": {}, + "eligibility": {}, + "roles": {}, + "gold": {}, + "goods": {}, + "esports": { + "status": "ok", + "opened": True, + "manual_type": 2, + "manual_score": 321, + "expire_time": 1795967999, + }, + "errors": {}, + } + + result = execute_account_work_item( + work.id, + session_factory=make_test_session, + client_factory=lambda _cookie: object(), + probe_fetcher=probe, + ) + + with make_test_session() as verify: + row = verify.get(Account, account.id) + state = ( + verify.query(AccountSyncState) + .filter_by(account_id=account.id, domain="esports") + .one() + ) + assert result["ok"] is True + assert seen["with_esports"] is True + assert seen["with_elite"] is False + assert row.ck_check_status == "valid" + assert row.esports_opened is True + assert row.esports_points == 321 + assert row.esports_expire_time == 1795967999 + assert state.status == "success" + + +def test_exchange_record_work_persists_events_and_raw_projection(db): + _config(db, esports_manual_id="esports-manual") + account = _account("sync-exchange") + db.add(account) + db.commit() + work = enqueue_account_work( + db, + account_id=account.id, + work_type=WORK_EXCHANGE_RECORDS_SYNC, + ) + db.commit() + claim_account_work(db, work_types=(WORK_EXCHANGE_RECORDS_SYNC,), limit=1) + db.commit() + + class _Client: + def exchange_records(self, *, manual_id, limit, offset=0): + return { + "records": [ + { + "commodityId": f"{manual_id}-item", + "commodityName": "测试套装", + "score": "18", + "time": "1795967997", + } + ] + } + + result = execute_account_work_item( + work.id, + session_factory=make_test_session, + client_factory=lambda _cookie: _Client(), + ) + + with make_test_session() as verify: + state = ( + verify.query(AccountSyncState) + .filter_by(account_id=account.id, domain="exchange_records") + .one() + ) + events = ( + verify.query(AccountEvent) + .filter_by(account_id=account.id, event_type="exchange") + .all() + ) + assert result["ok"] is True + assert result["item_count"] == 2 + assert set(state.raw_payload_json) == {"elite", "esports"} + assert state.raw_payload_json["elite"][0]["commodityId"] == "manual-item" + assert {event.subject_key for event in events} == { + "manual-item", + "esports-manual-item", + } + + def test_run_account_sync_queue_claims_and_executes(db): _config(db) account = _account("sync-runner") diff --git a/tests/test_douyu_activity_client_protocol.py b/tests/test_douyu_activity_client_protocol.py new file mode 100644 index 0000000..25dcb38 --- /dev/null +++ b/tests/test_douyu_activity_client_protocol.py @@ -0,0 +1,171 @@ +"""2026-11-21 斗鱼积分/鱼翅最新协议回归。""" + +from __future__ import annotations + +from typing import Any + +from core.douyu import DouyuActivityClient +from web.backend.services.douyu_activity_query import extract_points_expire_time + + +def _client(cookie: str = "acf_uid=100; acf_ccn=csrf-cookie") -> DouyuActivityClient: + return DouyuActivityClient(cookie) + + +def test_acf_ccn_reuses_existing_cookie_without_http(monkeypatch): + client = _client() + + def fail(*_args, **_kwargs): + raise AssertionError("Cookie 已有 acf_ccn 时不应请求补 token") + + monkeypatch.setattr(client, "_request", fail) + + assert client.acf_ccn(refresh_subscribe=False) == "csrf-cookie" + + +def test_points_uses_latest_multipart_and_returns_expiry(monkeypatch): + client = _client() + calls: list[tuple[str, str, dict[str, Any]]] = [] + + def request_json(method, url, source, **kwargs): + calls.append((method, url, kwargs)) + return { + "error": 0, + "data": { + "userId": 100, + "balance": 300, + "recentlyWillExpireBalance": 200, + "willExpireTime": 1803052799, + }, + } + + monkeypatch.setattr(client, "_request_json", request_json) + + result = client.query_points(uid="100", ctn="csrf-cookie") + + assert calls[0][0:2] == ( + "post", + "https://www.douyu.com/japi/oms/web/peace/credit/balance", + ) + assert calls[0][2]["files"] == {"uid": (None, "100")} + assert "json" not in calls[0][2] + assert result["points"] == 300 + assert result["recently_expiring_points"] == 200 + assert result["will_expire_time"] == 1803052799 + + +def test_points_expiry_extractor_supports_normalized_and_raw_shapes(): + assert ( + extract_points_expire_time({"will_expire_time": "1803052799"}) + == 1803052799 + ) + assert ( + extract_points_expire_time( + {"raw": {"data": {"willExpireTime": 1803052799000}}} + ) + == 1803052799 + ) + + +def test_gold_account_uses_web_endpoint_and_converts_cents(monkeypatch): + client = _client() + seen: list[tuple[str, str]] = [] + + def request_json(method, url, source, **_kwargs): + seen.append((method, url)) + return { + "code": 0, + "success": True, + "data": {"amount": 1000}, + } + + monkeypatch.setattr(client, "_request_json", request_json) + + result = client.gold_account() + + assert seen == [ + ("get", "https://www.douyu.com/japi/web/gold/account"), + ] + assert result["amount_cents"] == 1000 + assert result["gold"] == 10 + + +def test_gold_qr_uses_init_then_get_qr_and_returns_polling_token(monkeypatch): + client = _client() + calls: list[tuple[str, str, dict[str, Any]]] = [] + monkeypatch.setattr( + client, + "acf_ccn", + lambda **_kwargs: "csrf-cookie", + ) + + def request_json(method, url, source, **kwargs): + calls.append((method, url, kwargs)) + if url.endswith("/init"): + return { + "code": 0, + "success": True, + "data": {"uid": 100, "nickName": "测试用户", "scene": "web"}, + } + return { + "code": 0, + "success": True, + "data": { + "token": "pay-token", + "code_url": "https://cz.douyu.com/pages/one-pay/order/pay-token", + "polling_url": ( + "https://www.douyu.com/japi/web/gold/getQrState" + "?token=pay-token" + ), + }, + } + + monkeypatch.setattr(client, "_request_json", request_json) + + result = client.create_gold_qr(amount=10, pay_type=1) + + assert calls[0][0:2] == ( + "post", + "https://www.douyu.com/japi/web/gold/init", + ) + assert calls[0][2]["data"] == {"ctn": "csrf-cookie", "scene": "web"} + assert calls[1][0:2] == ( + "get", + "https://www.douyu.com/japi/web/gold/getQrCode", + ) + assert calls[1][2]["params"] == { + "payType": "1", + "productId": "DYTV_Product_Gold_10", + "number": "1", + "source": "", + "toName": "", + } + assert result["token"] == "pay-token" + assert result["pay_url"].endswith("/pay-token") + + +def test_gold_qr_state_maps_success_order(monkeypatch): + client = _client() + + def request_json(method, url, source, **kwargs): + assert method == "get" + assert url == "https://www.douyu.com/japi/web/gold/getQrState" + assert kwargs["params"] == {"token": "pay-token"} + return { + "code": 0, + "success": True, + "data": { + "state": 2, + "quantity": 10, + "nickname": "测试用户", + "orderId": "order-100", + }, + } + + monkeypatch.setattr(client, "_request_json", request_json) + + result = client.query_gold_qr_state("pay-token") + + assert result["state"] == 2 + assert result["quantity"] == 10 + assert result["order_id"] == "order-100" diff --git a/tests/test_douyu_activity_query.py b/tests/test_douyu_activity_query.py index c7d58ad..345923c 100644 --- a/tests/test_douyu_activity_query.py +++ b/tests/test_douyu_activity_query.py @@ -20,10 +20,13 @@ READ_METHODS = { "exchange_orders", "exchange_records", "gold_account", + "query_gold_qr_state", "list_esports_goods", "list_goods", "query_limited_goods", "query_points", + "query_points_expirations", + "query_points_ledger", "xpd_bind_info", } diff --git a/tests/test_douyu_gold_recharge_channel.py b/tests/test_douyu_gold_recharge_channel.py index d15588e..fb86220 100644 --- a/tests/test_douyu_gold_recharge_channel.py +++ b/tests/test_douyu_gold_recharge_channel.py @@ -144,6 +144,49 @@ class TestDouyuGoldRechargeChannel: assert self.task.queue_lane == "" assert self.task.result["gold_recharged"] is True + def test_wallet_gold_qr_uses_official_paid_state_before_balance_delta(self): + config = { + "gold_recharge_channel": "wechat_qr", + "gold_pay_type": 1, + } + self.task.result = { + "payment_amount": 10, + "payment_channel": "wechat_qr", + "payment_token": "pay-token", + "baseline_gold_balance": 0, + } + self.session.commit() + client = Mock() + client.query_gold_qr_state.return_value = { + "state": 2, + "quantity": 10, + "order_id": "gold-order-10", + } + self.runner._client = Mock(return_value=client) + self.runner._refresh_account_gold_balance = Mock( + return_value={ + "gold_balance": 10, + "exchange_balance": 0, + "gold": {"gold": 10}, + "exchange_balance_query": {"count": 0}, + } + ) + + self.runner._poll_create_gold_qr_once( + self.session, + self.task, + self.account, + "acf_uid=10001", + config, + ) + + self.session.refresh(self.task) + assert self.task.status == "success" + assert self.task.queue_lane == "" + assert self.task.result["gold_confirm_source"] == "gold_qr_state" + assert self.task.result["order_id"] == "gold-order-10" + assert self.task.result["gold_refreshed_after_success"] is True + @patch("web.backend.services.douyu_runner_gold.FishFinRechargeClient") def test_supplier_channel_creates_order_using_nickname_and_finishes_on_success( self, client_class diff --git a/tests/test_migration_high_value_roundtrip.py b/tests/test_migration_high_value_roundtrip.py index 04a8b53..f37ef43 100644 --- a/tests/test_migration_high_value_roundtrip.py +++ b/tests/test_migration_high_value_roundtrip.py @@ -12,6 +12,8 @@ from sqlalchemy import create_engine, inspect, text import web.backend.database as database_module from tests.support.db import get_mysql_credentials +TARGET_REVISION = "20261106_0065" + def test_0065_upgrade_downgrade_roundtrip(): creds = get_mysql_credentials() @@ -91,7 +93,7 @@ def test_0065_upgrade_downgrade_roundtrip(): }, ) - command.upgrade(cfg, "head") + command.upgrade(cfg, TARGET_REVISION) insp = inspect(engine) cols = {c["name"] for c in insp.get_columns("accounts")} @@ -144,7 +146,7 @@ def test_0065_upgrade_downgrade_roundtrip(): }, ) conn.execute(text("UPDATE alembic_version SET version_num='20261021_0064'")) - command.upgrade(cfg, "head") + command.upgrade(cfg, TARGET_REVISION) with engine.connect() as conn: resumed = conn.execute( text( @@ -200,7 +202,7 @@ def test_0065_upgrade_downgrade_roundtrip(): assert old_values["downgrade-1"] == ["套装-兔兔白日梦"] assert old_values["downgrade-2"] == ["套装-兔兔白日梦", "套装-狂战萌兔"] - command.upgrade(cfg, "head") + command.upgrade(cfg, TARGET_REVISION) insp = inspect(engine) cols = {c["name"] for c in insp.get_columns("accounts")} assert "high_value_owned" not in cols diff --git a/web/backend/services/account_exchange_sync_service.py b/web/backend/services/account_exchange_sync_service.py new file mode 100644 index 0000000..365a24a --- /dev/null +++ b/web/backend/services/account_exchange_sync_service.py @@ -0,0 +1,221 @@ +"""斗鱼官方兑换记录同步与事件补录。""" + +from __future__ import annotations + +from datetime import UTC, datetime +from typing import Any + +from sqlalchemy.orm import Session + +from core.douyu import DouyuActivityClient + +from ..models import Account +from .account_sync_service import ( + DOMAIN_EXCHANGE_RECORDS, + get_sync_state, + record_sync_success, +) +from .business_events import record_official_exchange +from .douyu_activity_query import ( + query_exchange_records, + query_xpd_embed, +) + +EXCHANGE_RECORD_FETCH_LIMIT = 100 + + +def _to_int(value: Any) -> int | None: + if value is None or value == "" or isinstance(value, bool): + return None + try: + return int(float(value)) + except (TypeError, ValueError, OverflowError): + return None + + +def _parse_official_time(value: Any) -> datetime | None: + if value is None or isinstance(value, bool): + return None + if isinstance(value, (int, float)): + seconds = float(value) + else: + text = str(value).strip() + if not text: + return None + if text.isdigit(): + seconds = float(text) + else: + for fmt in ("%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d"): + try: + return datetime.strptime(text, fmt).replace(tzinfo=UTC) + except ValueError: + continue + return None + if seconds > 10_000_000_000: + seconds /= 1000 + try: + return datetime.fromtimestamp(seconds, tz=UTC) + except (OverflowError, OSError, ValueError): + return None + + +def _group_by_scope( + pairs: list[tuple[str, dict[str, Any]]], +) -> dict[str, list[dict[str, Any]]]: + grouped: dict[str, list[dict[str, Any]]] = {} + for scope, record in pairs: + grouped.setdefault(scope, []).append(record) + return grouped + + +def persist_official_exchange_records( + db: Session, + account: Account, + pairs: list[tuple[str, dict[str, Any]]], +) -> int: + """把官方记录幂等补成账号事件,返回新写入数量。""" + added = 0 + for scope, record in pairs: + if not isinstance(record, dict): + continue + occurred_at = _parse_official_time(record.get("time")) + if record_official_exchange( + db, + account, + commodity_id=str( + record.get("commodityId") or record.get("commodity_id") or "" + ), + commodity_name=str( + record.get("commodityName") or record.get("commodity_name") or "" + ), + score=_to_int(record.get("score")), + occurred_at=occurred_at, + show_name=str( + record.get("showName") or record.get("show_name") or "" + ), + exchange_num=record.get("exchangeNum") or record.get("count"), + scope=scope, + ): + added += 1 + return added + + +def fetch_exchange_records( + client: DouyuActivityClient, + config: dict[str, Any], + *, + limit: int = EXCHANGE_RECORD_FETCH_LIMIT, +) -> tuple[list[tuple[str, dict[str, Any]]], dict[str, str]]: + """拉取已配置作用域的官方兑换记录,单项失败不阻断其他作用域。""" + pairs: list[tuple[str, dict[str, Any]]] = [] + errors: dict[str, str] = {} + manual_id = str(config.get("manual_id") or "").strip() + esports_manual_id = str(config.get("esports_manual_id") or "").strip() + configured = False + successful = 0 + + for scope, label, selected_manual_id in ( + ("elite", "精英宝典", manual_id), + ("esports", "电竞手册", esports_manual_id), + ): + if not selected_manual_id: + continue + configured = True + try: + rows = query_exchange_records( + client, + manual_id=selected_manual_id, + limit=limit, + ) + except Exception as exc: # noqa: BLE001 - 单个外部查询域独立失败 + errors[f"exchange_records_{scope}"] = ( + f"{type(exc).__name__}: {exc}"[:200] + ) + continue + successful += 1 + pairs.extend((scope, dict(row)) for row in rows if isinstance(row, dict)) + + if bool(config.get("peace_enabled")): + xpd_act_alias = str(config.get("xpd_act_alias") or "").strip() + xpd_rid = str(config.get("xpd_rid") or "").strip() + xpd_act_id = str(config.get("xpd_act_id") or "").strip() + if xpd_act_alias and xpd_rid and xpd_act_id: + configured = True + try: + embed = query_xpd_embed( + client, + act_alias=xpd_act_alias, + rid=xpd_rid, + ) + rows = ( + client.xpd_purchase_records( + embed_query=embed["query"], + act_id=xpd_act_id, + ).get("records") + or [] + ) + pairs.extend( + ("peace", dict(row)) for row in rows if isinstance(row, dict) + ) + successful += 1 + except Exception as exc: # noqa: BLE001 - 单个外部查询域独立失败 + errors["exchange_records_peace"] = ( + f"{type(exc).__name__}: {exc}"[:200] + ) + + if not configured: + raise RuntimeError("未配置可同步的斗鱼活动参数") + if successful == 0: + raise RuntimeError(";".join(errors.values()) or "兑换记录查询失败") + return pairs, errors + + +def sync_exchange_records( + db: Session, + account: Account, + client: DouyuActivityClient, + config: dict[str, Any], + *, + source: str, + task_id: int | None = None, +) -> dict[str, Any]: + """同步官方记录、补事件并保存可回放的按作用域原始投影。""" + observed_at = datetime.now(UTC) + pairs, errors = fetch_exchange_records(client, config) + changed = persist_official_exchange_records(db, account, pairs) + previous = get_sync_state( + db, + account_id=int(account.id), + domain=DOMAIN_EXCHANGE_RECORDS, + ) + grouped = ( + dict(previous.raw_payload_json) + if previous is not None and isinstance(previous.raw_payload_json, dict) + else {} + ) + grouped.update(_group_by_scope(pairs)) + record_sync_success( + db, + account_id=int(account.id), + domain=DOMAIN_EXCHANGE_RECORDS, + source=source, + observed_at=observed_at, + item_count=len(pairs), + changed_count=changed, + raw_payload=grouped, + task_id=task_id, + ) + return { + "item_count": len(pairs), + "changed_count": changed, + "records": grouped, + "errors": errors, + } + + +__all__ = [ + "EXCHANGE_RECORD_FETCH_LIMIT", + "fetch_exchange_records", + "persist_official_exchange_records", + "sync_exchange_records", +] diff --git a/web/backend/services/account_sync_runner.py b/web/backend/services/account_sync_runner.py index b02b5a1..6551cfe 100644 --- a/web/backend/services/account_sync_runner.py +++ b/web/backend/services/account_sync_runner.py @@ -11,19 +11,48 @@ from sqlalchemy.orm import Session from ..database import WorkerSessionLocal from ..models import Account, AccountWorkItem -from .account_sync_service import DOMAIN_STOREHOME, sync_storehome_snapshot +from .account_exchange_sync_service import sync_exchange_records +from .account_sync_service import ( + DOMAIN_ELITE, + DOMAIN_ESPORTS, + DOMAIN_EXCHANGE_RECORDS, + DOMAIN_GOLD, + DOMAIN_POINTS, + DOMAIN_ROLE, + DOMAIN_STOREHOME, + sync_storehome_snapshot, +) from .account_work_queue import ( claim_account_work, complete_account_work, fail_account_work, ) +from .douyu_account_probe import ( + config_mapping, + persist_probe_result, + probe_account, +) from .douyu_activity_query import activity_client from .douyu_read_gateway import StorehomeSnapshot, fetch_storehome_snapshot from .douyu_service import ensure_douyu_config from .task_runtime import TaskLane, available_slots, submit WORK_STOREHOME_SYNC = DOMAIN_STOREHOME + "_sync" -ACCOUNT_SYNC_WORK_TYPES = (WORK_STOREHOME_SYNC,) +WORK_POINTS_SYNC = DOMAIN_POINTS + "_sync" +WORK_GOLD_SYNC = DOMAIN_GOLD + "_sync" +WORK_ROLE_SYNC = DOMAIN_ROLE + "_sync" +WORK_ELITE_SYNC = DOMAIN_ELITE + "_sync" +WORK_ESPORTS_SYNC = DOMAIN_ESPORTS + "_sync" +WORK_EXCHANGE_RECORDS_SYNC = DOMAIN_EXCHANGE_RECORDS + "_sync" +ACCOUNT_SYNC_WORK_TYPES = ( + WORK_STOREHOME_SYNC, + WORK_POINTS_SYNC, + WORK_GOLD_SYNC, + WORK_ROLE_SYNC, + WORK_ELITE_SYNC, + WORK_ESPORTS_SYNC, + WORK_EXCHANGE_RECORDS_SYNC, +) def _execute_storehome_sync( @@ -64,12 +93,152 @@ def _execute_storehome_sync( } +def _profile_probe_kwargs( + work_type: str, + config: Any, +) -> dict[str, Any]: + base = { + "with_elite": False, + "with_esports": False, + "with_storehome": False, + "with_eligibility": False, + "with_limited_goods": False, + "with_gold": False, + "with_goods": False, + "with_elite_role": False, + "include_esports": False, + "include_peace": False, + "use_cache": False, + } + if work_type == WORK_POINTS_SYNC: + return {**base, "with_eligibility": True} + if work_type == WORK_GOLD_SYNC: + return {**base, "with_gold": True} + if work_type == WORK_ROLE_SYNC: + return { + **base, + "with_elite_role": True, + "include_esports": True, + "include_peace": bool(getattr(config, "peace_enabled", False)), + } + if work_type == WORK_ELITE_SYNC: + return {**base, "with_elite": True} + if work_type == WORK_ESPORTS_SYNC: + return {**base, "with_esports": True} + raise RuntimeError(f"不支持的账号资料同步类型: {work_type}") + + +def _profile_domain_succeeded(work_type: str, probe: dict[str, Any]) -> bool: + if work_type == WORK_POINTS_SYNC: + eligibility = probe.get("eligibility") or {} + return bool( + eligibility.get("points") is not None + and isinstance(eligibility.get("points_response"), dict) + and eligibility["points_response"] + ) + if work_type == WORK_GOLD_SYNC: + return (probe.get("gold") or {}).get("status") == "ok" + if work_type == WORK_ROLE_SYNC: + roles = probe.get("roles") or {} + return any( + (role or {}).get("status") in {"bound", "pending", "unbound"} + for role in roles.values() + if isinstance(role, dict) + ) + if work_type == WORK_ELITE_SYNC: + return (probe.get("elite") or {}).get("status") == "ok" + if work_type == WORK_ESPORTS_SYNC: + return (probe.get("esports") or {}).get("status") == "ok" + return False + + +def _execute_profile_sync( + db: Session, + *, + account: Account, + item: AccountWorkItem, + client_factory: Callable[[str], Any], + probe_fetcher: Callable[..., dict[str, Any]], +) -> dict[str, Any]: + if not account.cookie: + raise RuntimeError("账号没有可用 CK,不能同步账号资料") + if account.ck_check_status in {"invalid", "manual"}: + raise RuntimeError(f"账号 CK 状态为 {account.ck_check_status},暂不能同步") + config = ensure_douyu_config(db) + client = client_factory(account.cookie) + probe = probe_fetcher( + account, + config, + client=client, + **_profile_probe_kwargs(item.work_type, config), + ) + errors = probe.get("errors") or {} + domain_error = { + WORK_POINTS_SYNC: "points", + WORK_GOLD_SYNC: "gold", + WORK_ROLE_SYNC: "elite_role", + WORK_ELITE_SYNC: "elite", + WORK_ESPORTS_SYNC: "esports", + }.get(item.work_type, "") + if domain_error and domain_error in errors: + raise RuntimeError(str(errors[domain_error])) + if not _profile_domain_succeeded(item.work_type, probe): + detail = ";".join(str(value) for value in errors.values()) + raise RuntimeError(detail or f"{item.work_type} 未返回可落库结果") + persist_probe_result( + db, + account, + probe, + source=f"account_work.{item.work_type}", + record_event=False, + update_session=False, + ) + return { + "ok": True, + "work_id": int(item.id), + "account_id": int(account.id), + "work_type": item.work_type, + "errors": sorted(errors), + } + + +def _execute_exchange_records_sync( + db: Session, + *, + account: Account, + item: AccountWorkItem, + client_factory: Callable[[str], Any], +) -> dict[str, Any]: + if not account.cookie: + raise RuntimeError("账号没有可用 CK,不能同步兑换记录") + if account.ck_check_status in {"invalid", "manual"}: + raise RuntimeError( + f"账号 CK 状态为 {account.ck_check_status},暂不能同步兑换记录" + ) + config = ensure_douyu_config(db) + result = sync_exchange_records( + db, + account, + client_factory(account.cookie), + config_mapping(config), + source=f"account_work.{item.work_type}", + ) + return { + "ok": True, + "work_id": int(item.id), + "account_id": int(account.id), + "work_type": item.work_type, + **result, + } + + def execute_account_work_item( work_id: int, *, session_factory: Callable[[], Session] = WorkerSessionLocal, client_factory: Callable[[str], Any] = activity_client, snapshot_fetcher: Callable[..., StorehomeSnapshot] = fetch_storehome_snapshot, + probe_fetcher: Callable[..., dict[str, Any]] = probe_account, ) -> dict[str, Any]: """在一个短事务中执行单条账号维护工作。""" session = session_factory() @@ -81,15 +250,31 @@ def execute_account_work_item( account = session.get(Account, int(item.account_id)) if account is None: raise RuntimeError("工作项对应账号不存在") - if item.work_type != WORK_STOREHOME_SYNC: + if item.work_type == WORK_STOREHOME_SYNC: + result = _execute_storehome_sync( + session, + account=account, + item=item, + client_factory=client_factory, + snapshot_fetcher=snapshot_fetcher, + ) + elif item.work_type == WORK_EXCHANGE_RECORDS_SYNC: + result = _execute_exchange_records_sync( + session, + account=account, + item=item, + client_factory=client_factory, + ) + elif item.work_type in ACCOUNT_SYNC_WORK_TYPES: + result = _execute_profile_sync( + session, + account=account, + item=item, + client_factory=client_factory, + probe_fetcher=probe_fetcher, + ) + else: raise RuntimeError(f"不支持的工作类型: {item.work_type}") - result = _execute_storehome_sync( - session, - account=account, - item=item, - client_factory=client_factory, - snapshot_fetcher=snapshot_fetcher, - ) complete_account_work(session, item) session.commit() return result @@ -122,6 +307,7 @@ def run_account_sync_queue( session_factory: Callable[[], Session] = WorkerSessionLocal, client_factory: Callable[[str], Any] = activity_client, snapshot_fetcher: Callable[..., StorehomeSnapshot] = fetch_storehome_snapshot, + probe_fetcher: Callable[..., dict[str, Any]] = probe_account, ) -> dict[str, Any]: """领取少量到期工作并通过独立账号同步槽执行。""" moment = now or datetime.now(UTC) @@ -159,6 +345,7 @@ def run_account_sync_queue( session_factory=session_factory, client_factory=client_factory, snapshot_fetcher=snapshot_fetcher, + probe_fetcher=probe_fetcher, ) if future is not None: futures[future] = work_id @@ -194,6 +381,12 @@ def run_account_sync_queue( __all__ = [ "ACCOUNT_SYNC_WORK_TYPES", + "WORK_ELITE_SYNC", + "WORK_ESPORTS_SYNC", + "WORK_EXCHANGE_RECORDS_SYNC", + "WORK_GOLD_SYNC", + "WORK_POINTS_SYNC", + "WORK_ROLE_SYNC", "WORK_STOREHOME_SYNC", "execute_account_work_item", "run_account_sync_queue", diff --git a/web/backend/services/douyu_account_probe.py b/web/backend/services/douyu_account_probe.py index e893ba7..17ea947 100644 --- a/web/backend/services/douyu_account_probe.py +++ b/web/backend/services/douyu_account_probe.py @@ -40,6 +40,7 @@ from .account_events_service import CATEGORY_OPS, emit from .account_lifecycle import on_ck_verdict from .account_sync_service import ( DOMAIN_ELITE, + DOMAIN_ESPORTS, DOMAIN_GOLD, DOMAIN_POINTS, DOMAIN_ROLE, @@ -49,11 +50,13 @@ from .account_sync_service import ( from .cookie_check_service import check_douyu_cookie, classify_douyu_cookie_result from .douyu_activity_query import ( activity_client, + extract_points_expire_time, query_activity_goods, query_bind_info, query_csrf, query_elite_user_info, query_esports_bind_info, + query_esports_user_info, query_gold_account, query_limited_goods_result, query_points, @@ -520,14 +523,10 @@ def _points_expire_at( *, checked_at: datetime, ) -> datetime | None: - raw = (points_response or {}).get("willExpireTime") or (points_response or {}).get( - "will_expire_time" - ) - value = _to_int(raw) - if value is None or value <= 0: + # 2026-11-21:积分响应可能是归一化结果、raw 或 raw.data,统一走同一个提取器。 + value = extract_points_expire_time(points_response) + if value is None: return None - if value > 10_000_000_000: - value //= 1000 if value < int(checked_at.timestamp()) - 60: return None return datetime.fromtimestamp(value, tz=UTC) @@ -613,6 +612,8 @@ def probe_account( _DEFAULT_SESSION_CHECKER ), with_elite: bool = True, + with_esports: bool = False, + with_storehome: bool = True, with_eligibility: bool = False, with_limited_goods: bool = False, with_gold: bool = False, @@ -628,6 +629,8 @@ def probe_account( cache_mode = "|".join( [ f"elite={int(with_elite)}", + f"esports_info={int(with_esports)}", + f"storehome={int(with_storehome)}", f"elig={int(with_eligibility)}", f"limited={int(with_limited_goods)}", f"gold={int(with_gold)}", @@ -685,6 +688,15 @@ def probe_account( "opened": account.elite_opened, "error": "", }, + "esports": { + "status": CAPABILITY_UNKNOWN, + "raw": {}, + "manual_type": None, + "manual_score": None, + "expire_time": None, + "opened": account.esports_opened, + "error": "", + }, "eligibility": { "points": account.points, "points_response": {}, @@ -742,6 +754,17 @@ def probe_account( except Exception as exc: # noqa: BLE001 - 单项能力边界 errors["elite"] = f"{type(exc).__name__}: {_text(exc)}" + esports: dict[str, Any] = {} + esports_manual_id = str(config_info.get("esports_manual_id") or "").strip() + if esports_manual_id and with_esports: + try: + esports = query_esports_user_info( + active_client, + manual_id=esports_manual_id, + ) + except Exception as exc: # noqa: BLE001 - 单项能力边界 + errors["esports"] = f"{type(exc).__name__}: {_text(exc)}" + roles: dict[str, dict[str, Any]] = {} if with_elite_role: roles["elite"] = resolve_elite_role(active_client, config_info) @@ -764,7 +787,7 @@ def probe_account( points_response = points_result except Exception as exc: # noqa: BLE001 errors["points"] = f"{type(exc).__name__}: {_text(exc)}" - if with_eligibility or with_limited_goods: + if with_storehome and (with_eligibility or with_limited_goods): try: limited_result = query_limited_goods_result( active_client, @@ -849,6 +872,12 @@ def probe_account( manual_score = _to_int(elite.get("manual_score")) expire_time = normalize_elite_expire_time(elite.get("expire_time")) elite_opened = elite_is_currently_open(manual_type, expire_time, now=checked_at) + esports_manual_type = _to_int(esports.get("manual_type")) + esports_manual_score = _to_int(esports.get("manual_score")) + esports_expire_time = normalize_elite_expire_time(esports.get("expire_time")) + esports_opened = bool( + esports_manual_type is not None and esports_manual_type >= 1 + ) if points is None: points = manual_score identity = _identity_result( @@ -876,6 +905,19 @@ def probe_account( "opened": elite_opened, "error": errors.get("elite", ""), }, + "esports": { + "status": CAPABILITY_OK + if esports + else CAPABILITY_ERROR + if "esports" in errors + else CAPABILITY_UNKNOWN, + "raw": esports, + "manual_type": esports_manual_type, + "manual_score": esports_manual_score, + "expire_time": esports_expire_time, + "opened": esports_opened, + "error": errors.get("esports", ""), + }, "eligibility": { "manual_id": manual_id, "rid": str(config_info.get("rid") or ""), @@ -966,6 +1008,7 @@ def persist_probe_result( source: str, record_event: bool = True, commit: bool = False, + update_session: bool = True, ) -> None: """把一次探针结果写回账号和生命周期,是账号能力的唯一落库入口。""" now = _now() @@ -977,7 +1020,11 @@ def persist_probe_result( session = probe.get("session") or {} session_status = str(session.get("status") or "") - if session_status in {SESSION_VALID, SESSION_INVALID, SESSION_ERROR}: + if update_session and session_status in { + SESSION_VALID, + SESSION_INVALID, + SESSION_ERROR, + }: detail = ( session.get("detail") if isinstance(session.get("detail"), dict) else {} ) @@ -1022,10 +1069,26 @@ def persist_probe_result( raw_payload=dict(elite), ) + esports = probe.get("esports") or {} + if esports.get("status") == CAPABILITY_OK: + account.esports_opened = bool(esports.get("opened")) + account.esports_points = _to_int(esports.get("manual_score")) + account.esports_expire_time = _to_int(esports.get("expire_time")) + record_sync_success( + db, + account_id=int(account.id), + domain=DOMAIN_ESPORTS, + source=source, + observed_at=now, + raw_payload=dict(esports), + ) + eligibility = probe.get("eligibility") or {} if _to_int(eligibility.get("points")) is not None: account.points = _to_int(eligibility.get("points")) - if isinstance(eligibility.get("points_response"), dict): + if isinstance(eligibility.get("points_response"), dict) and eligibility[ + "points_response" + ]: record_sync_success( db, account_id=int(account.id), diff --git a/web/backend/services/douyu_activity_query.py b/web/backend/services/douyu_activity_query.py index df89cc5..275797a 100644 --- a/web/backend/services/douyu_activity_query.py +++ b/web/backend/services/douyu_activity_query.py @@ -46,6 +46,14 @@ def query_elite_user_info( return client.elite_user_info(manual_id=manual_id, rid=rid) +def query_esports_user_info( + client: DouyuActivityClient, + *, + manual_id: str, +) -> dict[str, Any]: + return client.esports_user_info(manual_id=manual_id) + + def query_bind_info( client: DouyuActivityClient, *, @@ -98,6 +106,14 @@ def query_gold_account(client: DouyuActivityClient) -> dict[str, Any]: return client.gold_account() +def query_gold_qr_state( + client: DouyuActivityClient, + *, + token: str, +) -> dict[str, Any]: + return client.query_gold_qr_state(token) + + def query_exchange_balance(client: DouyuActivityClient) -> dict[str, Any]: return client.exchange_balance() @@ -157,6 +173,65 @@ def query_points( return client.query_points(uid=uid, ctn=ctn) +def query_points_expirations( + client: DouyuActivityClient, + *, + user_id: str, + current: int = 0, + page_size: int = 20, +) -> dict[str, Any]: + return client.query_points_expirations( + user_id=user_id, + current=current, + page_size=page_size, + ) + + +def query_points_ledger( + client: DouyuActivityClient, + *, + uid: str, + page_num: int = 1, + page_size: int = 15, +) -> dict[str, Any]: + return client.query_points_ledger( + uid=uid, + page_num=page_num, + page_size=page_size, + ) + + +def extract_points_expire_time(payload: Any) -> int | None: + """从各层积分响应中提取 willExpireTime(2026-11-21 统一口径)。""" + if not isinstance(payload, dict): + return None + queue: list[dict[str, Any]] = [payload] + seen: set[int] = set() + while queue: + current = queue.pop(0) + marker = id(current) + if marker in seen: + continue + seen.add(marker) + for key in ("willExpireTime", "will_expire_time"): + raw = current.get(key) + if raw in (None, "", 0, "0"): + continue + try: + value = int(float(raw)) + except (TypeError, ValueError, OverflowError): + continue + if value > 10_000_000_000: + value //= 1000 + if value > 0: + return value + for key in ("raw", "data", "points_query", "result"): + nested = current.get(key) + if isinstance(nested, dict): + queue.append(nested) + return None + + def query_limited_goods_result( client: DouyuActivityClient, *, @@ -244,6 +319,7 @@ def query_exchange_orders( __all__ = [ "activity_client", + "extract_points_expire_time", "query_activity_goods", "query_activity_goods_result", "query_bind_info", @@ -252,15 +328,19 @@ __all__ = [ "query_esports_bind_info", "query_esports_goods", "query_esports_goods_result", + "query_esports_user_info", "query_exchange_balance", "query_exchange_orders", "query_exchange_orders_result", "query_exchange_records", "query_exchange_records_result", "query_gold_account", + "query_gold_qr_state", "query_limited_goods", "query_limited_goods_result", "query_points", + "query_points_expirations", + "query_points_ledger", "query_xpd_bind_info", "query_xpd_embed", "query_xpd_role", diff --git a/web/backend/services/douyu_runner_donate.py b/web/backend/services/douyu_runner_donate.py index 64e91a6..00f0029 100644 --- a/web/backend/services/douyu_runner_donate.py +++ b/web/backend/services/douyu_runner_donate.py @@ -11,7 +11,7 @@ from core.douyu import DouyuActivityClient from ..models import Account, DouyuTask from .business_events import record_gold_spent, record_points_credit -from .douyu_activity_query import query_csrf +from .douyu_activity_query import extract_points_expire_time, query_csrf DOUYU_GIFT_POINTS_REFRESH_TIMES = 3 DOUYU_GIFT_POINTS_REFRESH_INTERVAL = 2 @@ -366,12 +366,8 @@ def _points_expire_at(result: dict) -> object: 旧收集器走的是 `result["points_query"]["raw"]["data"]["willExpireTime"]` 这条 「猜出来」的路径;这里收敛成一个具名函数,只有它知道那段结构。 """ - query = result.get("points_query") - if not isinstance(query, dict): - return None - raw = query.get("raw") - data = raw.get("data") if isinstance(raw, dict) else None - return data.get("willExpireTime") if isinstance(data, dict) else None + # 2026-11-21:统一兼容顶层 will_expire_time 与旧的 points_query.raw.data 结构。 + return extract_points_expire_time(result.get("points_query")) def _esports_gift_points(result: dict, baseline_points: int | None) -> int | None: diff --git a/web/backend/services/douyu_runner_gold.py b/web/backend/services/douyu_runner_gold.py index 83ba36a..3b20f0d 100644 --- a/web/backend/services/douyu_runner_gold.py +++ b/web/backend/services/douyu_runner_gold.py @@ -19,7 +19,11 @@ from core.douyu import ( from ..models import Account, DouyuTask from .account_sync_service import DOMAIN_GOLD, record_sync_success from .business_events import record_recharge -from .douyu_activity_query import query_exchange_balance, query_gold_account +from .douyu_activity_query import ( + query_exchange_balance, + query_gold_account, + query_gold_qr_state, +) from .douyu_service import update_account_profile_from_cookie @@ -45,16 +49,70 @@ class GoldMixin: ) -> dict: """刷新鱼翅和钱包兑换余额并写回账号表。""" gold = query_gold_account(client) - exchange = query_exchange_balance(client) account.gold_balance = self._to_int(gold.get("gold")) - account.exchange_balance = self._to_int(exchange.get("count")) + # 2026-11-21:鱼翅余额是充值确认的关键结果。钱包兑换余额属于旁路资产, + # 单项失败不能反过来把已经成功拉到的鱼翅余额判为刷新失败。 + try: + exchange = query_exchange_balance(client) + account.exchange_balance = self._to_int(exchange.get("count")) + exchange_error = "" + except Exception as exc: # noqa: BLE001 兑换余额不能阻断鱼翅确认 + exchange = {} + exchange_error = str(exc) account.updated_at = datetime.now(UTC) - return { + result = { "gold_balance": account.gold_balance, "exchange_balance": account.exchange_balance, "gold": gold, "exchange_balance_query": exchange, } + if exchange_error: + result["exchange_balance_error"] = exchange_error + return result + + def _finish_wallet_gold_recharge( + self, + db: Session, + task: DouyuTask, + account: Account, + result: dict, + *, + amount: int, + channel: str, + order_id: str = "", + balance_refreshed: bool, + confirm_source: str, + ) -> None: + """2026-11-21:新版支付状态确认后统一刷新余额并写充值事实。""" + result.pop("payment_poll_error", None) + result["gold_recharged"] = True + result["payment_polling"] = False + result["gold_refreshed_after_success"] = balance_refreshed + result["gold_confirm_source"] = confirm_source + if order_id: + result["order_id"] = order_id + account.updated_at = datetime.now(UTC) + record_recharge( + db, + account, + task_id=task.id, + amount=max(1, int(amount)), + order_id=order_id, + channel=channel, + balance_after=account.gold_balance, + confirmed=True, + actor_id=int(task.created_by), + ) + order_text = f",订单 {order_id}" if order_id else "" + self._mark_task( + db, + task, + "success", + "鱼翅已到账" + f"{order_text},当前余额: " + f"{account.gold_balance if account.gold_balance is not None else '-'}", + result, + ) def _poll_create_gold_qr_once( self, @@ -68,12 +126,85 @@ class GoldMixin: result = dict(task.result or {}) result["payment_polling"] = True result["payment_poll_count"] = int(result.get("payment_poll_count") or 0) + 1 + amount = int( + result.get("payment_amount") + or self._task_payload(task).get("amount") + or self._task_payload(task).get("gold_amount") + or 1 + ) + channel = str( + result.get("payment_channel") + or config.get("gold_recharge_channel") + or "wechat_qr" + ) try: client = self._client(cookie, direct=True) - balance_result = self._refresh_account_gold_balance(client, account) - db.commit() - result.update(balance_result) - last_gold = balance_result["gold_balance"] + token = str(result.get("payment_token") or result.get("token") or "") + qr_state: int | None = None + qr_state_error = "" + if token: + try: + state_result = query_gold_qr_state(client, token=token) + if not isinstance(state_result, dict): + raise TypeError("鱼翅支付状态响应格式异常") + qr_state = self._to_int(state_result.get("state")) + result["gold_qr_state"] = qr_state + if state_result.get("quantity") is not None: + result["gold_qr_quantity"] = state_result.get("quantity") + quantity = self._to_int(state_result.get("quantity")) + if quantity is not None and quantity > 0: + amount = quantity + if state_result.get("order_id"): + result["order_id"] = str(state_result["order_id"]) + except Exception as exc: # noqa: BLE001 新接口失败时兼容余额轮询 + qr_state_error = str(exc) + result["gold_qr_state_error"] = qr_state_error + + balance_result: dict[str, Any] = {} + balance_error = "" + try: + balance_result = self._refresh_account_gold_balance(client, account) + db.commit() + result.update(balance_result) + except Exception as exc: # noqa: BLE001 官方状态成功后仍应完成充值 + balance_error = str(exc) + result["payment_poll_error"] = balance_error + last_gold = balance_result.get("gold_balance") + + if qr_state == 2: + self._finish_wallet_gold_recharge( + db, + task, + account, + result, + amount=amount, + channel=channel, + order_id=str(result.get("order_id") or ""), + balance_refreshed=not balance_error, + confirm_source="gold_qr_state", + ) + return + if qr_state is not None: + state_text = {-2: "等待支付", 0: "支付处理中"}.get( + qr_state, f"状态 {qr_state}" + ) + self._schedule_payment_poll( + db, + task, + result, + f"鱼翅充值{state_text}" + f"(当前鱼翅 {last_gold if last_gold is not None else '-'})", + ) + return + if balance_error: + self._schedule_payment_poll( + db, + task, + result, + f"等待鱼翅到账: {balance_error}", + ) + return + baseline_gold = self._to_int(result.get("baseline_gold_balance")) if baseline_gold is None: result["baseline_gold_balance"] = last_gold @@ -86,37 +217,16 @@ class GoldMixin: return # 余额必须增加才算到账,下降可能来自同账号送礼等消费。 if last_gold is not None and last_gold > baseline_gold: - result.pop("payment_poll_error", None) - result["gold_recharged"] = True - result["payment_polling"] = False - amount = int( - result.get("payment_amount") - or self._task_payload(task).get("amount") - or self._task_payload(task).get("gold_amount") - or 1 - ) - channel = str( - result.get("payment_channel") - or config.get("gold_recharge_channel") - or "wechat_qr" - ) - account.updated_at = datetime.now(UTC) - record_recharge( - db, - account, - task_id=task.id, - amount=amount, - channel=channel, - balance_after=account.gold_balance, - confirmed=True, - actor_id=int(task.created_by), - ) - self._mark_task( + self._finish_wallet_gold_recharge( db, task, - "success", - f"鱼翅已到账,当前余额: {account.gold_balance if account.gold_balance is not None else '-'}", + account, result, + amount=amount, + channel=channel, + order_id=str(result.get("order_id") or ""), + balance_refreshed=True, + confirm_source="balance_increase", ) return result.pop("payment_poll_error", None) @@ -443,6 +553,7 @@ class GoldMixin: { "payment_amount": amount, "payment_channel": channel, + "payment_token": str(result.get("token") or ""), "baseline_gold_balance": baseline_gold, } ) diff --git a/web/backend/services/douyu_runner_goods.py b/web/backend/services/douyu_runner_goods.py index 0aaac20..54388bb 100644 --- a/web/backend/services/douyu_runner_goods.py +++ b/web/backend/services/douyu_runner_goods.py @@ -12,13 +12,8 @@ from sqlalchemy.orm import Session from core.douyu import DouyuActivityClient, DouyuActivityError from ..models import Account, DouyuEsportsGoodsSnapshot, DouyuGoodsSnapshot, DouyuTask -from .account_sync_service import ( - DOMAIN_EXCHANGE_RECORDS, - finish_sync_failure, - finish_sync_success, - start_sync_run, - sync_storehome_snapshot, -) +from .account_exchange_sync_service import sync_exchange_records +from .account_sync_service import sync_storehome_snapshot from .business_events import record_exchange from .douyu_activity_query import ( query_activity_goods, @@ -26,12 +21,10 @@ from .douyu_activity_query import ( query_csrf, query_elite_user_info, query_esports_goods_result, - query_exchange_records_result, query_limited_goods_result, ) from .douyu_goods_service import upsert_goods_snapshot from .douyu_read_gateway import storehome_snapshot_from_result -from .douyu_runner_overview import record_official_exchange_records from .high_value_service import ( DEFAULT_HIGH_VALUE_GOODS, HIGH_VALUE_STATUS_OWNED, @@ -793,58 +786,21 @@ class GoodsMixin: self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict ): client = self._client(cookie) - result = query_exchange_records_result( - client, - manual_id=str(config["manual_id"]), - limit=100, - offset=0, - ) - records = result.get("records") or [] - observed_at = datetime.now(UTC) - sync_run = start_sync_run( + result = sync_exchange_records( db, - account_id=int(account.id), - domain=DOMAIN_EXCHANGE_RECORDS, + account, + client, + config, source="douyu_task.query_exchange_records", task_id=task.id, ) - try: - added = record_official_exchange_records( - db, - account, - [("elite", record) for record in records if isinstance(record, dict)], - ) - finish_sync_success( - db, - sync_run, - item_count=len(records), - changed_count=added, - observed_at=observed_at, - raw_payload=( - result.get("raw") - if isinstance(result.get("raw"), dict) - else None - ), - ) - except Exception as exc: - finish_sync_failure( - db, - sync_run, - error=exc, - raw_payload=( - result.get("raw") - if isinstance(result.get("raw"), dict) - else None - ), - ) - raise - result["exchange_events_added"] = added account.updated_at = datetime.now(UTC) + item_count = int(result.get("item_count") or 0) self._mark_task( db, task, "success", - f"兑换记录 {len(records)} 条" if records else "暂无兑换记录", + f"兑换记录 {item_count} 条" if item_count else "暂无兑换记录", result, ) diff --git a/web/backend/services/douyu_runner_overview.py b/web/backend/services/douyu_runner_overview.py index 83b3770..3adb54e 100644 --- a/web/backend/services/douyu_runner_overview.py +++ b/web/backend/services/douyu_runner_overview.py @@ -24,7 +24,8 @@ from __future__ import annotations import json -from datetime import UTC, datetime +import os +from datetime import UTC, datetime, timedelta from typing import TYPE_CHECKING, Any from loguru import logger @@ -32,17 +33,31 @@ from sqlalchemy.orm import Session from core.douyu import DouyuActivityClient -from ..models import Account, DouyuTask +from ..models import Account, AccountCommodityState, AccountSyncState, DouyuTask +from .account_exchange_sync_service import persist_official_exchange_records +from .account_sync_runner import ( + WORK_ELITE_SYNC, + WORK_ESPORTS_SYNC, + WORK_EXCHANGE_RECORDS_SYNC, + WORK_GOLD_SYNC, + WORK_POINTS_SYNC, + WORK_ROLE_SYNC, + WORK_STOREHOME_SYNC, +) from .account_sync_service import ( DOMAIN_ELITE, DOMAIN_ESPORTS, DOMAIN_EXCHANGE_RECORDS, DOMAIN_GOLD, DOMAIN_POINTS, + DOMAIN_ROLE, + DOMAIN_STOREHOME, + get_sync_state, + is_domain_fresh, record_sync_success, sync_storehome_snapshot, ) -from .business_events import record_official_exchange +from .account_work_queue import enqueue_account_work from .douyu_account_probe import ( ROLE_BOUND, ROLE_ERROR, @@ -74,6 +89,7 @@ from .high_value_service import ( #: 官方兑换记录单次抓取上限。能力匹配需要更完整的一年记录,详情页只展示前 20 条。 EXCHANGE_RECORD_FETCH_LIMIT = 100 EXCHANGE_RECORD_DISPLAY_LIMIT = 20 +DEFAULT_OVERVIEW_CACHE_SECONDS = 10 * 60 #: 单项错误的摘要长度(避免把整个 traceback 塞进 task.result) _ERROR_MAX = 200 @@ -83,6 +99,21 @@ def _err(exc: BaseException) -> str: return f"{type(exc).__name__}: {exc}"[:_ERROR_MAX] +def _positive_env_int(name: str, default: int) -> int: + try: + value = int(os.getenv(name, str(default))) + except ValueError: + return default + return value if value > 0 else default + + +def _overview_cache_seconds() -> int: + return _positive_env_int( + "DOUYU_OVERVIEW_CACHE_SECONDS", + DEFAULT_OVERVIEW_CACHE_SECONDS, + ) + + def _group_by_scope( pairs: list[tuple[str, dict[str, Any]]], ) -> dict[str, list[dict[str, Any]]]: @@ -93,40 +124,13 @@ def _group_by_scope( return grouped -def record_official_exchange_records( - db: Session, account: Account, pairs: list[tuple[str, dict[str, Any]]] -) -> int: - """把官方兑换记录补成账号事件,返回**新写入**的条数。 - - 幂等由 `record_official_exchange` 的 `source_ref` 保证(含作用域), - 所以一键查询反复执行不会重复落事件。 - """ - added = 0 - for scope, rec in pairs: - if not isinstance(rec, dict): - continue - when = _ts(rec.get("time")) - occurred = _parse_expire_at(when) if when is not None else None - score_raw = rec.get("score") - try: - score = int(str(score_raw)) if score_raw not in (None, "") else None - except (TypeError, ValueError): - score = None - if record_official_exchange( - db, - account, - commodity_id=str(rec.get("commodityId") or rec.get("commodity_id") or ""), - commodity_name=str( - rec.get("commodityName") or rec.get("commodity_name") or "" - ), - score=score, - occurred_at=occurred, - show_name=str(rec.get("showName") or rec.get("show_name") or ""), - exchange_num=rec.get("exchangeNum") or rec.get("count"), - scope=scope, - ): - added += 1 - return added +def _overview_owned_text(snapshot: dict[str, Any]) -> str: + owned = [ + str(item.get("name") or "") + for item in (snapshot.get("high_value_goods") or []) + if isinstance(item, dict) and not item.get("missing") and item.get("owned") + ] + return f": {', '.join(owned)}" if owned else "" def _goods_config(config: dict) -> list[str]: @@ -173,6 +177,7 @@ class OverviewMixin: def _client(self, cookie: str) -> DouyuActivityClient: ... def _mark_task(self, *args: Any, **kwargs: Any) -> None: ... def _push_log(self, level: str, message: str) -> None: ... + def _task_payload(self, task: DouyuTask) -> dict: ... def _refresh_account_gold_balance( self, client: DouyuActivityClient, account: Account ) -> dict: ... @@ -274,6 +279,165 @@ class OverviewMixin: or [] ) + @staticmethod + def _load_storehome_items( + db: Session, + account: Account, + *, + fresh_only: bool, + now: datetime, + ) -> list[dict[str, Any]]: + query = db.query(AccountCommodityState).filter( + AccountCommodityState.account_id == account.id, + AccountCommodityState.platform == "douyu", + AccountCommodityState.scope == "elite", + AccountCommodityState.observed_at.isnot(None), + AccountCommodityState.raw_json.isnot(None), + ) + if fresh_only: + query = query.filter( + AccountCommodityState.valid_until.isnot(None), + AccountCommodityState.valid_until > now, + ) + return [ + dict(row.raw_json) + for row in query.order_by(AccountCommodityState.commodity_id.asc()).all() + if isinstance(row.raw_json, dict) + ] + + @staticmethod + def _sync_mode( + db: Session, + account: Account, + domain: str, + *, + now: datetime, + ) -> str: + if is_domain_fresh( + db, + account_id=int(account.id), + domain=domain, + now=now, + ): + return "fresh" + return ( + "stale" + if get_sync_state(db, account_id=int(account.id), domain=domain) + else "missing" + ) + + @staticmethod + def _queue_domain_sync( + db: Session, + *, + account: Account, + domain: str, + now: datetime, + ) -> str | None: + work_type = { + DOMAIN_POINTS: WORK_POINTS_SYNC, + DOMAIN_GOLD: WORK_GOLD_SYNC, + DOMAIN_ELITE: WORK_ELITE_SYNC, + DOMAIN_ESPORTS: WORK_ESPORTS_SYNC, + DOMAIN_ROLE: WORK_ROLE_SYNC, + DOMAIN_EXCHANGE_RECORDS: WORK_EXCHANGE_RECORDS_SYNC, + }.get(domain) + if work_type is None: + return None + enqueue_account_work( + db, + account_id=int(account.id), + work_type=work_type, + priority=25, + due_at=now, + reason=f"一键查询发现 {domain} 查询域过期", + ) + return work_type + + @staticmethod + def _local_bind_overview(account: Account) -> dict[str, dict[str, Any]]: + rows = { + "elite": ( + account.role_bind_status, + account.game_name, + account.game_channel, + ), + "esports": ( + account.esports_bind_status, + account.esports_game_name, + account.esports_game_channel, + ), + "peace": ( + account.xpd_bind_status, + account.xpd_game_name, + "", + ), + } + return { + scope: { + "status": status or "unknown", + "role_name": role_name or "", + "game_channel": channel or "", + "source_alias": "local_projection", + } + for scope, (status, role_name, channel) in rows.items() + } + + @staticmethod + def _load_exchange_projection( + db: Session, + account: Account, + *, + fallback: dict[str, Any], + ) -> dict[str, list[dict[str, Any]]]: + state = ( + db.query(AccountSyncState) + .filter( + AccountSyncState.account_id == int(account.id), + AccountSyncState.platform == "douyu", + AccountSyncState.domain == DOMAIN_EXCHANGE_RECORDS, + ) + .first() + ) + raw = state.raw_payload_json if state is not None else None + source = raw if isinstance(raw, dict) else fallback + return { + str(scope): [dict(row) for row in rows if isinstance(row, dict)] + for scope, rows in source.items() + if isinstance(rows, list) + } + + def _cached_overview( + self, + account: Account, + task: DouyuTask, + *, + now: datetime, + ) -> dict[str, Any] | None: + snapshot = account.overview_snapshot + if not isinstance(snapshot, dict) or account.overview_queried_at is None: + return None + payload = ( + self._task_payload(task) + if hasattr(self, "_task_payload") + else {} + ) + if bool(payload.get("force_refresh")): + return None + age = (now - account.overview_queried_at).total_seconds() + ttl = _overview_cache_seconds() + # MySQL DATETIME 只有秒精度,写入时会四舍五入;同一秒内重试可能 + # 读到比应用时钟未来不到 1 秒的时间。给它一个小容差,避免新鲜快照 + # 被误判为“未来数据”而重复请求官网。 + if age < -5 or age > ttl: + return None + cached = dict(snapshot) + cached["cache_hit"] = True + cached["cache_source"] = "account.overview_snapshot" + cached["fresh_until"] = account.overview_queried_at + timedelta(seconds=ttl) + cached["fresh_until"] = cached["fresh_until"].isoformat() + return cached + def _execute_query_account_overview( self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict ): @@ -282,6 +446,27 @@ class OverviewMixin: 每一项独立 try/except —— 单项失败只记进 `errors`, 绝不因此让整次查询失败(账号没绑电竞手册就是最常见的例子)。 """ + started_at = datetime.now(UTC) + cached = self._cached_overview(account, task, now=started_at) + if cached is not None: + owned_count = sum( + 1 + for item in (cached.get("high_value_goods") or []) + if isinstance(item, dict) + and not item.get("missing") + and item.get("owned") + ) + message = ( + "一键查询命中本地新鲜快照;" + f"高价值套装已拥有 {owned_count} 个" + f"{_overview_owned_text(cached)}" + ) + self._push_log( + "info", + f"账号 {account.id} 一键查询命中本地快照,跳过官网请求", + ) + self._mark_task(db, task, "success", message, cached) + return client = self._client(cookie) manual_id = str(config.get("manual_id") or "").strip() esports_manual_id = str(config.get("esports_manual_id") or "").strip() @@ -294,6 +479,9 @@ class OverviewMixin: "elite": {}, "esports": {}, "high_value_goods": [], + "pending_domains": [], + "stale_domains": [], + "domain_sources": {}, "exchange_records": {}, "errors": {}, } @@ -302,110 +490,208 @@ class OverviewMixin: all_records: list[tuple[str, dict[str, Any]]] = [] pending_goods: list[dict[str, Any]] = [] goods_query_ok = False + local_goods_fresh = False storehome_snapshot: StorehomeSnapshot | None = None + previous_overview = ( + account.overview_snapshot + if isinstance(account.overview_snapshot, dict) + else {} + ) # ---- 鱼翅 + 钱包兑换余额 ---- - try: - gold = self._refresh_account_gold_balance(client, account) - overview["assets"]["gold_balance"] = gold.get("gold_balance") - overview["assets"]["exchange_balance"] = gold.get("exchange_balance") - record_sync_success( - db, - account_id=int(account.id), - domain=DOMAIN_GOLD, - source="douyu_task.query_account_overview", - observed_at=queried_at, - raw_payload=gold, - task_id=task.id, + gold_mode = self._sync_mode(db, account, DOMAIN_GOLD, now=queried_at) + if gold_mode in {"fresh", "stale"}: + overview["assets"]["gold_balance"] = account.gold_balance + overview["assets"]["exchange_balance"] = account.exchange_balance + overview["domain_sources"][DOMAIN_GOLD] = ( + "local_fresh" if gold_mode == "fresh" else "queued_stale" ) - except Exception as exc: # noqa: BLE001 外部接口边界需要宽泛兜底 - errors["gold"] = _err(exc) + if gold_mode == "stale": + overview["stale_domains"].append(DOMAIN_GOLD) + overview["pending_domains"].append(DOMAIN_GOLD) + self._queue_domain_sync( + db, + account=account, + domain=DOMAIN_GOLD, + now=queried_at, + ) + else: + try: + gold = self._refresh_account_gold_balance(client, account) + overview["assets"]["gold_balance"] = gold.get("gold_balance") + overview["assets"]["exchange_balance"] = gold.get("exchange_balance") + overview["domain_sources"][DOMAIN_GOLD] = "official_bootstrap" + record_sync_success( + db, + account_id=int(account.id), + domain=DOMAIN_GOLD, + source="douyu_task.query_account_overview", + observed_at=queried_at, + raw_payload=gold, + task_id=task.id, + ) + except Exception as exc: # noqa: BLE001 外部接口边界需要宽泛兜底 + errors["gold"] = _err(exc) # ---- 积分 + 到期时间 ---- - try: - ctn = query_csrf(client) - points = self._refresh_account_points(client, account, cookie, ctn=ctn) - overview["assets"]["points"] = points.get("points") - # `_refresh_account_points` 只回 {"points", "points_query"}, - # 到期时间得从 points_query 里取 —— 复用已有具名函数,不自己扒结构。 - expire_raw = _ts(_points_expire_at(points)) - overview["assets"]["points_expire_at"] = expire_raw - # **也写回账号**:积分到期时间是账号的信息,本来就该由账号表承载并 - # 在账号详情里展示 —— 查询只是一次「刷新」,不是另开一份副本。 - expire_dt = _parse_expire_at(expire_raw) - account.points_expire_at = expire_dt - record_sync_success( - db, - account_id=int(account.id), - domain=DOMAIN_POINTS, - source="douyu_task.query_account_overview", - observed_at=queried_at, - raw_payload=points.get("points_query") - if isinstance(points.get("points_query"), dict) - else points, - task_id=task.id, + points_mode = self._sync_mode(db, account, DOMAIN_POINTS, now=queried_at) + if points_mode in {"fresh", "stale"}: + overview["assets"]["points"] = account.points + previous_assets = ( + previous_overview.get("assets") + if isinstance(previous_overview.get("assets"), dict) + else {} ) - except Exception as exc: # noqa: BLE001 - errors["points"] = _err(exc) + overview["assets"]["points_expire_at"] = previous_assets.get( + "points_expire_at" + ) or ( + account.points_expire_at.isoformat() + if account.points_expire_at + else None + ) + overview["domain_sources"][DOMAIN_POINTS] = ( + "local_fresh" if points_mode == "fresh" else "queued_stale" + ) + if points_mode == "stale": + overview["stale_domains"].append(DOMAIN_POINTS) + overview["pending_domains"].append(DOMAIN_POINTS) + self._queue_domain_sync( + db, + account=account, + domain=DOMAIN_POINTS, + now=queried_at, + ) + else: + try: + ctn = query_csrf(client) + points = self._refresh_account_points(client, account, cookie, ctn=ctn) + overview["assets"]["points"] = points.get("points") + expire_raw = _ts(_points_expire_at(points)) + if expire_raw is not None: + # 2026-11-21:新客户端归一成整数时间戳;对外保持原有字符串契约。 + expire_raw = str(expire_raw) + overview["assets"]["points_expire_at"] = expire_raw + expire_dt = _parse_expire_at(expire_raw) + account.points_expire_at = expire_dt + overview["domain_sources"][DOMAIN_POINTS] = "official_bootstrap" + record_sync_success( + db, + account_id=int(account.id), + domain=DOMAIN_POINTS, + source="douyu_task.query_account_overview", + observed_at=queried_at, + raw_payload=points.get("points_query") + if isinstance(points.get("points_query"), dict) + else points, + task_id=task.id, + ) + except Exception as exc: # noqa: BLE001 + errors["points"] = _err(exc) # ---- 精英宝典(开通状态 / 到期时间 / 积分)---- if not manual_id: errors["elite"] = "未配置精英宝典 manualID" else: - try: - elite = self._refresh_elite_handbook( - client, account, manual_id=manual_id, rid=rid - ) + elite_mode = self._sync_mode(db, account, DOMAIN_ELITE, now=queried_at) + if elite_mode in {"fresh", "stale"}: overview["elite"] = { - "opened": elite.get("elite_opened"), - "expire_time": _ts(elite.get("elite_expire_time")), - "points": elite.get("elite_manual_score"), + "opened": account.elite_opened, + "expire_time": account.elite_expire_time, + "points": account.points, } - record_sync_success( - db, - account_id=int(account.id), - domain=DOMAIN_ELITE, - source="douyu_task.query_account_overview", - observed_at=queried_at, - raw_payload=elite, - task_id=task.id, + overview["domain_sources"][DOMAIN_ELITE] = ( + "local_fresh" if elite_mode == "fresh" else "queued_stale" ) - except Exception as exc: # noqa: BLE001 - errors["elite"] = _err(exc) + if elite_mode == "stale": + overview["stale_domains"].append(DOMAIN_ELITE) + overview["pending_domains"].append(DOMAIN_ELITE) + self._queue_domain_sync( + db, + account=account, + domain=DOMAIN_ELITE, + now=queried_at, + ) + else: + try: + elite = self._refresh_elite_handbook( + client, account, manual_id=manual_id, rid=rid + ) + overview["elite"] = { + "opened": elite.get("elite_opened"), + "expire_time": _ts(elite.get("elite_expire_time")), + "points": elite.get("elite_manual_score"), + } + overview["domain_sources"][DOMAIN_ELITE] = "official_bootstrap" + record_sync_success( + db, + account_id=int(account.id), + domain=DOMAIN_ELITE, + source="douyu_task.query_account_overview", + observed_at=queried_at, + raw_payload=elite, + task_id=task.id, + ) + except Exception as exc: # noqa: BLE001 + errors["elite"] = _err(exc) # ---- 电竞手册 ---- if not esports_manual_id: errors["esports"] = "未配置电竞手册 manualID" else: - try: - esports = self._refresh_esports_handbook( - client, account, manual_id=esports_manual_id - ) - manual_type = esports.get("esports_manual_type") - # `_refresh_esports_handbook` 不直接给「是否开通」; - # 语义以 create_esports_qr 里的判定为准:manual_type >= 1 = 已开通。 - opened = ( - manual_type is not None and int(manual_type) >= 1 - if isinstance(manual_type, (int, float)) - else None - ) + esports_mode = self._sync_mode( + db, + account, + DOMAIN_ESPORTS, + now=queried_at, + ) + if esports_mode in {"fresh", "stale"}: overview["esports"] = { - "opened": opened, - "manual_type": manual_type, - "points": esports.get("esports_points"), - "expire_time": _ts(esports.get("esports_expire_time")), + "opened": account.esports_opened, + "manual_type": 1 if account.esports_opened else 0, + "points": account.esports_points, + "expire_time": account.esports_expire_time, } - record_sync_success( - db, - account_id=int(account.id), - domain=DOMAIN_ESPORTS, - source="douyu_task.query_account_overview", - observed_at=queried_at, - raw_payload=esports, - task_id=task.id, + overview["domain_sources"][DOMAIN_ESPORTS] = ( + "local_fresh" if esports_mode == "fresh" else "queued_stale" ) - except Exception as exc: # noqa: BLE001 - errors["esports"] = _err(exc) + if esports_mode == "stale": + overview["stale_domains"].append(DOMAIN_ESPORTS) + overview["pending_domains"].append(DOMAIN_ESPORTS) + self._queue_domain_sync( + db, + account=account, + domain=DOMAIN_ESPORTS, + now=queried_at, + ) + else: + try: + esports = self._refresh_esports_handbook( + client, account, manual_id=esports_manual_id + ) + manual_type = esports.get("esports_manual_type") + opened = ( + manual_type is not None and int(manual_type) >= 1 + if isinstance(manual_type, (int, float)) + else None + ) + overview["esports"] = { + "opened": opened, + "manual_type": manual_type, + "points": esports.get("esports_points"), + "expire_time": _ts(esports.get("esports_expire_time")), + } + overview["domain_sources"][DOMAIN_ESPORTS] = "official_bootstrap" + record_sync_success( + db, + account_id=int(account.id), + domain=DOMAIN_ESPORTS, + source="douyu_task.query_account_overview", + observed_at=queried_at, + raw_payload=esports, + task_id=task.id, + ) + except Exception as exc: # noqa: BLE001 + errors["esports"] = _err(exc) # ---- 精英宝典商城商品列表(高价值套装的**唯一**判据来源)---- # @@ -418,24 +704,85 @@ class OverviewMixin: errors["limited_goods"] = "未配置精英宝典 manualID" else: goods_items: list[dict[str, Any]] = [] - try: - # 🎯 拉取完整商城商品列表(含每个商品准确的 status 状态:1=已达兑换上限,0=可兑换) - goods_result = query_activity_goods_result( - client, - manual_id=manual_id, - rid=rid, + storehome_state = get_sync_state( + db, + account_id=int(account.id), + domain=DOMAIN_STOREHOME, + ) + if is_domain_fresh( + db, + account_id=int(account.id), + domain=DOMAIN_STOREHOME, + now=queried_at, + ): + goods_items = self._load_storehome_items( + db, + account, + fresh_only=True, + now=queried_at, ) - storehome_snapshot = storehome_snapshot_from_result( - goods_result, - manual_id=manual_id, - rid=rid, - observed_at=queried_at, + if goods_items: + local_goods_fresh = True + overview["limited_goods_total"] = len(goods_items) + overview["goods_source"] = "local_fresh_projection" + else: + overview["pending_domains"].append(DOMAIN_STOREHOME) + errors["limited_goods"] = ( + "商品同步状态新鲜,但本地商品明细为空,已加入同步队列" + ) + enqueue_account_work( + db, + account_id=int(account.id), + work_type=WORK_STOREHOME_SYNC, + priority=20, + due_at=queried_at, + reason="一键查询发现商品投影为空", + ) + elif storehome_state is not None: + goods_items = self._load_storehome_items( + db, + account, + fresh_only=False, + now=queried_at, ) - goods_items = list(storehome_snapshot.items) - overview["limited_goods_total"] = len(goods_items) - goods_query_ok = True - except Exception as exc: # noqa: BLE001 - errors["limited_goods"] = _err(exc) + if goods_items: + overview["limited_goods_total"] = len(goods_items) + overview["goods_source"] = "local_stale_projection" + overview["high_value_goods"] = list( + previous_overview.get("high_value_goods") or [] + ) + overview["pending_domains"].append(DOMAIN_STOREHOME) + errors["limited_goods"] = ( + "本地商品快照已过期,已加入后台同步队列" + ) + enqueue_account_work( + db, + account_id=int(account.id), + work_type=WORK_STOREHOME_SYNC, + priority=20, + due_at=queried_at, + reason="一键查询发现商品快照过期", + ) + else: + try: + # 首次同步保留一次实时查询,避免新部署后一键查询直接返回空。 + goods_result = query_activity_goods_result( + client, + manual_id=manual_id, + rid=rid, + ) + storehome_snapshot = storehome_snapshot_from_result( + goods_result, + manual_id=manual_id, + rid=rid, + observed_at=queried_at, + ) + goods_items = list(storehome_snapshot.items) + overview["limited_goods_total"] = len(goods_items) + overview["goods_source"] = "official_bootstrap" + goods_query_ok = True + except Exception as exc: # noqa: BLE001 + errors["limited_goods"] = _err(exc) # 商品列表带有账号级 status;兑换记录只作历史展示,不参与准入判定。 pending_goods = goods_items @@ -456,119 +803,190 @@ class OverviewMixin: "和平小店已关闭(douyu_config.peace_enabled=False)" ) - overview["bind"] = {} - role_resolvers = { - "elite": resolve_elite_role, - "esports": resolve_esports_role, - "peace": resolve_peace_role, - } - for scope, label, alias_key in ( - ("elite", "精英宝典", "bind_act_alias"), - ("esports", "电竞手册", "esports_act_alias"), - ("peace", "和平小店", "xpd_act_alias"), - ): - if scope == "peace" and not peace_on: - continue - alias = str(config.get(alias_key) or "").strip() - if not alias: - errors[f"bind_{scope}"] = f"未配置{label}活动别名" - continue - role = role_resolvers[scope]( - client, - config, - bind_fetcher=lambda selected_alias, selected_scope=scope: ( - self._fetch_bind_info(client, selected_scope, selected_alias) - ), + role_mode = self._sync_mode(db, account, DOMAIN_ROLE, now=queried_at) + if role_mode in {"fresh", "stale"}: + overview["bind"] = self._local_bind_overview(account) + if not peace_on: + overview["bind"].pop("peace", None) + overview["domain_sources"][DOMAIN_ROLE] = ( + "local_fresh" if role_mode == "fresh" else "queued_stale" ) - status = str(role.get("status") or "") - snapshot = ( - role.get("snapshot") if isinstance(role.get("snapshot"), dict) else {} - ) - overview["bind"][scope] = { - "status": status, - "derived": snapshot.get("bind_status"), - "phase": snapshot.get("bind_phase"), - "role_name": snapshot.get("role_name") or "", - "area_name": snapshot.get("area_name") or "", - "plat_name": snapshot.get("plat_name") or "", - "nickname": snapshot.get("nickname") or "", - "game_id": snapshot.get("game_id") or "", - "game_name": snapshot.get("game_name") or "", - "source_alias": role.get("source_alias") or "", - "is_change_cooling": snapshot.get("is_change_cooling"), - "can_change_role": snapshot.get("can_change_role"), - "change_role_wait_time": _ts(snapshot.get("change_role_wait_time")), - "can_change_time": _ts(snapshot.get("can_change_time")), - } - if status in {ROLE_ERROR, ROLE_UNKNOWN} and role.get("errors"): - errors[f"bind_{scope}"] = ";".join(role.get("errors") or []) - if status == ROLE_ERROR: - continue - if status in {ROLE_BOUND, ROLE_PENDING, ROLE_UNBOUND}: - persist_probe_result( + if role_mode == "stale": + overview["stale_domains"].append(DOMAIN_ROLE) + overview["pending_domains"].append(DOMAIN_ROLE) + self._queue_domain_sync( db, - account, - { - "probe_id": f"task:{task.id}:role:{scope}", - "identity": {"uid": account.uid, "username": account.username}, - "session": {}, - "elite": {}, - "eligibility": {}, - "roles": {scope: role}, - "gold": {}, - "goods": {}, - "errors": {}, - }, - source=f"douyu_task.query_account_overview.{scope}", - record_event=False, + account=account, + domain=DOMAIN_ROLE, + now=queried_at, ) - # 后面还要查询其他作用域和官方兑换记录。先提交角色快照, - # 避免数据库事务跨过下一轮外部 HTTP 请求。 - db.commit() + else: + overview["bind"] = {} + role_resolvers = { + "elite": resolve_elite_role, + "esports": resolve_esports_role, + "peace": resolve_peace_role, + } + for scope, label, alias_key in ( + ("elite", "精英宝典", "bind_act_alias"), + ("esports", "电竞手册", "esports_act_alias"), + ("peace", "和平小店", "xpd_act_alias"), + ): + if scope == "peace" and not peace_on: + continue + alias = str(config.get(alias_key) or "").strip() + if not alias: + errors[f"bind_{scope}"] = f"未配置{label}活动别名" + continue + role = role_resolvers[scope]( + client, + config, + bind_fetcher=lambda selected_alias, selected_scope=scope: ( + self._fetch_bind_info(client, selected_scope, selected_alias) + ), + ) + status = str(role.get("status") or "") + snapshot = ( + role.get("snapshot") + if isinstance(role.get("snapshot"), dict) + else {} + ) + overview["bind"][scope] = { + "status": status, + "derived": snapshot.get("bind_status"), + "phase": snapshot.get("bind_phase"), + "role_name": snapshot.get("role_name") or "", + "area_name": snapshot.get("area_name") or "", + "plat_name": snapshot.get("plat_name") or "", + "nickname": snapshot.get("nickname") or "", + "game_id": snapshot.get("game_id") or "", + "game_name": snapshot.get("game_name") or "", + "source_alias": role.get("source_alias") or "", + "is_change_cooling": snapshot.get("is_change_cooling"), + "can_change_role": snapshot.get("can_change_role"), + "change_role_wait_time": _ts(snapshot.get("change_role_wait_time")), + "can_change_time": _ts(snapshot.get("can_change_time")), + } + if status in {ROLE_ERROR, ROLE_UNKNOWN} and role.get("errors"): + errors[f"bind_{scope}"] = ";".join(role.get("errors") or []) + if status == ROLE_ERROR: + continue + if status in {ROLE_BOUND, ROLE_PENDING, ROLE_UNBOUND}: + persist_probe_result( + db, + account, + { + "probe_id": f"task:{task.id}:role:{scope}", + "identity": { + "uid": account.uid, + "username": account.username, + }, + "session": {}, + "elite": {}, + "eligibility": {}, + "roles": {scope: role}, + "gold": {}, + "goods": {}, + "errors": {}, + }, + source=f"douyu_task.query_account_overview.{scope}", + record_event=False, + ) + db.commit() # ---- 官方兑换记录(**三个作用域都拉**)---- # # 精英宝典 / 电竞手册 用的是同一个接口,只是 manual_id 不同; # 和平小店是另一套(先拿道聚城短时授权 query,再查购买记录)。 # 任一作用域失败只记进 errors,不影响其余两个。 - for scope, label, fetcher in ( - ("elite", "精英宝典", self._fetch_elite_records), - ("esports", "电竞手册", self._fetch_esports_records), - ("peace", "和平小店", self._fetch_xpd_records), - ): - if scope == "peace" and not peace_on: - continue - key = f"exchange_records_{scope}" - if not self._scope_configured(scope, manual_id, esports_manual_id, config): - errors[key] = f"未配置{label}活动参数,跳过" - continue - try: - rows = fetcher(client, config, manual_id, esports_manual_id) - except Exception as exc: # noqa: BLE001 外部接口边界需要宽泛兜底 - errors[key] = _err(exc) - continue - overview["exchange_records"][scope] = rows[:EXCHANGE_RECORD_DISPLAY_LIMIT] - all_records.extend((scope, r) for r in rows) - overview[f"exchange_record_count_{scope}"] = len(rows) + exchange_mode = self._sync_mode( + db, + account, + DOMAIN_EXCHANGE_RECORDS, + now=queried_at, + ) + if exchange_mode in {"fresh", "stale"}: + fallback_records = ( + previous_overview.get("exchange_records") + if isinstance(previous_overview.get("exchange_records"), dict) + else {} + ) + cached_records = self._load_exchange_projection( + db, + account, + fallback=fallback_records, + ) + for scope, rows in cached_records.items(): + valid_rows = [dict(row) for row in rows if isinstance(row, dict)] + overview["exchange_records"][scope] = valid_rows[ + :EXCHANGE_RECORD_DISPLAY_LIMIT + ] + overview[f"exchange_record_count_{scope}"] = len(valid_rows) + all_records.extend((str(scope), row) for row in valid_rows) + overview["domain_sources"][DOMAIN_EXCHANGE_RECORDS] = ( + "local_fresh" if exchange_mode == "fresh" else "queued_stale" + ) + if exchange_mode == "stale": + overview["stale_domains"].append(DOMAIN_EXCHANGE_RECORDS) + overview["pending_domains"].append(DOMAIN_EXCHANGE_RECORDS) + self._queue_domain_sync( + db, + account=account, + domain=DOMAIN_EXCHANGE_RECORDS, + now=queried_at, + ) + else: + for scope, label, fetcher in ( + ("elite", "精英宝典", self._fetch_elite_records), + ("esports", "电竞手册", self._fetch_esports_records), + ("peace", "和平小店", self._fetch_xpd_records), + ): + if scope == "peace" and not peace_on: + continue + key = f"exchange_records_{scope}" + if not self._scope_configured( + scope, + manual_id, + esports_manual_id, + config, + ): + errors[key] = f"未配置{label}活动参数,跳过" + continue + try: + rows = fetcher(client, config, manual_id, esports_manual_id) + except Exception as exc: # noqa: BLE001 外部接口边界需要宽泛兜底 + errors[key] = _err(exc) + continue + overview["exchange_records"][scope] = rows[ + :EXCHANGE_RECORD_DISPLAY_LIMIT + ] + all_records.extend((scope, r) for r in rows) + overview[f"exchange_record_count_{scope}"] = len(rows) + overview["domain_sources"][DOMAIN_EXCHANGE_RECORDS] = ( + "official_bootstrap" + ) overview["exchange_record_count"] = len(all_records) # 兑换记录开始前结束前一步可能留下的角色快照事务。 db.commit() - # 把官方记录**补成账号事件**(带真实兑换时间、按作用域去重)。 - overview["exchange_events_added"] = record_official_exchange_records( - db, account, all_records - ) - record_sync_success( - db, - account_id=int(account.id), - domain=DOMAIN_EXCHANGE_RECORDS, - source="douyu_task.query_account_overview", - observed_at=queried_at, - item_count=len(all_records), - changed_count=int(overview["exchange_events_added"] or 0), - raw_payload=_group_by_scope(all_records), - task_id=task.id, - ) + if exchange_mode == "missing": + # 把官方记录**补成账号事件**(带真实兑换时间、按作用域去重)。 + overview["exchange_events_added"] = persist_official_exchange_records( + db, account, all_records + ) + record_sync_success( + db, + account_id=int(account.id), + domain=DOMAIN_EXCHANGE_RECORDS, + source="douyu_task.query_account_overview", + observed_at=queried_at, + item_count=len(all_records), + changed_count=int(overview["exchange_events_added"] or 0), + raw_payload=_group_by_scope(all_records), + task_id=task.id, + ) + else: + overview["exchange_events_added"] = 0 overview["exchange_records"] = { scope: rows[:EXCHANGE_RECORD_DISPLAY_LIMIT] for scope, rows in _group_by_scope(all_records).items() @@ -603,7 +1021,7 @@ class OverviewMixin: _goods_config(config), [rec for _scope, rec in all_records], ) - if goods_query_ok: + if goods_query_ok or local_goods_fresh: apply_high_value_goods( account, overview["high_value_goods"],