"""绑定失败分类、客户提示与历史事件展示修正。""" from __future__ import annotations from typing import Any from ...models import ( DouyuTask, PlatformFulfillmentEvent, PlatformFulfillmentTask, ) from ..account_binding import ( classify_bind_cooldown, is_platform_account_bind_cooldown, ) from .common import BINDING_COOLDOWN_ISSUES def _customer_task_message_for(issue_code: str) -> str: messages = { "mapping_missing": "商品正在准备中,请稍后重试", "no_eligible_account": "正在准备可用账号,请稍后重试", "elite_not_opened": "账号正在准备精英宝典,请稍后重试", "points_short": "账号正在补充积分,请稍后重试", "commodity_limit_reached": "当前商品暂时无法兑换,请联系客服", "binding_failed": "暂未识别到绑定角色,请继续使用当前二维码完成扫码", "account_role_cooldown": "当前斗鱼账号未到换绑时间,请截图本页面并联系客服处理", "customer_role_cooldown": "该游戏角色仍在换绑冷却期,请截图本页面并联系客服处理", "eligibility_check_failed": "状态查询异常,请稍后刷新", "manual_required": "当前需要人工处理,请联系客服", } return messages.get(issue_code, "") if issue_code else "" def _customer_task_message(task: PlatformFulfillmentTask) -> str: return _customer_task_message_for(task.issue_code) def _binding_failure_raw_message( issue_code: str, result: dict[str, Any], *, fallback: str = "", ) -> str: before_bound = result.get("before_bound_snapshot") before_bound_role = ( str(before_bound.get("role_name") or "") if isinstance(before_bound, dict) else "" ) role_name = str(result.get("role_name") or "") if issue_code == "account_role_cooldown": role_label = before_bound_role or role_name or "当前角色" return ( f"斗鱼账号当前绑定角色({role_label})未到可换绑时间" ",请更换斗鱼账号或稍后重试" ) if issue_code == "customer_role_cooldown": return ( f"待绑定游戏账号({role_name or '-'})未到换绑时间" "(不是斗鱼/虎牙账号),请重新换账号扫码绑定" ) return fallback def _binding_failure_issue(bind_task: DouyuTask, result: dict[str, Any]) -> str: phase = str(result.get("bind_phase") or "") message = str(bind_task.message or "") confirm_error = str(result.get("confirm_error") or "") confirm_code = result.get("confirm_error_code") # 原始接口文案是分类的第一事实源。历史任务可能已经写入了错误的 # derived issue_code,不能在刷新时继续让错误派生值覆盖原始响应。 cooldown_kind = classify_bind_cooldown(None, confirm_error) if cooldown_kind is None: cooldown_kind = classify_bind_cooldown(None, message) if cooldown_kind == "platform": return "account_role_cooldown" if cooldown_kind == "customer": return "customer_role_cooldown" explicit_issue = str(result.get("issue_code") or "") if explicit_issue in BINDING_COOLDOWN_ISSUES: return explicit_issue ambiguous_kind = classify_bind_cooldown( confirm_code, f"{confirm_error} {message}", ) if ambiguous_kind == "platform": return "account_role_cooldown" if ambiguous_kind == "customer": return "customer_role_cooldown" if ( phase == "change_waiting" or is_platform_account_bind_cooldown( confirm_code, f"{message} {confirm_error}", ) or "暂不能换绑" in message ): task_issue = "account_role_cooldown" elif ( phase == "confirm_failed" or "待绑定游戏账号" in message or "未到换绑时间" in message ): task_issue = "customer_role_cooldown" else: return "binding_failed" messages = { "account_role_cooldown": "当前斗鱼账号未到换绑时间,请截图本页面并联系客服处理", "customer_role_cooldown": "该游戏角色仍在换绑冷却期,请截图本页面并联系客服处理", } return task_issue if messages.get(task_issue) else "binding_failed" def _effective_binding_issue( task: PlatformFulfillmentTask, bind_task: DouyuTask | None, result: dict[str, Any], ) -> str: if ( task.binding_status == "failed" and bind_task is not None and bind_task.status in {"failed", "error", "stopped"} ): corrected = _binding_failure_issue(bind_task, result) if corrected in BINDING_COOLDOWN_ISSUES: return corrected return task.issue_code def _effective_task_message( task: PlatformFulfillmentTask, issue_code: str, ) -> str: if issue_code != task.issue_code and issue_code in BINDING_COOLDOWN_ISSUES: return _customer_task_message_for(issue_code) return task.last_error def _normalized_event_detail( event: PlatformFulfillmentEvent, bind_tasks: dict[int, DouyuTask], ) -> dict[str, Any]: """只修正派生展示,不篡改 append-only 的原始事件。 历史版本曾把客户角色冷却写成斗鱼账号冷却。事件表保留原值用于审计, API 返回使用原始 confirm_error 重新计算后的展示字段。 """ detail = dict(event.detail_json or {}) if event.event_type not in { "douyu_operation_failed", "douyu_operation_stopped", "binding_failed", }: return detail raw_id = ( detail.get("douyuTaskId") if event.event_type.startswith("douyu_operation_") else detail.get("bindTaskId") ) try: bind_task_id = int(raw_id) if raw_id is not None else 0 except (TypeError, ValueError): bind_task_id = 0 bind_task = bind_tasks.get(bind_task_id) if bind_task is None or bind_task.status not in {"failed", "error", "stopped"}: return detail result = bind_task.result or {} issue_code = _binding_failure_issue(bind_task, result) if issue_code not in BINDING_COOLDOWN_ISSUES: return detail corrected_message = _binding_failure_raw_message( issue_code, result, fallback=str( detail.get("rawError") or detail.get("message") or bind_task.message or "" ), ) detail["issueCode"] = issue_code if event.event_type == "binding_failed": detail["rawError"] = corrected_message else: detail["message"] = corrected_message return detail