From 53ff37bbcdf9266061e546efcdaef4ccbe601f6f Mon Sep 17 00:00:00 2001 From: yml Date: Thu, 26 Nov 2026 22:22:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E5=B8=83(0.4.2):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=B1=A5=E7=BA=A6=E7=BB=91=E5=AE=9A=E5=90=8C=E6=AD=A5=E5=B9=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=89=B9=E6=AE=8A=E8=A7=92=E8=89=B2=E5=90=8D?= =?UTF-8?q?=E6=A0=B8=E5=AF=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- pyproject.toml | 2 +- tests/test_platform_fulfillment.py | 87 +++++++++++++- uv.lock | 2 +- .../services/platform_fulfillment/binding.py | 111 ++++++++++++++---- .../src/pages/PlatformFulfillmentPage.tsx | 63 +++++++++- 6 files changed, 237 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 545a840..fbfaab9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 直播账号运营后台 -斗鱼 / 虎牙账号运营 Web 平台:批量登录拿 Cookie、按工作台跑活动任务、应用宝点券充值。版本见 `pyproject.toml`(当前 0.4.1)。 +斗鱼 / 虎牙账号运营 Web 平台:批量登录拿 Cookie、按工作台跑活动任务、应用宝点券充值。版本见 `pyproject.toml`(当前 0.4.2)。 ## 能做什么 diff --git a/pyproject.toml b/pyproject.toml index 1ae8716..9e83046 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "live-hub-py" -version = "0.4.1" +version = "0.4.2" description = "直播账号运营 Web 后台" readme = "README.md" requires-python = ">=3.12,<3.13" diff --git a/tests/test_platform_fulfillment.py b/tests/test_platform_fulfillment.py index 3daafe9..bfe8b7d 100644 --- a/tests/test_platform_fulfillment.py +++ b/tests/test_platform_fulfillment.py @@ -2943,9 +2943,11 @@ class TestBindingAndCompletion(_Base): .filter_by(task_id=task.id, event_type="binding_sync_rejected") .one() ) - assert synced == 0 + assert synced == 1 assert task.binding_status == "role_ready" - assert task.bind_task_id == qr_task.id + assert task.bind_task_id == confirm_task.id + assert "别的角色" in (task.last_error or "") + assert "本单目标角色" in (task.last_error or "") assert rejection.detail_json["reason"] == "target_mismatch" def _role_lock_event(self, account, role_name, *, occurred_at=None): @@ -5655,10 +5657,89 @@ class TestBindingAndCompletion(_Base): .filter_by(task_id=task.id, event_type="binding_sync_rejected") .one() ) - assert synced >= 0 + assert synced == 1 assert task.binding_status != "bound" + assert task.binding_status == "role_ready" + assert task.execution_status == "role_ready" + assert "账号旧角色" in (task.last_error or "") + assert "客户指定角色" in (task.last_error or "") assert rejection.detail_json["reason"] == "target_mismatch" + def test_refresh_absorbs_newer_owned_confirm_and_operator_can_fix_special_role_name( + self, + ): + """精英宝典确认后应吸收最新角色;特殊字符名可直接复制并修正后结算。""" + task = self._allocated() + task.customer_role_name = "狞王ゞ" + task.customer_platform = "wechat" + task.customer_system = "android" + qr_task = DouyuTask( + batch_id="older-qr-still-polling", + account_id=task.account_id, + account_assigned_to=self.admin_id, + task_type="get_bind_qr", + handbook_scope="elite", + status="running", + platform_task_id=task.id, + result={"url": "https://livelink.example/qr", "bind_polling": True}, + created_by=self.admin_id, + ) + self.session.add(qr_task) + self.session.flush() + task.bind_task_id = qr_task.id + task.binding_status = "qr_pending" + task.execution_status = "waiting_bind" + account = self.session.get(Account, task.account_id) + account.role_bind_status = "bound" + account.game_name = "゚狙王゜" + account.game_channel = "微信 · 安卓(android)" + confirm_task = DouyuTask( + batch_id="elite-workbench-confirmed-special-name", + account_id=task.account_id, + account_assigned_to=self.admin_id, + task_type="confirm_bind", + handbook_scope="elite", + status="success", + platform_task_id=task.id, + result={ + "role_name": "゚狙王゜", + "area_name": "微信", + "plat_name": "安卓(android)", + "bind_confirmed": True, + "bind_phase": "confirmed", + }, + created_by=self.admin_id, + ) + self.session.add(confirm_task) + self.session.commit() + + detail = service.refresh_binding(self.session, task_id=task.id) + + self.session.refresh(task) + assert task.bind_task_id == confirm_task.id + assert detail["task"]["bindingStatus"] == "role_ready" + assert detail["task"]["executionStatus"] == "role_ready" + assert detail["task"]["customerProfile"]["platformRoleName"] == "゚狙王゜" + assert "゚狙王゜" in (task.last_error or "") + assert "狞王ゞ" in (task.last_error or "") + + updated = service.update_customer_profile_by_operator( + self.session, + task_id=task.id, + current=self.admin, + customer_role_name=detail["task"]["customerProfile"]["platformRoleName"], + platform="wechat", + system="android", + reopen_binding=False, + ) + + self.session.refresh(task) + assert task.customer_role_name == "゚狙王゜" + assert task.binding_status == "bound" + assert task.execution_status == "bound" + assert task.last_error == "" + assert updated["task"]["bindingStatus"] == "bound" + def test_save_customer_profile_immediately_reconciles_existing_qr_candidates(self): """先出码扫码、后提交客户资料时,保存资料应立即把订单收敛到 role_ready。""" task = self._allocated() diff --git a/uv.lock b/uv.lock index b07d180..0522182 100644 --- a/uv.lock +++ b/uv.lock @@ -381,7 +381,7 @@ wheels = [ [[package]] name = "live-hub-py" -version = "0.4.1" +version = "0.4.2" source = { editable = "." } dependencies = [ { name = "alembic" }, diff --git a/web/backend/services/platform_fulfillment/binding.py b/web/backend/services/platform_fulfillment/binding.py index 6700ac9..fd7e4bd 100644 --- a/web/backend/services/platform_fulfillment/binding.py +++ b/web/backend/services/platform_fulfillment/binding.py @@ -28,6 +28,16 @@ from ..binding_ownership import ( source_created_within_lease, ) from ..business_events import record_douyu_task_started +from ..douyu_account_probe import ( + ROLE_BOUND, + ROLE_ERROR, + ROLE_UNKNOWN, + activity_client, + config_mapping, + elite_action_aliases, + persist_probe_result, + resolve_elite_role, +) from ..douyu_binding import ( BINDING_OBSERVATION_TASK_TYPES, BINDING_VIEW_CONFIRMED, @@ -35,6 +45,8 @@ from ..douyu_binding import ( resolve_douyu_binding, resolve_fulfillment_binding, ) +from ..douyu_role_audit import record_role_api_candidates +from ..douyu_service import ensure_douyu_config from ..execution_jobs import ( JOB_DOUYU_BINDING, LANE_FULFILLMENT, @@ -65,18 +77,6 @@ from .runtime import ( _apply_task_lease_policy, ) from .views import get_task_detail -from ..douyu_account_probe import ( - ROLE_BOUND, - ROLE_ERROR, - ROLE_UNKNOWN, - activity_client, - config_mapping, - elite_action_aliases, - persist_probe_result, - resolve_elite_role, -) -from ..douyu_role_audit import record_role_api_candidates -from ..douyu_service import ensure_douyu_config BINDING_SYNC_TASK_TYPES = frozenset( { @@ -104,8 +104,6 @@ ACTIVE_BINDING_STATUSES = frozenset( "role_ready", } ) -#: 可以接受确认任务结算的阶段。 -CONFIRMABLE_BINDING_STATUSES = frozenset({"role_ready", "confirming"}) #: 订单的权威绑定任务类型。查询任务只能展示,不能结算。 BINDING_EVIDENCE_TASK_TYPES = frozenset({"get_bind_qr", "confirm_bind"}) assert BINDING_EVIDENCE_TASK_TYPES <= BINDING_TASK_TYPES @@ -359,6 +357,22 @@ def _order_target_role( return "" +def _binding_target_mismatch_message( + confirmed_role: str, + expected_role: str, +) -> str: + if expected_role: + return ( + f"确认绑定已生效:账号当前角色「{confirmed_role}」" + f"与客户填写「{expected_role}」不一致。" + "请点击“修改资料”,复制或采用平台当前角色后保存。" + ) + return ( + f"确认绑定已生效:账号当前角色「{confirmed_role}」," + "但订单缺少客户目标角色。请点击“修改资料”补齐后保存。" + ) + + def _sync_confirmed_observation( db: Session, observation: DouyuTask, @@ -1177,13 +1191,14 @@ def _apply_binding_task( if binding.status == BINDING_VIEW_CONFIRMED: confirmed_role = binding.confirmed_role_name + customer_role = str(task.customer_role_name or "").strip() + explicit_expected = str(result.get("expected_role_name") or "").strip() target_role = _order_target_role( db, task, before_task_id=int(source.id) if source.task_type == "confirm_bind" else None, ) - customer_role = str(task.customer_role_name or "").strip() - expected_role = target_role or customer_role + expected_role = customer_role or explicit_expected or target_role if is_current: # 订单自己的当前任务(订单创建的或兼容路径挂上的)回查到 bindAct=1。 # 查询类任务不会走到这里:resolve_fulfillment_binding 会把观测降级。 @@ -1197,7 +1212,7 @@ def _apply_binding_task( accepted = False elif source.task_type == "confirm_bind": accepted = ( - phase in CONFIRMABLE_BINDING_STATUSES + phase in ACTIVE_BINDING_STATUSES | {"confirming"} and bool(expected_role) and role_names_match(confirmed_role, expected_role) ) @@ -1207,13 +1222,52 @@ def _apply_binding_task( not expected_role or role_names_match(confirmed_role, expected_role) ) if not accepted: + reason = ( + "target_mismatch" + if expected_role and not role_names_match(confirmed_role, expected_role) + else "missing_target" + ) + if ( + source.task_type == "confirm_bind" + and (owned or is_current) + and phase in ACTIVE_BINDING_STATUSES | {"confirming"} + ): + message = _binding_target_mismatch_message( + confirmed_role, + expected_role, + ) + if ( + int(task.bind_task_id or 0) == int(source.id) + and task.binding_status == "role_ready" + and task.last_error == message + ): + return False + # 确认动作已经成功,旧 QR 指针必须让位。这里不宣告订单绑定完成, + # 而是把实际角色交给运营核对;修改客户资料后会再次结算为 bound。 + task.bind_task_id = int(source.id) + task.binding_status = "role_ready" + task.execution_status = "role_ready" + task.issue_code = "" + task.last_error = message + task.updated_at = _now() + _record_binding_rejection( + db, + task, + source, + reason=reason, + confirmed_role=confirmed_role, + target_role=expected_role, + ) + _apply_task_lease_policy(db, task, lease=lease) + _emit_order_site_binding_state(db, task.id) + return True _record_binding_rejection( db, task, source, reason=( - "target_mismatch" - if expected_role and not role_names_match(confirmed_role, expected_role) + reason + if reason != "missing_target" else "untrusted_confirmation" ), confirmed_role=confirmed_role, @@ -1239,7 +1293,7 @@ def _apply_binding_task( or result.get("current_role_name") or "" )[:128], - "targetRoleName": target_role[:128], + "targetRoleName": expected_role[:128], "confirmedRoleName": confirmed_role[:128], "confirmedChannel": binding.confirmed_channel, }, @@ -1355,6 +1409,18 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]: task = _load_task(db, task_id) if _frozen(task): return get_task_detail(db, task.id) + # 精英宝典等外部入口可能先创建确认任务,履约订单仍指向更早的扫码任务。 + # 刷新时必须主动吸收更新、已归属本单的成功证据,不能只盯着旧指针。 + for evidence in _owned_binding_tasks( + db, + task, + newer_than=int(task.bind_task_id or 0), + ): + if evidence.status != "success": + continue + if _apply_binding_task(db, task, evidence, owned=True): + db.commit() + return get_task_detail(db, task.id) if task.binding_status in ACTIVE_BINDING_STATUSES: latest_pending = _latest_pending_binding_task(db, task) if latest_pending is not None and _attach_pending_binding_task( @@ -1609,7 +1675,10 @@ def reconcile_binding_observations( # role_ready 订单最可能已有新观测可自愈;先处理它们,避免 # 大量历史 failed/manual_required 订单把固定 limit 的名额占满。 case((PlatformFulfillmentTask.binding_status == "role_ready", 0), else_=1), - PlatformFulfillmentTask.id.asc(), + # 其余状态优先收敛最近更新的订单。生产有上千条失败/待扫码历史, + # 按 id 正序会把刚确认的新订单挡在启动对账窗口之外。 + PlatformFulfillmentTask.updated_at.desc(), + PlatformFulfillmentTask.id.desc(), ) .limit(max(1, min(limit, 2000))) .all() diff --git a/web/frontend/src/pages/PlatformFulfillmentPage.tsx b/web/frontend/src/pages/PlatformFulfillmentPage.tsx index 05d141c..ffe423a 100644 --- a/web/frontend/src/pages/PlatformFulfillmentPage.tsx +++ b/web/frontend/src/pages/PlatformFulfillmentPage.tsx @@ -1508,7 +1508,11 @@ export default function PlatformFulfillmentPage() { {task.account && task.bindingStatus === 'failed' ? ( 账号保留中 ) : null} - + 客户提交角色:{customerRole || '未填写'} 扫码绑定角色:{scannedRole || '未识别'} {scannedRole && scannedChannel ? ` · ${scannedChannel}` : ''} @@ -2103,7 +2108,18 @@ export default function PlatformFulfillmentPage() { - {selected.customerProfile.roleName || '未填写'} + {selected.customerProfile.roleName ? ( + + {selected.customerProfile.roleName} + + ) : ( + '未填写' + )} {CUSTOMER_PLATFORM_LABELS[selected.customerProfile.platform] || selected.customerProfile.platform || '-'} @@ -2112,7 +2128,18 @@ export default function PlatformFulfillmentPage() { {CUSTOMER_SYSTEM_LABELS[selected.customerProfile.system] || selected.customerProfile.system || '-'} - {selected.customerProfile.platformRoleName || '未识别'} + {selected.customerProfile.platformRoleName ? ( + + {selected.customerProfile.platformRoleName} + + ) : ( + '未识别' + )} {selected.customerProfile.platformChannel || '-'} @@ -2742,6 +2769,36 @@ export default function PlatformFulfillmentPage() { message="请对照客户主页截图核对并修正" description="修改后会同步写入时间线和订单侧;若正在扫码中会立即按新角色名重算。" /> +
+ 平台当前角色 + + {profileEditTarget?.customerProfile?.platformRoleName ? ( + <> + + {profileEditTarget.customerProfile.platformRoleName} + + + + ) : ( + 暂未识别 + )} + +
客户游戏角色名