Restructure /wechat nav (VIP submenu for ebooks/course/shop/memo), rebuild ops pages with shared wx UI, and wire vip-library remote content end-to-end with README paths. Co-authored-by: Cursor <cursoragent@cursor.com>
263 lines
7.5 KiB
TypeScript
263 lines
7.5 KiB
TypeScript
import { useEffect, useMemo, useState } from 'react'
|
||
import { NavLink, Outlet, useLocation, useNavigate } from 'react-router-dom'
|
||
import { clearToken } from '../api'
|
||
import '../pages/wechat.css'
|
||
|
||
type NavLeaf = { kind?: 'link'; to: string; end?: boolean; ico: string; label: string }
|
||
type NavBranch = {
|
||
kind: 'branch'
|
||
id: string
|
||
ico: string
|
||
label: string
|
||
/** 任一前缀命中则父级高亮并保持展开 */
|
||
match: string[]
|
||
children: NavLeaf[]
|
||
}
|
||
type NavEntry = NavLeaf | NavBranch
|
||
type NavGroup = { label: string; items: NavEntry[] }
|
||
|
||
const NAV_GROUPS: NavGroup[] = [
|
||
{
|
||
label: '总览',
|
||
items: [{ to: '/wechat', end: true, ico: '◈', label: '运营概览' }],
|
||
},
|
||
{
|
||
label: '内容',
|
||
items: [
|
||
{ to: '/wechat/nav', ico: '🧭', label: '导航' },
|
||
{ to: '/wechat/books/default', ico: '📕', label: '小册与试读%' },
|
||
{
|
||
kind: 'branch',
|
||
id: 'vip',
|
||
ico: '👑',
|
||
label: 'VIP',
|
||
match: ['/wechat/vip', '/wechat/shop', '/wechat/memo'],
|
||
children: [
|
||
{ to: '/wechat/vip', end: true, ico: '⚙', label: '运营配置' },
|
||
{ to: '/wechat/vip/ebooks', ico: '📖', label: '电子书' },
|
||
{ to: '/wechat/vip/course', ico: '🎬', label: '视频课程' },
|
||
{ to: '/wechat/shop', ico: '🛒', label: '电商' },
|
||
{ to: '/wechat/memo', ico: '🏕️', label: '知识社群' },
|
||
],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
label: '运营',
|
||
items: [
|
||
{ to: '/wechat/users', ico: '👥', label: '读者' },
|
||
{ to: '/wechat/orders', ico: '🧾', label: '订单' },
|
||
],
|
||
},
|
||
{
|
||
label: '数据与发布',
|
||
items: [
|
||
{ to: '/wechat/events', ico: '📊', label: '埋点事件' },
|
||
{ to: '/wechat/checklist', ico: '✓', label: '部署检查清单' },
|
||
],
|
||
},
|
||
]
|
||
|
||
function flattenLeaves(groups: NavGroup[]): NavLeaf[] {
|
||
const out: NavLeaf[] = []
|
||
for (const g of groups) {
|
||
for (const item of g.items) {
|
||
if (item.kind === 'branch') out.push(...item.children)
|
||
else out.push(item)
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
const FLAT_NAV = flattenLeaves(NAV_GROUPS)
|
||
|
||
function titleFromPath(pathname: string) {
|
||
const sorted = [...FLAT_NAV].sort((a, b) => b.to.length - a.to.length)
|
||
const hit = sorted.find((n) => (n.end ? pathname === n.to : pathname.startsWith(n.to)))
|
||
return hit?.label || '微信后台'
|
||
}
|
||
|
||
function isBranchActive(branch: NavBranch, pathname: string) {
|
||
return branch.match.some(
|
||
(m) => pathname === m || pathname.startsWith(`${m}/`),
|
||
)
|
||
}
|
||
|
||
/** 微信小程序后台壳层 — 仅 /wechat/*;VIP 二级菜单;移动端抽屉 */
|
||
export default function Layout() {
|
||
const nav = useNavigate()
|
||
const { pathname } = useLocation()
|
||
const [drawerOpen, setDrawerOpen] = useState(false)
|
||
const vipActive = useMemo(
|
||
() =>
|
||
pathname === '/wechat/vip' ||
|
||
pathname.startsWith('/wechat/vip/') ||
|
||
pathname.startsWith('/wechat/shop') ||
|
||
pathname.startsWith('/wechat/memo'),
|
||
[pathname],
|
||
)
|
||
const [vipOpen, setVipOpen] = useState(() => vipActive)
|
||
|
||
useEffect(() => {
|
||
setDrawerOpen(false)
|
||
}, [pathname])
|
||
|
||
useEffect(() => {
|
||
if (vipActive) setVipOpen(true)
|
||
}, [vipActive])
|
||
|
||
useEffect(() => {
|
||
document.body.style.overflow = drawerOpen ? 'hidden' : ''
|
||
return () => {
|
||
document.body.style.overflow = ''
|
||
}
|
||
}, [drawerOpen])
|
||
|
||
function logout() {
|
||
clearToken()
|
||
nav('/login')
|
||
}
|
||
|
||
function renderEntry(item: NavEntry) {
|
||
if (item.kind === 'branch') {
|
||
const open = item.id === 'vip' ? vipOpen : false
|
||
const active = isBranchActive(item, pathname)
|
||
return (
|
||
<div
|
||
key={item.id}
|
||
className={`wx-nav-branch${open ? ' is-open' : ''}${active ? ' is-active' : ''}`}
|
||
>
|
||
<button
|
||
type="button"
|
||
className="wx-nav-parent"
|
||
aria-expanded={open}
|
||
onClick={() => {
|
||
if (item.id === 'vip') {
|
||
setVipOpen((v) => {
|
||
const next = !v
|
||
if (next && !vipActive) nav('/wechat/vip')
|
||
return next
|
||
})
|
||
}
|
||
}}
|
||
>
|
||
<span className="wx-nav-ico" aria-hidden>
|
||
{item.ico}
|
||
</span>
|
||
<span className="wx-nav-parent-label">{item.label}</span>
|
||
<span className="wx-nav-caret" aria-hidden>
|
||
{open ? '▾' : '▸'}
|
||
</span>
|
||
</button>
|
||
{open ? (
|
||
<div className="wx-nav-sub" role="group" aria-label={`${item.label}子菜单`}>
|
||
{item.children.map((child) => (
|
||
<NavLink
|
||
key={child.to}
|
||
to={child.to}
|
||
end={!!child.end}
|
||
className="wx-nav-sublink"
|
||
onClick={() => setDrawerOpen(false)}
|
||
>
|
||
<span className="wx-nav-ico" aria-hidden>
|
||
{child.ico}
|
||
</span>
|
||
{child.label}
|
||
</NavLink>
|
||
))}
|
||
</div>
|
||
) : null}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
return (
|
||
<NavLink
|
||
key={item.to}
|
||
to={item.to}
|
||
end={!!item.end}
|
||
onClick={() => setDrawerOpen(false)}
|
||
>
|
||
<span className="wx-nav-ico" aria-hidden>
|
||
{item.ico}
|
||
</span>
|
||
{item.label}
|
||
</NavLink>
|
||
)
|
||
}
|
||
|
||
return (
|
||
<div className={`wx-shell${drawerOpen ? ' wx-shell--nav-open' : ''}`}>
|
||
<header className="wx-topbar">
|
||
<button
|
||
type="button"
|
||
className="wx-topbar-menu"
|
||
aria-label={drawerOpen ? '关闭菜单' : '打开菜单'}
|
||
aria-expanded={drawerOpen}
|
||
onClick={() => setDrawerOpen((v) => !v)}
|
||
>
|
||
<span />
|
||
<span />
|
||
<span />
|
||
</button>
|
||
<div className="wx-topbar-brand">
|
||
<img src="/favicon-32x32.png" width={28} height={28} alt="" />
|
||
<div>
|
||
<div className="wx-topbar-title">微信后台</div>
|
||
<div className="wx-topbar-sub">{titleFromPath(pathname)}</div>
|
||
</div>
|
||
</div>
|
||
<button type="button" className="wx-topbar-action" onClick={() => nav('/')}>
|
||
平台
|
||
</button>
|
||
</header>
|
||
|
||
{drawerOpen && (
|
||
<button
|
||
type="button"
|
||
className="wx-nav-scrim"
|
||
aria-label="关闭菜单"
|
||
onClick={() => setDrawerOpen(false)}
|
||
/>
|
||
)}
|
||
|
||
<aside className="wx-side" aria-hidden={!drawerOpen}>
|
||
<div className="wx-logo">
|
||
<img src="/apple-touch-icon.png" width={42} height={42} alt="" />
|
||
<div>
|
||
<div className="wx-title">微信后台</div>
|
||
<div className="wx-sub">小程序 · 虚拟支付运营</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="wx-pill">
|
||
<span className="wx-pill-dot" />
|
||
与抖音数据隔离
|
||
</div>
|
||
|
||
<nav className="wx-nav" aria-label="微信后台导航">
|
||
{NAV_GROUPS.map((group) => (
|
||
<div className="wx-nav-group" key={group.label}>
|
||
<div className="wx-nav-label">{group.label}</div>
|
||
{group.items.map((item) => renderEntry(item))}
|
||
</div>
|
||
))}
|
||
</nav>
|
||
|
||
<div className="wx-side-foot">
|
||
<button type="button" className="wx-ghost" onClick={() => nav('/')}>
|
||
← 切换平台
|
||
</button>
|
||
<button type="button" className="wx-ghost" onClick={logout}>
|
||
退出登录
|
||
</button>
|
||
</div>
|
||
</aside>
|
||
|
||
<main className="wx-main">
|
||
<Outlet />
|
||
</main>
|
||
</div>
|
||
)
|
||
}
|