/** * 对齐门户 components/sections/hero.tsx(结构 / PORTALS / 开发快捷入口) * H5 关键布局用原生 div(Taro View 自定义元素会弄塌 grid/绝对定位)。 */ /** * 首页 A — 数字游民门户(审核通过后由后台 homeVariant=hub 启用) */ import { useEffect, useMemo, useState, type CSSProperties, type ReactNode } from 'react' import { View, Text, Image } from '@tarojs/components' import Taro, { usePageScroll } from '@tarojs/taro' import { isModuleEnabled } from '@/services/remoteConfig' import type { NomadroRemoteConfig } from '@/data/remoteConfig.defaults' import { applyH5Theme, type H5Theme } from '@/utils/h5Theme' import { goPage } from '@/utils/nav' import { getSafeAreaMetrics } from '@/utils/safeArea' import { trackClick, trackPage } from '@/utils/track' import './index.scss' type PortalId = 'nav' | 'book' | 'community' | 'vip' type Portal = { id: PortalId moduleKey: keyof NomadroRemoteConfig['modules'] emoji: string name: string main?: boolean } const PORTALS: Portal[] = [ { id: 'nav', moduleKey: 'nav', emoji: '🧭', name: '出海导航', main: false }, { id: 'book', moduleKey: 'book', emoji: '📖', name: '数字游民', main: false }, { id: 'community', moduleKey: 'community', emoji: '🌏', name: '旅居社群', main: true }, ] const INTERNAL_PORTALS: Record = { nav: '/pages/nav/index', book: '/packageBook/pages/book/index', community: '/pages/community/index', vip: '/pages/vip/index', } const isH5 = process.env.TARO_ENV === 'h5' /** 开发快捷入口:默认隐藏 VIP;Admin/Config 仅 H5 开发态 */ const showQuickNav = false const isDev = process.env.NODE_ENV === 'development' const LOGO_SVG = `` const GLOBE_SVG = `` function goPath(path: string) { goPage(path) } function openDevLink(kind: 'vip' | 'admin' | 'config') { if (kind === 'vip') { goPath('/pages/vip/index') return } if (isH5 && typeof window !== 'undefined') { const origin = window.location.origin.includes('10086') ? 'http://localhost:3000' : window.location.origin const href = kind === 'admin' ? `${origin}/admin` : `${origin}/admin/config` window.open(href, '_blank', 'noopener,noreferrer') return } Taro.showToast({ title: kind === 'admin' ? '请用门户 /admin' : '请用门户 /admin/config', icon: 'none', }) } /** H5 用原生标签,避开 taro-view-core 默认 inline */ function Box({ className, id, children, onClick, html, style, 'aria-hidden': ariaHidden, 'aria-label': ariaLabel, 'data-theme': dataTheme, }: { className?: string id?: string children?: ReactNode onClick?: () => void html?: string style?: CSSProperties 'aria-hidden'?: boolean 'aria-label'?: string 'data-theme'?: string }) { if (isH5) { return (
{html ? undefined : children}
) } return ( {html ? undefined : children} ) } function Txt({ className, children }: { className?: string; children?: ReactNode }) { if (isH5) return {children} return {children} } export default function HubHome({ config, theme, }: { config: NomadroRemoteConfig theme: H5Theme }) { const [navScrolled, setNavScrolled] = useState(false) useEffect(() => { applyH5Theme(theme) trackPage('pages/hub/index', '门户首页', { homeVariant: 'hub' }) }, [theme]) // H5:锁 16px,并干掉 Taro flexible 脚本改根字号 useEffect(() => { if (!isH5) return const html = document.documentElement const prev = html.style.fontSize const lock = () => { html.style.fontSize = '16px' } lock() const id = window.setInterval(lock, 500) window.addEventListener('resize', lock) return () => { clearInterval(id) window.removeEventListener('resize', lock) html.style.fontSize = prev } }, []) usePageScroll((e) => { setNavScrolled(e.scrollTop > 24) }) useEffect(() => { if (!isH5) return const onScroll = () => { setNavScrolled(window.scrollY > 24) } window.addEventListener('scroll', onScroll, { passive: true }) onScroll() return () => window.removeEventListener('scroll', onScroll) }, []) useEffect(() => { if (!isH5) return const host = document.getElementById('particle-canvas-host') if (!host) return let canvas = host.querySelector('canvas') as HTMLCanvasElement | null if (!canvas) { canvas = document.createElement('canvas') canvas.id = 'particle-canvas' canvas.className = 'hub-particle-canvas' canvas.setAttribute('aria-hidden', 'true') host.appendChild(canvas) } const ctx = canvas.getContext('2d') if (!ctx) return let particles: { x: number; y: number; r: number; vx: number; vy: number; a: number }[] = [] let w = 0 let h = 0 let raf = 0 let running = true const resize = () => { w = canvas!.width = window.innerWidth h = canvas!.height = window.innerHeight const n = Math.min(60, Math.floor((w * h) / 22000)) particles = Array.from({ length: n }, () => ({ x: Math.random() * w, y: Math.random() * h, r: Math.random() * 1.6 + 0.4, vx: (Math.random() - 0.5) * 0.3, vy: (Math.random() - 0.5) * 0.3, a: Math.random() * 0.4 + 0.15, })) } const draw = () => { if (!running) return ctx.clearRect(0, 0, w, h) particles.forEach((p, i) => { p.x += p.vx p.y += p.vy if (p.x < 0 || p.x > w) p.vx *= -1 if (p.y < 0 || p.y > h) p.vy *= -1 ctx.beginPath() ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2) ctx.fillStyle = `rgba(255,255,255,${p.a})` ctx.fill() for (let j = i + 1; j < particles.length; j++) { const q = particles[j]! const d = Math.hypot(p.x - q.x, p.y - q.y) if (d < 110) { ctx.beginPath() ctx.moveTo(p.x, p.y) ctx.lineTo(q.x, q.y) ctx.strokeStyle = `rgba(78,205,196,${0.16 * (1 - d / 110)})` ctx.stroke() } } }) raf = requestAnimationFrame(draw) } resize() draw() window.addEventListener('resize', resize) return () => { running = false cancelAnimationFrame(raf) window.removeEventListener('resize', resize) } }, []) const cfg = config const portals = PORTALS.filter((p) => isModuleEnabled(cfg, p.moduleKey)) const brand = cfg.copy.homeBrand || 'nomadro' const safe = useMemo(() => getSafeAreaMetrics(), []) const rootSafeStyle = { ['--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 return ( {isH5 ? : null} goPath('/pages/hub/index')} aria-label="返回首页"> {isH5 ? ( ) : ( )} {brand} {showQuickNav && isH5 && isDev ? ( openDevLink('admin')}> Admin openDevLink('config')}> Config ) : null} {isH5 ? ( ) : ( )} 🏝️ 巴厘岛 ☕ 里斯本 🏔️ 清迈 随处工作 自由生活 {portals.map((portal) => ( { trackClick(`入口-${portal.name}`, { module: portal.id }) goPath(INTERNAL_PORTALS[portal.id]) }} > {portal.emoji} {cfg?.modules[portal.moduleKey]?.title || portal.name} ))} ) }