Keep 20261011 hub visuals on home; index only reLaunches to avoid missing page wxss.
Co-authored-by: Cursor <cursoragent@cursor.com>
431 lines
14 KiB
TypeScript
431 lines
14 KiB
TypeScript
/**
|
||
* 对齐门户 components/sections/hero.tsx(结构 / PORTALS / 开发快捷入口)
|
||
* H5 真布局用原生 div(Taro View 自定义元素会弄塌 grid/绝对定位)。
|
||
*/
|
||
import { useEffect, useState, type CSSProperties, type ReactNode } from 'react'
|
||
import { View, Text } from '@tarojs/components'
|
||
import Taro, { useDidShow, usePageScroll } from '@tarojs/taro'
|
||
import {
|
||
isModuleEnabled,
|
||
loadRemoteConfig,
|
||
subscribeRemoteConfig,
|
||
} from '@/services/remoteConfig'
|
||
import type { NomadroRemoteConfig } from '@/data/remoteConfig.defaults'
|
||
import { applyH5Theme, useNomadroTheme } from '@/utils/h5Theme'
|
||
import { goPage } from '@/utils/nav'
|
||
import { usePageShare, markPageShare } from '@/utils/pageShare'
|
||
import { safePx, useSafeAreaMetrics } from '@/utils/safeArea'
|
||
import { usePageTrack } from '@/hooks/usePageTrack'
|
||
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<PortalId, string> = {
|
||
nav: '/pages/nav/index',
|
||
book: '/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 = `<svg class="hub-logo-icon" 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 GLOBE_SVG = `<svg class="hub-globe-svg" viewBox="0 0 400 400" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
|
||
<defs>
|
||
<radialGradient id="hubGlobeGlow" cx="50%" cy="50%" r="50%">
|
||
<stop offset="0%" stop-color="#4ECDC4" stop-opacity="0.3"/>
|
||
<stop offset="100%" stop-color="#4ECDC4" stop-opacity="0"/>
|
||
</radialGradient>
|
||
<linearGradient id="hubOrbitGrad" x1="0%" y1="0%" x2="100%" y2="100%">
|
||
<stop offset="0%" stop-color="#FF6B6B"/>
|
||
<stop offset="100%" stop-color="#4ECDC4"/>
|
||
</linearGradient>
|
||
</defs>
|
||
<circle cx="200" cy="200" r="160" fill="url(#hubGlobeGlow)"/>
|
||
<circle cx="200" cy="200" r="120" fill="none" stroke="rgba(78,205,196,0.2)" stroke-width="1"/>
|
||
<circle cx="200" cy="200" r="90" fill="none" stroke="rgba(78,205,196,0.15)" stroke-width="1"/>
|
||
<ellipse class="hub-orbit-ring" cx="200" cy="200" rx="120" ry="40" fill="none" stroke="rgba(78,205,196,0.3)" stroke-width="1.5"/>
|
||
<ellipse class="hub-orbit-ring hub-orbit-ring-2" cx="200" cy="200" rx="40" ry="120" fill="none" stroke="rgba(255,107,107,0.3)" stroke-width="1.5"/>
|
||
<path class="hub-continent" d="M160 140 Q180 130 200 135 Q220 128 240 140 Q250 155 245 170 Q235 185 220 190 Q200 195 185 188 Q165 180 160 165 Z" fill="rgba(78,205,196,0.4)"/>
|
||
<path class="hub-continent" d="M130 210 Q145 200 160 215 Q155 235 140 240 Q125 235 130 210 Z" fill="rgba(78,205,196,0.3)"/>
|
||
<path class="hub-continent" d="M250 200 Q270 195 280 210 Q275 230 260 235 Q245 228 250 200 Z" fill="rgba(78,205,196,0.35)"/>
|
||
<circle class="hub-pulse-dot" cx="180" cy="155" r="5" fill="#FF6B6B"/>
|
||
<circle class="hub-pulse-dot" cx="260" cy="215" r="5" fill="#FFE66D"/>
|
||
<circle class="hub-pulse-dot" cx="145" cy="225" r="5" fill="#4ECDC4"/>
|
||
<path class="hub-flight-path" d="M180 155 Q220 140 260 215" fill="none" stroke="url(#hubOrbitGrad)" stroke-width="2" stroke-dasharray="6 4"/>
|
||
</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 (
|
||
<div
|
||
id={id}
|
||
className={className}
|
||
style={style}
|
||
onClick={onClick}
|
||
aria-hidden={ariaHidden}
|
||
aria-label={ariaLabel}
|
||
data-theme={dataTheme}
|
||
dangerouslySetInnerHTML={html ? { __html: html } : undefined}
|
||
>
|
||
{html ? undefined : children}
|
||
</div>
|
||
)
|
||
}
|
||
return (
|
||
<View
|
||
id={id}
|
||
className={className}
|
||
style={style}
|
||
onClick={onClick}
|
||
data-theme={dataTheme}
|
||
dangerouslySetInnerHTML={html ? { __html: html } : undefined}
|
||
>
|
||
{html ? undefined : children}
|
||
</View>
|
||
)
|
||
}
|
||
|
||
function Txt({ className, children }: { className?: string; children?: ReactNode }) {
|
||
if (isH5) return <span className={className}>{children}</span>
|
||
return <Text className={className}>{children}</Text>
|
||
}
|
||
|
||
function IndexPage() {
|
||
usePageTrack('pages/home/index', '首页')
|
||
usePageShare({ title: 'nomadro · 随处工作,自由生活', path: '/pages/home/index' })
|
||
|
||
const [config, setConfig] = useState<NomadroRemoteConfig | null>(null)
|
||
const theme = useNomadroTheme()
|
||
const [navScrolled, setNavScrolled] = useState(false)
|
||
|
||
useEffect(() => {
|
||
const unsub = subscribeRemoteConfig((r) => setConfig(r.config))
|
||
void loadRemoteConfig(true).then((r) => setConfig(r.config))
|
||
applyH5Theme(theme)
|
||
return unsub
|
||
}, [])
|
||
|
||
useDidShow(() => {
|
||
void loadRemoteConfig(true).then((r) => setConfig(r.config))
|
||
})
|
||
|
||
useEffect(() => {
|
||
applyH5Theme(theme)
|
||
}, [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 = cfg ? PORTALS.filter((p) => isModuleEnabled(cfg, p.moduleKey)) : []
|
||
const brand = cfg?.copy.homeBrand || 'nomadro'
|
||
const safe = useSafeAreaMetrics()
|
||
const rootSafeStyle = {
|
||
// CSS 变量用小写 px(样式表已排除 rpx 转换);内联尺寸必须用 safePx
|
||
['--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 (
|
||
<Box
|
||
className={`hub-root${theme === 'light' ? ' hub-root--light' : ''}${!isH5 ? ' hub-root--mini' : ''}`}
|
||
data-theme={theme}
|
||
style={rootSafeStyle}
|
||
>
|
||
{isH5 ? <Box id="particle-canvas-host" className="hub-particle-canvas" /> : null}
|
||
|
||
<Box
|
||
className={`hub-navbar${navScrolled ? ' scrolled' : ''}`}
|
||
id="navbar"
|
||
style={
|
||
!isH5
|
||
? {
|
||
paddingTop: safePx(safe.statusBarHeight),
|
||
height: 'auto',
|
||
}
|
||
: undefined
|
||
}
|
||
>
|
||
<Box
|
||
className="hub-nav-container"
|
||
style={
|
||
!isH5
|
||
? {
|
||
height: safePx(safe.navBarHeight),
|
||
paddingRight: safePx(safe.menuRightPad),
|
||
}
|
||
: undefined
|
||
}
|
||
>
|
||
<Box className="hub-logo">
|
||
{isH5 ? (
|
||
<Box className="hub-logo-svg" html={LOGO_SVG} />
|
||
) : (
|
||
<Box className="hub-logo-mark" aria-hidden>
|
||
<Box className="hub-logo-mark-ring" />
|
||
<Box className="hub-logo-mark-dot" />
|
||
</Box>
|
||
)}
|
||
<Txt className="hub-logo-text">{brand}</Txt>
|
||
</Box>
|
||
<Box className="hub-nav-actions">
|
||
{showQuickNav && isH5 && isDev ? (
|
||
<Box className="hub-dev-nav">
|
||
<Box className="hub-dev-link" onClick={() => openDevLink('admin')}>
|
||
<Txt>Admin</Txt>
|
||
</Box>
|
||
<Box className="hub-dev-link" onClick={() => openDevLink('config')}>
|
||
<Txt>Config</Txt>
|
||
</Box>
|
||
</Box>
|
||
) : null}
|
||
</Box>
|
||
</Box>
|
||
</Box>
|
||
|
||
<Box
|
||
className="hub-hero"
|
||
style={
|
||
!isH5
|
||
? {
|
||
paddingTop: safePx(safe.headerHeight + 8),
|
||
}
|
||
: undefined
|
||
}
|
||
>
|
||
<Box className="hub-hero-bg-shapes" aria-hidden>
|
||
<Box className="hub-shape hub-shape-1" />
|
||
<Box className="hub-shape hub-shape-2" />
|
||
<Box className="hub-shape hub-shape-3" />
|
||
</Box>
|
||
|
||
<Box className="hub-hero-inner">
|
||
<Box className="hub-hero-visual hub-reveal visible">
|
||
<Box className="hub-globe-container">
|
||
{isH5 ? (
|
||
<Box className="hub-globe-wrap" html={GLOBE_SVG} />
|
||
) : (
|
||
<Box className="hub-globe-css" aria-hidden>
|
||
<Box className="hub-globe-css-glow" />
|
||
<Box className="hub-globe-css-ring hub-globe-css-ring-a" />
|
||
<Box className="hub-globe-css-ring hub-globe-css-ring-b" />
|
||
<Box className="hub-globe-css-core" />
|
||
<Box className="hub-globe-css-dot hub-globe-css-dot-1" />
|
||
<Box className="hub-globe-css-dot hub-globe-css-dot-2" />
|
||
<Box className="hub-globe-css-dot hub-globe-css-dot-3" />
|
||
<Box className="hub-globe-css-path" />
|
||
</Box>
|
||
)}
|
||
<Box className="hub-floating-cards" aria-hidden>
|
||
<Box className="hub-float-card hub-fc-1">
|
||
<Txt>🏝️ 巴厘岛</Txt>
|
||
</Box>
|
||
<Box className="hub-float-card hub-fc-2">
|
||
<Txt>☕ 里斯本</Txt>
|
||
</Box>
|
||
<Box className="hub-float-card hub-fc-3">
|
||
<Txt>🏔️ 清迈</Txt>
|
||
</Box>
|
||
</Box>
|
||
</Box>
|
||
</Box>
|
||
|
||
<Box className="hub-hero-title hub-reveal visible">
|
||
<Txt>随处工作</Txt>
|
||
<Txt className="hub-gradient-text">自由生活</Txt>
|
||
</Box>
|
||
|
||
<Box className="hub-portal-grid hub-reveal visible">
|
||
{portals.map((portal) => (
|
||
<Box
|
||
key={portal.id}
|
||
className={`hub-portal-card${portal.main ? ' hub-portal-card--main' : ''}`}
|
||
onClick={() => goPath(INTERNAL_PORTALS[portal.id])}
|
||
>
|
||
<Box className="hub-portal-ico" aria-hidden>
|
||
<Txt>{portal.emoji}</Txt>
|
||
</Box>
|
||
<Txt className="hub-portal-name">
|
||
{cfg?.modules[portal.moduleKey]?.title || portal.name}
|
||
</Txt>
|
||
</Box>
|
||
))}
|
||
</Box>
|
||
</Box>
|
||
</Box>
|
||
</Box>
|
||
)
|
||
}
|
||
|
||
export default markPageShare(IndexPage)
|