"""公开 VIP 正式内容 — 小程序拉电子书/课程(合法域名 API)。""" from __future__ import annotations from typing import Any from fastapi import APIRouter from fastapi.responses import JSONResponse from ebook_svc.services.vip_library import get_course, get_ebook, get_lesson, library_meta, read_library router = APIRouter(tags=["vip-library-public"]) _NO_STORE = { "Cache-Control": "no-store, no-cache, must-revalidate", "Access-Control-Allow-Origin": "*", } @router.get("/api/vip/library/meta") @router.get("/api/ebook/vip/library/meta") async def public_library_meta() -> JSONResponse: return JSONResponse(content={"ok": True, **library_meta()}, headers=_NO_STORE) @router.get("/api/vip/library") @router.get("/api/ebook/vip/library") async def public_library() -> JSONResponse: lib = read_library() return JSONResponse(content={"ok": True, **lib}, headers=_NO_STORE) @router.get("/api/vip/ebooks/{book_id}") @router.get("/api/ebook/vip/ebooks/{book_id}") async def public_ebook(book_id: str) -> JSONResponse: book = get_ebook(book_id) if not book: return JSONResponse( content={"ok": False, "error": "not_found", "id": book_id}, status_code=404, headers=_NO_STORE, ) return JSONResponse(content={"ok": True, "ebook": book}, headers=_NO_STORE) @router.get("/api/vip/course") @router.get("/api/ebook/vip/course") async def public_course() -> JSONResponse: return JSONResponse(content={"ok": True, "course": get_course()}, headers=_NO_STORE) @router.get("/api/vip/lessons/{lesson_key}") @router.get("/api/ebook/vip/lessons/{lesson_key}") async def public_lesson(lesson_key: str) -> JSONResponse: lesson = get_lesson(lesson_key) course = get_course() # also resolve lesson meta from course modules meta: dict[str, Any] | None = None for m in course.get("modules") or []: if not isinstance(m, dict): continue for les in m.get("lessons") or []: if isinstance(les, dict) and str(les.get("key")) == lesson_key: meta = les break if not lesson and not meta: return JSONResponse( content={"ok": False, "error": "not_found", "key": lesson_key}, status_code=404, headers=_NO_STORE, ) return JSONResponse( content={ "ok": True, "key": lesson_key, "lesson": lesson or {}, "meta": meta, }, headers=_NO_STORE, )