nomadro/apps/api/services/ebook_svc/routers/nav_admin.py
Eric 4a7690c0b1 Add remote-controlled nav CRUD and fix weapp VIP/nav chrome.
Ship admin nav editor + navContent remote-config, custom nav bars for memo/store, and weapp icon copy/webp path so nav icons render in DevTools.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-06 18:37:34 +08:00

84 lines
2.4 KiB
Python
Raw 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.

"""导航管理:admin.nomadro.cn/wechat/nav — CRUD navContent。"""
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.nav_config import read_nav_content, save_nav_content
from ebook_svc.services.tt_platform_config import resolve_config_file
router = APIRouter(prefix="/api/admin/nav", tags=["nav-admin"])
class NavLinkIn(BaseModel):
id: str = ""
title: str = ""
subtitle: str = ""
href: str = ""
icon: str = "/icons/nomadro.webp"
action: str = "open"
hidden: bool = False
class NavGroupIn(BaseModel):
id: str = ""
title: str = ""
links: list[NavLinkIn] = Field(default_factory=list)
hidden: bool = False
class NavCategoryIn(BaseModel):
id: str = ""
title: str = ""
emoji: str = "✦"
groups: list[NavGroupIn] = Field(default_factory=list)
hidden: bool = False
class NavBody(BaseModel):
brand: str | None = None
tagline: str | None = None
footer: str | None = None
categories: list[NavCategoryIn] | None = None
@router.get("")
async def get_nav(_: str = Depends(require_admin)) -> dict[str, Any]:
nav = read_nav_content()
path = resolve_config_file()
return {
"ok": True,
"nav": nav,
"configPath": str(path),
"stats": {
"categories": len(nav.get("categories") or []),
"groups": sum(len(c.get("groups") or []) for c in nav.get("categories") or []),
"links": sum(
len(g.get("links") or [])
for c in nav.get("categories") or []
for g in c.get("groups") or []
),
"hiddenCategories": sum(
1 for c in nav.get("categories") or [] if c.get("hidden")
),
},
}
@router.put("")
async def put_nav(body: NavBody, _: str = Depends(require_admin)) -> dict[str, Any]:
patch: dict[str, Any] = {}
if body.brand is not None:
patch["brand"] = body.brand
if body.tagline is not None:
patch["tagline"] = body.tagline
if body.footer is not None:
patch["footer"] = body.footer
if body.categories is not None:
patch["categories"] = [c.model_dump() for c in body.categories]
saved = save_nav_content(patch)
return {"ok": True, "nav": saved}