Add apps/miniprogram/tt as a standalone Douyin app with official usePrivacyCheck + tt.showModal/setClipboardData flow, single-chapter offline reader, and document console privacy setup for clipboard. Co-authored-by: Cursor <cursoragent@cursor.com>
409 lines
14 KiB
TypeScript
409 lines
14 KiB
TypeScript
/**
|
||
* 对齐门户 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 { goHome } from '@/utils/nav'
|
||
import { usePageShare, markPageShare } from '@/utils/pageShare'
|
||
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[]; hidden?: boolean }
|
||
type NavConfig = {
|
||
brand: string
|
||
tagline: string
|
||
footer: string
|
||
categories: NavCategory[]
|
||
}
|
||
|
||
const nav = config as NavConfig
|
||
/** 隐藏分类仍保留在 links.json,仅不展示(如 books) */
|
||
const visibleCategories = nav.categories.filter((c) => !c.hidden)
|
||
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) =>
|
||
`<svg class="menu-icon${open ? ' open' : ''}" viewBox="0 0 24 24" fill="none" aria-hidden="true">
|
||
<path class="menu-line top" d="M4 7h16" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>
|
||
<path class="menu-line mid" d="M4 12h16" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>
|
||
<path class="menu-line bot" d="M4 17h12" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>
|
||
</svg>`
|
||
|
||
const LOGO_SVG = `<svg class="nav-portal-mark" viewBox="0 0 40 40" fill="none" aria-hidden="true">
|
||
<circle cx="20" cy="20" r="18" stroke="#f06664" stroke-width="2.5"/>
|
||
<path d="M12 22c4-8 12-8 16 0" stroke="#f06664" stroke-width="2" stroke-linecap="round"/>
|
||
<circle cx="28" cy="14" r="3" fill="#f06664"/>
|
||
</svg>`
|
||
|
||
const UP_SVG = `<svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
|
||
<path d="M12 19V5M6.5 10.5 12 5l5.5 5.5" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
|
||
</svg>`
|
||
|
||
const CLOSE_SVG = `<svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
|
||
<path d="M7 7l10 10M17 7 7 17" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>
|
||
</svg>`
|
||
|
||
function NavPageInner() {
|
||
const theme = useNomadroTheme()
|
||
const [activeId, setActiveId] = useState(visibleCategories[0]?.id ?? '')
|
||
const [drawerOpen, setDrawerOpen] = useState(false)
|
||
const [toast, setToast] = useState('')
|
||
const [showTop, setShowTop] = useState(false)
|
||
const [pillKey, setPillKey] = useState(0)
|
||
const [scrollInto, setScrollInto] = useState('')
|
||
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 = visibleCategories
|
||
.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(
|
||
() => visibleCategories.find((c) => c.id === activeId) ?? visibleCategories[0],
|
||
[activeId],
|
||
)
|
||
|
||
const onOpen = useCallback((link: NavLink) => {
|
||
const href = String(link.href || '').trim()
|
||
if (!href) {
|
||
void Taro.showToast({ title: '暂无链接', icon: 'none' })
|
||
return
|
||
}
|
||
if (isH5 && typeof window !== 'undefined' && (link.action ?? 'open') === 'open') {
|
||
window.open(href, '_blank', 'noopener,noreferrer')
|
||
return
|
||
}
|
||
// 微信 / 抖音:统一复制(抖音官方支持 tt.setClipboardData)
|
||
void Taro.setClipboardData({
|
||
data: href,
|
||
success: () => {
|
||
setToast('已复制')
|
||
void Taro.showToast({ title: '链接已复制', icon: 'none' })
|
||
},
|
||
fail: () => {
|
||
setToast('复制失败')
|
||
void 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 {
|
||
setScrollInto(`cat-${id}`)
|
||
setTimeout(() => {
|
||
setScrollInto('')
|
||
jumpingRef.current = false
|
||
}, 500)
|
||
}
|
||
}, [])
|
||
|
||
const body = (
|
||
<H5Box className={`app-shell${drawerOpen ? ' drawer-open' : ''}`}>
|
||
<H5Box className="bg-glow" aria-hidden>
|
||
<H5Box className="orb orb-a" />
|
||
<H5Box className="orb orb-b" />
|
||
<H5Box className="orb orb-c" />
|
||
</H5Box>
|
||
|
||
<H5Box className="desktop-rail">
|
||
<H5Box className="desktop-rail-brand">
|
||
<Image src="/icons/nomadro.webp" className="desktop-rail-brand-img" mode="aspectFit" />
|
||
<H5Box>
|
||
<H5Txt className="desktop-rail-title">{nav.brand}</H5Txt>
|
||
<H5Txt className="desktop-rail-sub">{nav.tagline}</H5Txt>
|
||
</H5Box>
|
||
</H5Box>
|
||
<H5Box className="desktop-rail-nav">
|
||
{visibleCategories.map((cat) => {
|
||
const count = countLinks(cat)
|
||
const active = cat.id === activeCategory.id
|
||
return (
|
||
<H5Box
|
||
key={cat.id}
|
||
className={`desktop-rail-item${active ? ' active' : ''}`}
|
||
onClick={() => onSelectCategory(cat.id)}
|
||
>
|
||
<H5Txt className="desktop-rail-emoji">{cat.emoji}</H5Txt>
|
||
<H5Txt className="desktop-rail-item-title">{cat.title}</H5Txt>
|
||
<H5Txt className="desktop-rail-item-count">{count}</H5Txt>
|
||
</H5Box>
|
||
)
|
||
})}
|
||
</H5Box>
|
||
</H5Box>
|
||
|
||
<H5Box className="desktop-main">
|
||
<H5Box className="topbar sticky-stack">
|
||
<H5Box
|
||
className="brand-row"
|
||
style={
|
||
!isH5
|
||
? {
|
||
paddingTop: safe.statusBarHeight,
|
||
minHeight: safe.navBarHeight,
|
||
paddingRight: Math.max(safe.menuRightPad - 16, 88),
|
||
boxSizing: 'border-box',
|
||
}
|
||
: undefined
|
||
}
|
||
>
|
||
<H5Box
|
||
className={`icon-btn menu-trigger${drawerOpen ? ' open' : ''}`}
|
||
onClick={() => setDrawerOpen(true)}
|
||
>
|
||
{isH5 ? (
|
||
<H5Box html={MENU_SVG(drawerOpen)} />
|
||
) : (
|
||
<H5Txt>{drawerOpen ? '✕' : '☰'}</H5Txt>
|
||
)}
|
||
</H5Box>
|
||
|
||
<H5Box className="nav-portal-logo" onClick={goHome}>
|
||
{isH5 ? (
|
||
<H5Box html={LOGO_SVG} />
|
||
) : (
|
||
<Image className="hub-logo-img" src="/icons/nomadro.webp" mode="aspectFit" />
|
||
)}
|
||
<H5Txt className="nav-portal-brand">nomadro</H5Txt>
|
||
<H5Txt className="nav-portal-sep">·</H5Txt>
|
||
<H5Txt className="nav-portal-section">导航</H5Txt>
|
||
</H5Box>
|
||
|
||
<H5Txt key={`pc-${pillKey}`} className="pc-section-label">
|
||
{activeCategory.title}
|
||
</H5Txt>
|
||
</H5Box>
|
||
</H5Box>
|
||
|
||
<H5Box className="page">
|
||
{visibleCategories.map((cat) => {
|
||
const count = countLinks(cat)
|
||
let flatIndex = 0
|
||
return (
|
||
<H5Box key={cat.id} className="cat-panel" id={`cat-${cat.id}`}>
|
||
<H5Box className="cat-heading reveal is-visible">
|
||
<H5Txt className="cat-h2">{cat.title}</H5Txt>
|
||
<H5Txt className="cat-count">{count}</H5Txt>
|
||
</H5Box>
|
||
{cat.groups.map((group) => (
|
||
<H5Box key={group.id} className="group-block">
|
||
<H5Txt className="group-title reveal is-visible">{group.title}</H5Txt>
|
||
<H5Box className="link-grid">
|
||
{group.links.map((link) => {
|
||
const i = flatIndex++
|
||
return (
|
||
<H5Box
|
||
key={link.id}
|
||
className="link-card reveal is-visible"
|
||
style={{
|
||
// @ts-expect-error css var
|
||
'--reveal-delay': `${Math.min(i % 6, 5) * 45}ms`,
|
||
}}
|
||
onClick={() => onOpen(link)}
|
||
>
|
||
<H5Box className="link-icon-wrap">
|
||
<Image
|
||
className="link-icon"
|
||
src={iconSrc(link.icon)}
|
||
mode="aspectFit"
|
||
/>
|
||
</H5Box>
|
||
<H5Box className="link-meta">
|
||
<H5Txt className="link-title">{link.title}</H5Txt>
|
||
<H5Txt className="link-desc">{link.subtitle}</H5Txt>
|
||
</H5Box>
|
||
</H5Box>
|
||
)
|
||
})}
|
||
</H5Box>
|
||
</H5Box>
|
||
))}
|
||
</H5Box>
|
||
)
|
||
})}
|
||
<H5Txt className="footer">{nav.footer}</H5Txt>
|
||
</H5Box>
|
||
</H5Box>
|
||
|
||
{/* SideDrawer */}
|
||
<H5Box className={`drawer-root${drawerOpen ? ' open' : ''}`}>
|
||
<H5Box className="drawer-mask" onClick={() => setDrawerOpen(false)} />
|
||
<H5Box className="drawer-panel">
|
||
<H5Box className="drawer-head">
|
||
<H5Box className="drawer-brand-wrap">
|
||
<Image
|
||
src={iconSrc('/icons/nomadro.webp')}
|
||
style={{ width: 28, height: 28 }}
|
||
mode="aspectFit"
|
||
/>
|
||
<H5Box>
|
||
<H5Txt className="drawer-brand">{nav.brand}</H5Txt>
|
||
<H5Txt className="drawer-sub">{nav.tagline}</H5Txt>
|
||
</H5Box>
|
||
</H5Box>
|
||
<H5Box className="icon-btn" onClick={() => setDrawerOpen(false)}>
|
||
{isH5 ? <H5Box html={CLOSE_SVG} /> : <H5Txt>✕</H5Txt>}
|
||
</H5Box>
|
||
</H5Box>
|
||
<H5Box className="drawer-nav">
|
||
{visibleCategories.map((cat) => {
|
||
const count = countLinks(cat)
|
||
const active = cat.id === activeId
|
||
return (
|
||
<H5Box
|
||
key={cat.id}
|
||
className={`drawer-item${active ? ' active' : ''}`}
|
||
onClick={() => onSelectCategory(cat.id)}
|
||
>
|
||
<H5Txt className="drawer-emoji">{cat.emoji}</H5Txt>
|
||
<H5Txt className="drawer-item-title">{cat.title}</H5Txt>
|
||
<H5Txt className="drawer-item-count">{count}</H5Txt>
|
||
</H5Box>
|
||
)
|
||
})}
|
||
</H5Box>
|
||
</H5Box>
|
||
</H5Box>
|
||
|
||
<H5Box
|
||
className={`back-top${showTop ? ' show' : ''}`}
|
||
onClick={() => {
|
||
if (isH5) window.scrollTo({ top: 0, behavior: 'smooth' })
|
||
else Taro.pageScrollTo({ scrollTop: 0, duration: 300 })
|
||
}}
|
||
>
|
||
{isH5 ? <H5Box html={UP_SVG} /> : <H5Txt>↑</H5Txt>}
|
||
</H5Box>
|
||
|
||
<H5Box className={`toast${toast ? ' show' : ''}`}>
|
||
<H5Txt>{toast}</H5Txt>
|
||
</H5Box>
|
||
</H5Box>
|
||
)
|
||
|
||
// 小程序:外层 ScrollView;H5:整页滚动(对齐 Next)
|
||
if (!isH5) {
|
||
return (
|
||
<ScrollView
|
||
scrollY
|
||
className="navmini-root"
|
||
style={{ height: '100vh', ...safeStyle }}
|
||
data-theme={theme}
|
||
scrollIntoView={scrollInto}
|
||
scrollWithAnimation
|
||
enableFlex
|
||
>
|
||
{body}
|
||
</ScrollView>
|
||
)
|
||
}
|
||
return (
|
||
<H5Box className="navmini-root" data-theme={theme} style={safeStyle}>
|
||
{body}
|
||
</H5Box>
|
||
)
|
||
}
|
||
|
||
function NavPage() {
|
||
usePageShare({ title: 'nomadro · 出海导航', path: '/pages/nav/index' })
|
||
return (
|
||
<MiniModuleGate moduleKey="nav">
|
||
<NavPageInner />
|
||
</MiniModuleGate>
|
||
)
|
||
}
|
||
|
||
export default markPageShare(NavPage)
|
||
|