Restructure /wechat nav (VIP submenu for ebooks/course/shop/memo), rebuild ops pages with shared wx UI, and wire vip-library remote content end-to-end with README paths. Co-authored-by: Cursor <cursoragent@cursor.com>
107 lines
3.2 KiB
Python
107 lines
3.2 KiB
Python
"""VIP 正式内容管理 — /api/admin/vip/library*"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from pydantic import BaseModel, Field
|
|
|
|
from ebook_svc.core.security import require_admin
|
|
from ebook_svc.services.vip_library import (
|
|
get_ebook,
|
|
get_course,
|
|
get_lesson,
|
|
library_meta,
|
|
read_library,
|
|
write_library,
|
|
)
|
|
|
|
router = APIRouter(prefix="/api/admin/vip/library", tags=["vip-library-admin"])
|
|
|
|
|
|
class LibraryPatch(BaseModel):
|
|
ebooks: dict[str, Any] | None = None
|
|
course: dict[str, Any] | None = None
|
|
lessons: dict[str, Any] | None = None
|
|
|
|
|
|
class EbookPatch(BaseModel):
|
|
title: str | None = None
|
|
free: bool | None = None
|
|
estimatedMinutes: int | None = Field(default=None, ge=0)
|
|
headers: list[str] | None = None
|
|
sections: list[str] | None = None
|
|
markdown: str | None = None
|
|
|
|
|
|
class LessonPatch(BaseModel):
|
|
markdown: str | None = None
|
|
videoUrl: str | None = None
|
|
|
|
|
|
@router.get("")
|
|
async def get_library(_: str = Depends(require_admin)) -> dict[str, Any]:
|
|
lib = read_library()
|
|
return {"ok": True, "meta": library_meta(), **lib}
|
|
|
|
|
|
@router.get("/meta")
|
|
async def get_meta(_: str = Depends(require_admin)) -> dict[str, Any]:
|
|
return {"ok": True, **library_meta()}
|
|
|
|
|
|
@router.put("")
|
|
async def put_library(body: LibraryPatch, _: str = Depends(require_admin)) -> dict[str, Any]:
|
|
saved = write_library(body.model_dump(exclude_none=True))
|
|
return {"ok": True, "meta": library_meta(), **saved}
|
|
|
|
|
|
@router.get("/ebooks/{book_id}")
|
|
async def get_ebook_admin(book_id: str, _: str = Depends(require_admin)) -> dict[str, Any]:
|
|
book = get_ebook(book_id)
|
|
if not book:
|
|
return {"ok": False, "error": "not_found", "id": book_id}
|
|
return {"ok": True, "ebook": book}
|
|
|
|
|
|
@router.put("/ebooks/{book_id}")
|
|
async def put_ebook_admin(
|
|
book_id: str, body: EbookPatch, _: str = Depends(require_admin)
|
|
) -> dict[str, Any]:
|
|
patch = body.model_dump(exclude_none=True)
|
|
patch["id"] = book_id
|
|
saved = write_library({"ebooks": {book_id: patch}})
|
|
return {"ok": True, "ebook": (saved.get("ebooks") or {}).get(book_id), "meta": library_meta()}
|
|
|
|
|
|
@router.get("/course")
|
|
async def get_course_admin(_: str = Depends(require_admin)) -> dict[str, Any]:
|
|
return {"ok": True, "course": get_course()}
|
|
|
|
|
|
@router.put("/course")
|
|
async def put_course_admin(body: dict[str, Any], _: str = Depends(require_admin)) -> dict[str, Any]:
|
|
saved = write_library({"course": body})
|
|
return {"ok": True, "course": saved.get("course"), "meta": library_meta()}
|
|
|
|
|
|
@router.get("/lessons/{lesson_key}")
|
|
async def get_lesson_admin(lesson_key: str, _: str = Depends(require_admin)) -> dict[str, Any]:
|
|
lesson = get_lesson(lesson_key)
|
|
if not lesson:
|
|
return {"ok": False, "error": "not_found", "key": lesson_key}
|
|
return {"ok": True, "key": lesson_key, "lesson": lesson}
|
|
|
|
|
|
@router.put("/lessons/{lesson_key}")
|
|
async def put_lesson_admin(
|
|
lesson_key: str, body: LessonPatch, _: str = Depends(require_admin)
|
|
) -> dict[str, Any]:
|
|
saved = write_library({"lessons": {lesson_key: body.model_dump(exclude_none=True)}})
|
|
return {
|
|
"ok": True,
|
|
"key": lesson_key,
|
|
"lesson": (saved.get("lessons") or {}).get(lesson_key),
|
|
"meta": library_meta(),
|
|
}
|