Harden weapp for review: silent audit book, no flashy module defaults, safe-area PX, subpackages/lazyCodeLoading, and CDN nav icons. Document pre3 and move next work to pre4. Co-authored-by: Cursor <cursoragent@cursor.com>
89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
from __future__ import annotations
|
||
|
||
from fastapi import APIRouter, Depends, HTTPException, Response
|
||
|
||
from ebook_svc.core.deps import get_current_openid, get_optional_openid
|
||
from ebook_svc.domains.books import access_snapshot, is_free_unlock, public_book, truncate_by_percent
|
||
from ebook_svc.services.store import store
|
||
|
||
router = APIRouter(prefix="/books", tags=["books"])
|
||
|
||
# 原小册(对外文案 Youtube运营笔记)book_id=default:单买解锁 或 VIP(__vip__) 均可全文
|
||
VIP_BOOK_ID = "__vip__"
|
||
VIP_ALSO_UNLOCKS = frozenset({"default"})
|
||
|
||
|
||
def _no_store(resp: Response) -> None:
|
||
resp.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
|
||
resp.headers["Pragma"] = "no-cache"
|
||
|
||
|
||
def _book_unlocked(openid: str, book_id: str, *, free: bool) -> bool:
|
||
if free:
|
||
return True
|
||
if store.is_unlocked(openid, book_id):
|
||
return True
|
||
if book_id in VIP_ALSO_UNLOCKS and store.is_unlocked(openid, VIP_BOOK_ID):
|
||
return True
|
||
return False
|
||
|
||
|
||
@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 = _book_unlocked(openid, book_id, free=free)
|
||
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_optional_openid),
|
||
):
|
||
_no_store(response)
|
||
book = store.get_book(book_id)
|
||
if not book:
|
||
raise HTTPException(404, "书籍不存在")
|
||
free = is_free_unlock(book.get("unlock_percent"))
|
||
# 付费书必须登录;免费/提审书可匿名全文
|
||
if not free and not openid:
|
||
raise HTTPException(401, "未登录")
|
||
unlocked = _book_unlocked(openid or "anon", book_id, free=free)
|
||
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,
|
||
}
|