/** * 对齐门户 app/nav → @navmini/App.tsx(结构 / 样式 / 双列卡片 / 侧栏) * 数据:src/data/links.json(与 navmini 同步) */ import { useCallback, useEffect, useMemo, useRef, useState, type CSSProperties } from 'react' import { Image, ScrollView } from '@tarojs/components' import Taro from '@tarojs/taro' import { MiniModuleGate } from '@/components/MiniModuleGate' import { H5Box, H5Txt } from '@/components/H5Box' import config from '@/data/links.json' import { applyH5Theme, useNomadroTheme } from '@/utils/h5Theme' import { getSafeAreaMetrics } from '@/utils/safeArea' import './index.scss' type LinkAction = 'open' | 'copy' type NavLink = { id: string title: string subtitle: string href: string icon: string action?: LinkAction } type NavGroup = { id: string; title: string; links: NavLink[] } type NavCategory = { id: string; title: string; emoji: string; groups: NavGroup[] } type NavConfig = { brand: string tagline: string footer: string categories: NavCategory[] } const nav = config as NavConfig const isH5 = process.env.TARO_ENV === 'h5' function countLinks(cat: NavCategory) { return cat.groups.reduce((s, g) => s + g.links.length, 0) } function iconSrc(icon: string) { if (!icon) return '' // 必须从小程序包根读:/icons/xxx.webp(去掉 / 会变成相对 pages/nav/,全裂) const cleaned = icon.replace(/^\//, '') return `/icons/${cleaned.replace(/^icons\//, '')}` } const MENU_SVG = (open: boolean) => `` const LOGO_SVG = `` const UP_SVG = `` const CLOSE_SVG = `` function NavPageInner() { const theme = useNomadroTheme() const [activeId, setActiveId] = useState(nav.categories[0]?.id ?? '') const [drawerOpen, setDrawerOpen] = useState(false) const [toast, setToast] = useState('') const [showTop, setShowTop] = useState(false) const [pillKey, setPillKey] = useState(0) const jumpingRef = useRef(false) const safe = useMemo(() => getSafeAreaMetrics(), []) const safeStyle = useMemo( () => ({ ['--safe-status-bar' as string]: `${safe.statusBarHeight}px`, ['--safe-nav-bar' as string]: `${safe.navBarHeight}px`, ['--safe-menu-pad' as string]: `${safe.menuRightPad}px`, ['--safe-header' as string]: `${safe.headerHeight}px`, }) as CSSProperties, [safe], ) // rem 锁定(H5);主题由 useNomadroTheme + 根节点 data-theme 承接 useEffect(() => { applyH5Theme(theme) if (!isH5) return const html = document.documentElement const prevFs = html.style.fontSize const lock = () => { html.style.fontSize = '16px' } lock() window.addEventListener('resize', lock) return () => { window.removeEventListener('resize', lock) html.style.fontSize = prevFs } }, [theme]) useEffect(() => { if (!toast) return const id = setTimeout(() => setToast(''), 1600) return () => clearTimeout(id) }, [toast]) useEffect(() => { if (!isH5) return const sections = nav.categories .map((c) => document.getElementById(`cat-${c.id}`)) .filter((el): el is HTMLElement => !!el) const onScroll = () => { setShowTop(window.scrollY > 480) if (jumpingRef.current || sections.length === 0) return const marker = 88 let current = sections[0].id.replace(/^cat-/, '') for (const el of sections) { if (el.getBoundingClientRect().top <= marker) current = el.id.replace(/^cat-/, '') else break } setActiveId((prev) => { if (prev === current) return prev setPillKey((k) => k + 1) return current }) } onScroll() window.addEventListener('scroll', onScroll, { passive: true }) return () => window.removeEventListener('scroll', onScroll) }, []) const activeCategory = useMemo( () => nav.categories.find((c) => c.id === activeId) ?? nav.categories[0], [activeId], ) const onOpen = useCallback((link: NavLink) => { const action = link.action ?? 'open' if (action === 'copy') { Taro.setClipboardData({ data: link.href }) .then(() => setToast('已复制')) .catch(() => setToast('复制失败')) return } if (isH5 && typeof window !== 'undefined') { window.open(link.href, '_blank', 'noopener,noreferrer') } else { Taro.setClipboardData({ data: link.href }).then(() => { Taro.showToast({ title: '链接已复制', icon: 'none' }) }) } }, []) const onSelectCategory = useCallback((id: string) => { setActiveId(id) setPillKey((k) => k + 1) setDrawerOpen(false) jumpingRef.current = true if (isH5) { const el = document.getElementById(`cat-${id}`) el?.scrollIntoView({ behavior: 'smooth', block: 'start' }) setTimeout(() => { jumpingRef.current = false }, 700) } else { jumpingRef.current = false } }, []) function goHome() { if (isH5) { Taro.redirectTo({ url: '/pages/index/index' }).catch(() => { Taro.navigateTo({ url: '/pages/index/index' }) }) } else { Taro.reLaunch({ url: '/pages/index/index' }).catch(() => { Taro.redirectTo({ url: '/pages/index/index' }) }) } } const body = ( {nav.brand} {nav.tagline} {nav.categories.map((cat) => { const count = countLinks(cat) const active = cat.id === activeCategory.id return ( onSelectCategory(cat.id)} > {cat.emoji} {cat.title} {count} ) })} setDrawerOpen(true)} > {isH5 ? ( ) : ( {drawerOpen ? '✕' : '☰'} )} {isH5 ? ( ) : ( )} nomadro · 导航 {activeCategory.title} {nav.categories.map((cat) => { const count = countLinks(cat) let flatIndex = 0 return ( {cat.title} {count} {cat.groups.map((group) => ( {group.title} {group.links.map((link) => { const i = flatIndex++ return ( onOpen(link)} > {link.title} {link.subtitle} ) })} ))} ) })} {nav.footer} {/* SideDrawer */} setDrawerOpen(false)} /> {nav.brand} {nav.tagline} setDrawerOpen(false)}> {isH5 ? : ✕} {nav.categories.map((cat) => { const count = countLinks(cat) const active = cat.id === activeId return ( onSelectCategory(cat.id)} > {cat.emoji} {cat.title} {count} ) })} { if (isH5) window.scrollTo({ top: 0, behavior: 'smooth' }) else Taro.pageScrollTo({ scrollTop: 0, duration: 300 }) }} > {isH5 ? : ↑} {toast} ) // 小程序:外层 ScrollView;H5:整页滚动(对齐 Next) if (!isH5) { return ( {body} ) } return ( {body} ) } export default function NavPage() { return ( ) }