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>
134 lines
4.5 KiB
Python
134 lines
4.5 KiB
Python
"""导航远程内容 — 读写 remote-config.json 的 navContent。"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from ebook_svc.services.tt_platform_config import resolve_config_file
|
|
|
|
_SEED_CANDIDATES = [
|
|
Path(__file__).resolve().parents[5] / "src" / "lib" / "nav-default-links.json",
|
|
Path(__file__).resolve().parents[5] / "src" / "modules" / "nav" / "data" / "links.json",
|
|
Path("/opt/nomadmini/backend/data/nav-default-links.json"),
|
|
Path("/var/www/nomadro.cn/data/nav-default-links.json"),
|
|
]
|
|
|
|
|
|
def _read_raw() -> dict[str, Any]:
|
|
path = resolve_config_file()
|
|
if not path.is_file():
|
|
return {}
|
|
try:
|
|
raw = json.loads(path.read_text(encoding="utf-8"))
|
|
return raw if isinstance(raw, dict) else {}
|
|
except Exception:
|
|
return {}
|
|
|
|
|
|
def _seed_nav() -> dict[str, Any]:
|
|
for p in _SEED_CANDIDATES:
|
|
if p.is_file():
|
|
try:
|
|
raw = json.loads(p.read_text(encoding="utf-8"))
|
|
if isinstance(raw, dict) and isinstance(raw.get("categories"), list):
|
|
return normalize_nav(raw)
|
|
except Exception:
|
|
continue
|
|
return {
|
|
"brand": "游民导航",
|
|
"tagline": "数字游民工具与出海资源合集",
|
|
"footer": "2026 游民导航",
|
|
"categories": [],
|
|
}
|
|
|
|
|
|
def _norm_link(raw: Any, i: int) -> dict[str, Any]:
|
|
r = raw if isinstance(raw, dict) else {}
|
|
action = r.get("action")
|
|
return {
|
|
"id": str(r.get("id") or f"link-{i}"),
|
|
"title": str(r.get("title") or "未命名"),
|
|
"subtitle": str(r.get("subtitle") or ""),
|
|
"href": str(r.get("href") or ""),
|
|
"icon": str(r.get("icon") or "/icons/nomadro.webp"),
|
|
"action": "copy" if action == "copy" else "open",
|
|
"hidden": bool(r.get("hidden")),
|
|
}
|
|
|
|
|
|
def _norm_group(raw: Any, i: int) -> dict[str, Any]:
|
|
r = raw if isinstance(raw, dict) else {}
|
|
links = r.get("links") if isinstance(r.get("links"), list) else []
|
|
return {
|
|
"id": str(r.get("id") or f"group-{i}"),
|
|
"title": str(r.get("title") or "未命名组"),
|
|
"links": [_norm_link(x, j) for j, x in enumerate(links)],
|
|
"hidden": bool(r.get("hidden")),
|
|
}
|
|
|
|
|
|
def _norm_cat(raw: Any, i: int) -> dict[str, Any]:
|
|
r = raw if isinstance(raw, dict) else {}
|
|
groups = r.get("groups") if isinstance(r.get("groups"), list) else []
|
|
return {
|
|
"id": str(r.get("id") or f"cat-{i}"),
|
|
"title": str(r.get("title") or "未命名分类"),
|
|
"emoji": str(r.get("emoji") or "✦"),
|
|
"groups": [_norm_group(x, j) for j, x in enumerate(groups)],
|
|
"hidden": bool(r.get("hidden")),
|
|
}
|
|
|
|
|
|
def normalize_nav(raw: Any) -> dict[str, Any]:
|
|
r = raw if isinstance(raw, dict) else {}
|
|
cats = r.get("categories") if isinstance(r.get("categories"), list) else []
|
|
out = {
|
|
"brand": str(r.get("brand") or "游民导航"),
|
|
"tagline": str(r.get("tagline") or ""),
|
|
"footer": str(r.get("footer") or ""),
|
|
"categories": [_norm_cat(c, i) for i, c in enumerate(cats)],
|
|
}
|
|
if r.get("updatedAt"):
|
|
out["updatedAt"] = str(r["updatedAt"])
|
|
return out
|
|
|
|
|
|
def read_nav_content() -> dict[str, Any]:
|
|
raw = _read_raw()
|
|
nav = raw.get("navContent")
|
|
if isinstance(nav, dict) and isinstance(nav.get("categories"), list) and nav["categories"]:
|
|
return normalize_nav(nav)
|
|
return _seed_nav()
|
|
|
|
|
|
def save_nav_content(patch: dict[str, Any]) -> dict[str, Any]:
|
|
path = resolve_config_file()
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
raw = _read_raw()
|
|
now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.000Z")
|
|
current = read_nav_content()
|
|
merged = normalize_nav(
|
|
{
|
|
"brand": patch.get("brand", current["brand"]),
|
|
"tagline": patch.get("tagline", current["tagline"]),
|
|
"footer": patch.get("footer", current["footer"]),
|
|
"categories": patch["categories"]
|
|
if isinstance(patch.get("categories"), list)
|
|
else current["categories"],
|
|
"updatedAt": now,
|
|
}
|
|
)
|
|
raw["navContent"] = merged
|
|
raw["updatedAt"] = now
|
|
try:
|
|
raw["version"] = int(raw.get("version") or 0) + 1
|
|
except Exception:
|
|
raw["version"] = 1
|
|
# atomic write
|
|
tmp = path.with_suffix(path.suffix + ".tmp")
|
|
tmp.write_text(json.dumps(raw, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
tmp.replace(path)
|
|
return merged
|