nomadro/apps/api/services/ebook_svc/routers/books.py
Eric 60606e53a7 Isolate WeChat audit books as always-free from paid unlocks.
Keep review-mode content off nomad VIP/purchase state, document the rule in README, and add the 82 API push script.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-22 20:04:22 +08:00

105 lines
3.3 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.

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_audit_book_id,
is_book_free,
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:
# 提审书:只按免费放行,绝不读正式 unlocks / __vip__
if is_audit_book_id(book_id):
return True
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
def _book_is_free(book: dict, book_id: str) -> bool:
if is_audit_book_id(book_id):
return True
return is_book_free(book) or is_free_unlock(book.get("unlock_percent"))
@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 = _book_is_free(book, book_id)
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 = _book_is_free(book, book_id)
# 付费书必须登录;免费/提审书可匿名全文
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,
}