feat: 新增签到 Cookie 失效警告功能

- 签到认证失败时返回 cookie_expired 标记而非静默跳过
- 前端显示"签到 Cookie 已失效"警告提示
- 支持 auth_expired 状态,余额数据仍可正常显示
- 认证失败(auth_failed)时清除余额缓存以便重试
This commit is contained in:
fawney19
2026-01-20 02:44:22 +08:00
parent c8b256ded1
commit 8e7582d8b9
4 changed files with 75 additions and 18 deletions

View File

@@ -10,6 +10,7 @@ import httpx
from src.services.provider_ops.actions.base import ProviderAction
from src.services.provider_ops.types import (
ActionResult,
ActionStatus,
BalanceInfo,
ProviderActionType,
)
@@ -53,9 +54,16 @@ class BalanceAction(ProviderAction):
if checkin_result and result.data and hasattr(result.data, "extra"):
if result.data.extra is None:
result.data.extra = {}
result.data.extra["checkin_success"] = checkin_result.get("success")
result.data.extra["checkin_message"] = checkin_result.get("message", "")
logger.debug(f"签到结果已附加到 extra: {checkin_result}")
# 处理 cookie_expired 标记
if checkin_result.get("cookie_expired"):
result.data.extra["cookie_expired"] = True
result.data.extra["cookie_expired_message"] = checkin_result.get("message", "")
result.status = ActionStatus.AUTH_EXPIRED
logger.warning(f"Cookie 已失效: {checkin_result}")
else:
result.data.extra["checkin_success"] = checkin_result.get("success")
result.data.extra["checkin_message"] = checkin_result.get("message", "")
logger.debug(f"签到结果已附加到 extra: {checkin_result}")
return result

View File

@@ -120,7 +120,8 @@ class NewApiBalanceAction(BalanceAction):
Returns:
签到结果字典,包含 success 和 message 字段;
如果功能未开放或认证失败,返回 None
如果功能未开放返回 None
如果 Cookie 失效返回 {"cookie_expired": True}
"""
site = client.base_url.host or str(client.base_url)
checkin_endpoint = self.config.get("checkin_endpoint", "/api/user/checkin")
@@ -139,10 +140,10 @@ class NewApiBalanceAction(BalanceAction):
logger.debug(f"[{site}] 签到功能未开放")
return None
# 401/403 表示签到需要额外认证(如 Cookie当前配置不支持
# 401/403 表示 Cookie 已失效
if response.status_code in (401, 403):
logger.debug(f"[{site}] 签到需要额外认证")
return None
logger.warning(f"[{site}] Cookie 已失效(签到返回 {response.status_code}")
return {"cookie_expired": True, "message": "Cookie 已失效"}
try:
data = response.json()
@@ -160,15 +161,15 @@ class NewApiBalanceAction(BalanceAction):
logger.debug(f"[{site}] 今日已签到: {message}")
return {"success": None, "message": message or "今日已签到"}
# 检查是否是认证失败(未登录、无权限等)- 静默跳过
# 检查是否是认证失败(未登录、无权限等)- Cookie 已失效
auth_fail_indicators = [
"未登录", "请登录", "login", "unauthorized", "无权限", "权限不足",
"turnstile", "captcha", "验证码", # 需要人机验证
]
is_auth_fail = any(ind in message.lower() for ind in auth_fail_indicators)
if is_auth_fail:
logger.debug(f"[{site}] 签到需要额外认证,跳过")
return None
logger.warning(f"[{site}] Cookie 已失效(签到认证失败): {message}")
return {"cookie_expired": True, "message": message or "Cookie 已失效"}
# 其他失败情况
logger.debug(f"[{site}] 签到失败: {message}")

View File

@@ -353,9 +353,12 @@ class ProviderOpsService:
provider_id, ProviderActionType.QUERY_BALANCE, config
)
# 成功时更新缓存
if result.status == ActionStatus.SUCCESS and result.data:
# 成功或 auth_expired 时缓存auth_expired 带有 cookie_expired 信息供前端显示警告)
if result.status in (ActionStatus.SUCCESS, ActionStatus.AUTH_EXPIRED) and result.data:
await self._cache_balance(provider_id, result)
# auth_failed 时清除缓存(配置错误,用户修正后应立即重试)
elif result.status == ActionStatus.AUTH_FAILED:
await self._clear_balance_cache(provider_id)
return result
@@ -398,6 +401,12 @@ class ProviderOpsService:
except Exception as e:
logger.warning(f"异步刷新余额失败: provider_id={provider_id}, error={e}")
async def _clear_balance_cache(self, provider_id: str) -> None:
"""清除余额缓存(认证失败时调用)"""
cache_key = f"provider_ops:balance:{provider_id}"
await CacheService.delete(cache_key)
logger.info(f"余额缓存已清除: provider_id={provider_id}")
async def _cache_balance(self, provider_id: str, result: ActionResult) -> None:
"""缓存余额结果"""
cache_key = f"provider_ops:balance:{provider_id}"