Align Taro pages with Next hub/nav/book/community/VIP, pack the single-index Xiaohongshu portal with host-chrome safe areas, and refresh shared book content plus portal README constraints. Co-authored-by: Cursor <cursoragent@cursor.com>
63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
import secrets
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
from app.core.deps import get_current_openid
|
|
from app.schemas.auth import LoginBody
|
|
from app.services.pocketbase import pb
|
|
from app.services.store import store
|
|
from app.services.wxpay import code2session
|
|
|
|
router = APIRouter(prefix="/auth", tags=["auth"])
|
|
|
|
# 兼容旧 import 路径
|
|
__all__ = ["router", "get_current_openid", "LoginBody"]
|
|
|
|
|
|
@router.post("/login")
|
|
async def login(body: LoginBody):
|
|
from app.services.track import ingest_events
|
|
|
|
try:
|
|
data = await code2session(body.code)
|
|
except Exception as e:
|
|
await ingest_events(
|
|
[{"name": "login_fail", "label": "code2session", "props": {"msg": str(e)[:120]}}],
|
|
openid=None,
|
|
client="api",
|
|
)
|
|
raise HTTPException(400, str(e)) from e
|
|
openid = data["openid"]
|
|
session_key = data.get("session_key") or ""
|
|
token = secrets.token_urlsafe(24)
|
|
store.upsert_user(openid, session_key, token, data.get("unionid"))
|
|
user = store.get_user(openid)
|
|
if user and (user.get("status") or "active") == "blocked":
|
|
await ingest_events(
|
|
[{"name": "login_fail", "label": "blocked", "props": {"openid": openid[:8]}}],
|
|
openid=openid,
|
|
client="api",
|
|
)
|
|
raise HTTPException(403, "账号不可用")
|
|
try:
|
|
if user:
|
|
rid = await pb.upsert_wx_reader(user)
|
|
if rid and rid != user.get("pb_record_id"):
|
|
store.set_user_pb_id(openid, rid)
|
|
except Exception:
|
|
pass
|
|
await ingest_events(
|
|
[
|
|
{
|
|
"name": "login_ok",
|
|
"label": "api",
|
|
"props": {"session_ready": bool(session_key)},
|
|
}
|
|
],
|
|
openid=openid,
|
|
client="api",
|
|
)
|
|
return {"openid": openid, "token": token, "session_ready": bool(session_key)}
|