nomadro/apps/api/services/ebook_svc/routers/track.py
eric 3e5b7c2098 Monorepo super-optimize: src layout, workspace scripts, docs.
Collapse portal under src/, unify pnpm workspace/scripts, thin VIP topic re-exports, drop orphan vip scaffolds and one-shot scripts, ignore xhs/release, document root commands.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-01 00:34:04 +08:00

29 lines
955 B
Python

from __future__ import annotations
from fastapi import APIRouter, Header
from typing import Annotated
from ebook_svc.schemas.track import TrackBatchBody
from ebook_svc.services.store import store
from ebook_svc.services.track import ingest_events
router = APIRouter(prefix="/track", tags=["track"])
@router.post("")
@router.post("/")
async def track_batch(
body: TrackBatchBody,
authorization: Annotated[str | None, Header()] = None,
):
"""小程序埋点上报。可匿名;若带 Bearer 则绑定 openid。"""
openid = (body.openid or "").strip() or None
if authorization and authorization.startswith("Bearer "):
token = authorization.removeprefix("Bearer ").strip()
user = store.get_user_by_token(token)
if user:
openid = user.get("openid") or openid
events = [e.model_dump() for e in body.events]
return await ingest_events(events, openid=openid, client=body.client or "miniapp")