/** * 电子书 H5 封面 — 对齐门户首页 / 社群首页视觉语言 * (CommunityNavbar + hero shapes + gradient + typewriter + 固定 CTA) * 内容 SoT: COVER_BOOK / nomadCodeBook;阅读入口 → /pages/reader */ import { useEffect, useMemo, useRef, useState } from 'react' import { Button } from '@tarojs/components' import Taro, { useDidShow, useLoad } from '@tarojs/taro' import { fetchBook, type BookDetail } from '../../services/ebookApi' import { COVER_BOOK } from '../../data/coverBook' import { formatAgo, getReadingProgress, type ReadingProgress, } from '../../utils/readingProgress' import { track, trackClick, trackPage } from '../../utils/track' import { MiniModuleGate } from '@/components/MiniModuleGate' import { CommunityNavbar } from '../../components/Chrome' import { H5Box, H5Txt } from '../../components/H5Box' import { applyH5Theme, useNomadroTheme } from '../../utils/h5Theme' import { goPage } from '@/utils/nav' import { getSafeAreaMetrics } from '../../utils/safeArea' import { getCachedRemoteConfig, loadRemoteConfig, subscribeRemoteConfig, } from '@/services/remoteConfig' import { resolveBookAudit } from '@/data/remoteConfig.defaults' import './index.scss' const isH5 = process.env.TARO_ENV === 'h5' const BOOK_VISUAL_SVG = `` function coverAsBookDetail(): BookDetail { return { id: 'default', title: COVER_BOOK.title, subtitle: COVER_BOOK.subtitle, author: COVER_BOOK.author, cover_emoji: '📖', description: COVER_BOOK.description, tags: [...COVER_BOOK.viralTags], price_fen: 0, product_id: 'ebook_default', unlock_percent: 100, is_free: true, chapter_count: COVER_BOOK.chapters.length, word_count: 0, rating: 0, chapters: COVER_BOOK.chapters.map((ch, i) => ({ id: ch.slug, title: ch.title, order: i + 1, })), } } const HOOK_LINES = COVER_BOOK.hooks.map((h) => `${h.emoji} ${h.text}`) const CHIP_TAGS = COVER_BOOK.viralTags.slice(0, 3) function BookPageInner() { const theme = useNomadroTheme() // 封面视觉:正式 COVER_BOOK;提审时用线上 audit 书名(失联才兜底文案) const [book, setBook] = useState(coverAsBookDetail()) const [apiBook, setApiBook] = useState(null) const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [progress, setProgress] = useState(null) const [tw, setTw] = useState('') const [audit, setAudit] = useState(() => resolveBookAudit(getCachedRemoteConfig())) const auditKeyRef = useRef(`${audit.reviewMode}:${audit.bookId}`) useLoad(() => { trackPage('pages/book/index', '小册封面') loadBook() }) useDidShow(() => { void loadRemoteConfig(true).then((r) => { applyAudit(resolveBookAudit(r.config)) }) loadBook(true) }) useEffect(() => { return subscribeRemoteConfig((r) => { applyAudit(resolveBookAudit(r.config)) }) }, []) useEffect(() => { applyH5Theme(theme) }, [theme]) function applyAudit(next: ReturnType) { const key = `${next.reviewMode}:${next.bookId}` const changed = key !== auditKeyRef.current auditKeyRef.current = key setAudit(next) const pid = next.reviewMode ? next.bookId || 'audit' : 'nomad' setProgress(getReadingProgress(pid)) if (changed) { void loadBook(true) } } // 对齐正式封面:hooks 轮播打字机(提审同样跑) useEffect(() => { if (loading) return const phrases = HOOK_LINES.length ? HOOK_LINES : [COVER_BOOK.subtitle] let phraseIdx = 0 let charIdx = 0 let deleting = false let timer: ReturnType const type = () => { const current = phrases[phraseIdx] || '' if (!deleting) { setTw(current.substring(0, charIdx + 1)) charIdx++ if (charIdx === current.length) { deleting = true timer = setTimeout(type, 2600) return } timer = setTimeout(type, 42) } else { setTw(current.substring(0, charIdx - 1)) charIdx-- if (charIdx === 0) { deleting = false phraseIdx = (phraseIdx + 1) % phrases.length } timer = setTimeout(type, 22) } } timer = setTimeout(type, 380) return () => clearTimeout(timer) }, [loading]) async function loadBook(silent = false) { setError('') const a = resolveBookAudit(getCachedRemoteConfig()) setAudit(a) auditKeyRef.current = `${a.reviewMode}:${a.bookId}` // 正式:封面品牌视觉;提审:立刻换成审核版书名/章节,保证开关可见 if (!a.reviewMode) { setBook(coverAsBookDetail()) setApiBook(null) } try { if (!silent) setLoading(true) const bookId = a.reviewMode ? a.bookId || 'audit' : 'nomad' const data = await fetchBook(bookId) setApiBook(data) if (a.reviewMode && data) { setBook({ ...coverAsBookDetail(), id: data.id, title: data.title || '数字游民入门指南', subtitle: data.subtitle || '远程办公 · 自律生活 · 稳健成长', description: data.description || coverAsBookDetail().description, chapters: data.chapters?.length ? data.chapters : coverAsBookDetail().chapters, is_free: true, unlock_percent: 100, }) } } catch (e: any) { if (a.reviewMode) { setBook({ ...coverAsBookDetail(), id: a.bookId || 'audit', title: '数字游民入门指南', subtitle: '远程办公 · 自律生活 · 稳健成长', is_free: true, unlock_percent: 100, }) } track('error', { page: 'pages/book/index', label: 'load_book', props: { msg: String(e.message || e) }, }) } finally { if (!silent) setLoading(false) } } const continueChapter = useMemo(() => { const chapters = apiBook?.chapters?.length ? apiBook.chapters : book?.chapters if (!chapters || !progress?.chapterId) return null const found = chapters.find((c) => String(c.id) === String(progress.chapterId)) if (found) return found return { id: progress.chapterId, title: `第 ${progress.chapterIndex + 1} 章` } }, [book, apiBook, progress]) // 必须在一切 early-return 之前:reviewMode 冷启动 false→true 时否则 hooks 数量变化 → 黑屏 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`, }), [safe], ) function goRead() { const a = resolveBookAudit(getCachedRemoteConfig()) // 提审 / 正式同一套 vip-ebook 阅读壳(样式一致);提审只换正文来源 const bookId = a.reviewMode ? a.bookId || 'audit' : 'nomad' const continuing = !!progress?.chapterId trackClick(continuing ? '继续阅读' : '开始阅读', { book_id: bookId, has_progress: continuing, audit: a.reviewMode, }) track('read_start', { page: 'pages/book/index', label: continuing ? '继续阅读' : '开始阅读', props: { book_id: bookId, audit: a.reviewMode }, }) goPage('/pages/vip-ebook/index?id=nomad') } if (loading && !book) { return ( 加载中… ) } if (error || !book) { return ( 暂时打不开 {error || '请稍后再试'} ) } const hasProgress = !!continueChapter const unlockSource = apiBook || book const isFree = !!(unlockSource.is_free || (unlockSource.unlock_percent ?? 0) >= 100) const ctaText = hasProgress ? '📖 继续阅读' : '📖 开始阅读' const ctaHint = hasProgress ? progress?.updatedAt ? `上次翻到 · ${formatAgo(progress.updatedAt)}` : '接着上次的地方' : isFree ? '点一下,直接进入正文' : '先翻开前面的章节' const displayTitle = audit.reviewMode ? book.title || '数字游民入门指南' : COVER_BOOK.title const tagParts = [ audit.reviewMode ? book.subtitle || '远程办公 · 自律生活 · 稳健成长' : book.subtitle || COVER_BOOK.subtitle || '低成本工作室', ] return ( {displayTitle} {tagParts[0]} ✨ {tw} | {isH5 ? ( ) : ( 📖 )} {CHIP_TAGS.map((tag, i) => ( {i === 0 ? '🏷 ' : i === 1 ? '⚡ ' : '🌐 '} {tag} ))} {ctaHint} ) } /** 正式 / 提审同一封面壳;点阅读都进 vip-ebook(提审换线上 audit 正文) */ export default function BookCoverPage() { useDidShow(() => { void loadRemoteConfig(true) // 预下载阅读壳,避免提审点开黑屏 try { void Taro.loadSubPackage?.({ name: 'vip-ebook' }) } catch { /* ignore */ } }) return ( ) }