Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes. Co-authored-by: Cursor <cursoragent@cursor.com>
72 lines
2.3 KiB
Python
72 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Response
|
|
|
|
from app.core.deps import get_current_openid
|
|
from app.domains.books import access_snapshot, is_free_unlock, public_book, truncate_by_percent
|
|
from app.services.store import store
|
|
|
|
router = APIRouter(prefix="/api/books", tags=["books"])
|
|
|
|
|
|
def _no_store(resp: Response) -> None:
|
|
resp.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
|
|
resp.headers["Pragma"] = "no-cache"
|
|
|
|
|
|
@router.get("/{book_id}")
|
|
async def get_book(book_id: str, response: Response):
|
|
_no_store(response)
|
|
book = store.get_book(book_id)
|
|
if not book:
|
|
raise HTTPException(404, "书籍不存在")
|
|
return public_book(book)
|
|
|
|
|
|
@router.get("/{book_id}/access")
|
|
async def book_access(
|
|
book_id: str,
|
|
response: Response,
|
|
openid: str = Depends(get_current_openid),
|
|
):
|
|
"""轻量权限/商品快照:小程序轮询,后台改免费/付费/解锁后秒级生效。"""
|
|
_no_store(response)
|
|
book = store.get_book(book_id)
|
|
if not book:
|
|
raise HTTPException(404, "书籍不存在")
|
|
free = is_free_unlock(book.get("unlock_percent"))
|
|
unlocked = free or store.is_unlocked(openid, book_id)
|
|
return access_snapshot(book, openid=openid, unlocked=unlocked)
|
|
|
|
|
|
@router.get("/{book_id}/read")
|
|
async def read_book(
|
|
book_id: str,
|
|
response: Response,
|
|
openid: str = Depends(get_current_openid),
|
|
):
|
|
_no_store(response)
|
|
book = store.get_book(book_id)
|
|
if not book:
|
|
raise HTTPException(404, "书籍不存在")
|
|
free = is_free_unlock(book.get("unlock_percent"))
|
|
unlocked = free or store.is_unlocked(openid, book_id)
|
|
chapters = book["chapters"]
|
|
if not unlocked:
|
|
chapters = truncate_by_percent(chapters, book["unlock_percent"])
|
|
pub = public_book(book)
|
|
snap = access_snapshot(book, openid=openid, unlocked=unlocked)
|
|
return {
|
|
"book": pub,
|
|
"chapters": chapters,
|
|
"unlocked": unlocked,
|
|
"unlock_percent": book["unlock_percent"],
|
|
"preview_ratio": 1.0 if free else book["unlock_percent"] / 100.0,
|
|
"is_free": free,
|
|
"unlock_btn_text": pub.get("unlock_btn_text"),
|
|
"price_label": pub.get("price_label"),
|
|
"offer_name": pub.get("offer_name"),
|
|
"updated_at": snap["updated_at"],
|
|
"access": snap,
|
|
}
|