Archived
补齐最后缺口: - POST /api/v1/auth/admin_login/email、/auth/admin_login/verify(管理员 2FA Step2/3), 复用 auth_routes 的票据表与尝试上限(跨栈共享),签发会话 Cookie 重构(消除约 200 行重复,11 个 router 受益): - web/request_utils.py:request_data / safe_int / extract_id / cookies / dedup_ids 原在各 router 各复制一份,现集中 - web/serializers.py:format_credits / user_public / record_req_log - 各 router 改为导入别名,行为逐字不变 覆盖率:Flask 90 条业务路由全部有 FastAPI 等价端点(含 5 个向后兼容别名)。 验证:pytest 69/69;ruff All checks passed;pyright 0 errors;奇偶 36/36;冒烟 10/10。
63 lines
2.1 KiB
Python
63 lines
2.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""认证状态端点(FastAPI 栈)——与 Flask /auth/me 响应契约逐字段对齐。
|
|
|
|
- GET/POST /api/v1/auth/me ↔ Flask /auth/me
|
|
· user:站点用户公开字段(未登录为 None)
|
|
· duration_text:次数文案(不限次数 / 已用完 / N 次)
|
|
· announcement、smtp_configured、register_credits 逐字段一致
|
|
"""
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Depends, Request
|
|
|
|
from core import repo
|
|
from web import ratelimit
|
|
from web.deps import SiteUser, current_user
|
|
from web.response import error, success
|
|
from web.serializers import format_credits as _format_credits
|
|
from web.serializers import user_public as _user_public
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
@router.api_route('/auth/me', methods=['GET', 'POST'])
|
|
async def auth_me(
|
|
user: Annotated[SiteUser | None, Depends(current_user)],
|
|
request: Request,
|
|
):
|
|
"""当前会话与站点公开配置(未登录也返回,与 Flask 同口径)。"""
|
|
await ratelimit.guard(request, bucket='free', limit=30, window=60)
|
|
try:
|
|
ann = await repo.get_announcement()
|
|
smtp_ok = await repo.smtp_configured()
|
|
try:
|
|
credits = int(await repo.get_setting('register_credits', '3') or 0)
|
|
except (TypeError, ValueError):
|
|
credits = 3
|
|
credits = max(0, credits)
|
|
|
|
pub = None
|
|
duration_text = ''
|
|
if user is not None:
|
|
row = await repo.get_user_by_id(user.id)
|
|
if row is not None:
|
|
pub = _user_public(row)
|
|
duration_text = _format_credits(int(row.get('duration_seconds') or 0))
|
|
return success({
|
|
'user': pub,
|
|
'duration_text': duration_text,
|
|
'announcement': {
|
|
'title': ann.get('title', ''),
|
|
'content': ann.get('content', ''),
|
|
'popup_mode': ann.get('popup_mode', 'off'),
|
|
'updated_at': ann.get('updated_at', ''),
|
|
},
|
|
'smtp_configured': smtp_ok,
|
|
'register_credits': credits,
|
|
})
|
|
except Exception:
|
|
return error('服务暂时不可用,请稍后再试', 500)
|