nomadro/apps/api/services/ebook_svc/routers/vip_library_public.py
Eric 017008dd7c Harden weapp/API P0 paths: VIP auth, ebook login, WAL limits, game stub.
Drop Three from weapp game, mount VIP library on unified API, and auto-enable local debug without manual .env.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-11 19:06:50 +08:00

147 lines
4.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""公开 VIP 正式内容 — 目录可匿名;正文需 VIP(免费方案除外)。"""
from __future__ import annotations
from typing import Any
from fastapi import APIRouter, Depends
from fastapi.responses import JSONResponse
from ebook_svc.core.deps import get_optional_openid
from ebook_svc.services.store import store
from ebook_svc.services.vip_config import read_vip_config
from ebook_svc.services.vip_library import get_course, get_ebook, get_lesson, library_meta, read_library
router = APIRouter(tags=["vip-library-public"])
VIP_BOOK_ID = "__vip__"
_NO_STORE = {
"Cache-Control": "no-store, no-cache, must-revalidate",
}
def _is_vip(openid: str) -> bool:
cfg = read_vip_config()
if cfg.get("is_free") or int(cfg.get("vipPriceFen") or 0) <= 0:
return True
if not openid:
return False
return store.is_unlocked(openid, VIP_BOOK_ID)
def _strip_ebook(book: dict[str, Any]) -> dict[str, Any]:
"""未解锁:只返回元信息,去掉章节正文。"""
out = {k: v for k, v in book.items() if k != "chapters"}
chapters = book.get("chapters")
if isinstance(chapters, list):
out["chapters"] = [
{
"id": c.get("id"),
"title": c.get("title"),
"locked": True,
}
for c in chapters
if isinstance(c, dict)
]
out["locked"] = True
return out
@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(openid: str = Depends(get_optional_openid)) -> JSONResponse:
lib = read_library()
if _is_vip(openid):
return JSONResponse(content={"ok": True, **lib}, headers=_NO_STORE)
# 未 VIP:课程大纲可看,电子书正文与课时 markdown 剥离
ebooks = lib.get("ebooks") if isinstance(lib.get("ebooks"), dict) else {}
stripped_books = {
k: _strip_ebook(v) if isinstance(v, dict) else v for k, v in ebooks.items()
}
return JSONResponse(
content={
"ok": True,
"version": lib.get("version"),
"ebooks": stripped_books,
"course": lib.get("course") or get_course(),
"lessons": {},
"locked": True,
},
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, openid: str = Depends(get_optional_openid)
) -> 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,
)
if not _is_vip(openid):
return JSONResponse(
content={"ok": True, "ebook": _strip_ebook(book), "locked": True},
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, openid: str = Depends(get_optional_openid)
) -> JSONResponse:
lesson = get_lesson(lesson_key)
course = get_course()
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,
)
if not _is_vip(openid):
return JSONResponse(
content={
"ok": True,
"key": lesson_key,
"lesson": {},
"meta": meta,
"locked": True,
},
headers=_NO_STORE,
)
return JSONResponse(
content={
"ok": True,
"key": lesson_key,
"lesson": lesson or {},
"meta": meta,
},
headers=_NO_STORE,
)