From 153788327b087f32695313203f96573677c6ce95 Mon Sep 17 00:00:00 2001 From: Rogee Date: Tue, 22 Sep 2026 15:26:29 +0800 Subject: [PATCH] =?UTF-8?q?fix(web):=20=E4=BF=AE=E5=A4=8D=E5=B8=83?= =?UTF-8?q?=E5=B1=80=E6=B8=B2=E6=9F=93=E4=B8=8E=E5=85=A8=E5=B1=80=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=E5=AE=88=E5=8D=AB=E7=9A=84=E4=B8=89=E4=B8=AA=E7=9C=9F?= =?UTF-8?q?=E5=AE=9E=E7=BC=BA=E9=99=B7=EF=BC=88=E6=B5=8F=E8=A7=88=E5=99=A8?= =?UTF-8?q?=E5=AE=9E=E6=B5=8B=E5=8F=91=E7=8E=B0=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Layout 崩溃:Umi 4 约定式全局布局不注入 location prop(undefined 直接白屏报错),改用 useLocation() - 整页空白无报错:Umi 4 渲染器不传 children prop(RemoteComponent 以 createElement(Component, null) 渲染),布局必须自行渲染 - 认证守卫是死代码:onRouteChange 写在 access.ts 从未被调用(Umi 运行时配置只认 src/app.ts,且签名为 ({location}) 对象参数);移入 app.ts 并删除 access.ts --- web/src/access.ts | 15 --------------- web/src/app.ts | 16 ++++++++++++++++ web/src/layouts/index.tsx | 14 +++++++------- 3 files changed, 23 insertions(+), 22 deletions(-) delete mode 100644 web/src/access.ts create mode 100644 web/src/app.ts diff --git a/web/src/access.ts b/web/src/access.ts deleted file mode 100644 index 99da5fb..0000000 --- a/web/src/access.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { history } from '@umijs/max'; - -// 全局认证守卫(对齐 web.archived AuthGate 语义): -// - localStorage('creatorhub.auth') 缺失 → 一律重定向 /login -// - /login 已登录 → 重定向 /(避免重复登录) -export function onRouteChange(clientRoutes: unknown, location: { pathname: string }) { - const authed = localStorage.getItem('creatorhub.auth') !== null; - if (!authed && location.pathname !== '/login') { - history.replace('/login'); - return; - } - if (authed && location.pathname === '/login') { - history.replace('/'); - } -} diff --git a/web/src/app.ts b/web/src/app.ts new file mode 100644 index 0000000..f36b3a7 --- /dev/null +++ b/web/src/app.ts @@ -0,0 +1,16 @@ +// Umi 运行时配置:全局认证守卫(对齐 web.archived AuthGate 语义)。 +// onRouteChange 必须从 app.ts 导出才会被 Umi 调用;签名见 src/.umi/core/defineApp.ts。 +import { history } from '@umijs/max'; + +export function onRouteChange({ location }: { location: { pathname: string } }) { + const authed = localStorage.getItem('creatorhub.auth') !== null; + // - localStorage('creatorhub.auth') 缺失 → 一律重定向 /login + if (!authed && location.pathname !== '/login') { + history.replace('/login'); + return; + } + // - /login 已登录 → 重定向 /(避免重复登录) + if (authed && location.pathname === '/login') { + history.replace('/'); + } +} diff --git a/web/src/layouts/index.tsx b/web/src/layouts/index.tsx index 91aa8cd..4f14fa0 100644 --- a/web/src/layouts/index.tsx +++ b/web/src/layouts/index.tsx @@ -1,16 +1,16 @@ -import { useRef, useState } from 'react'; -import { history } from '@umijs/max'; +import { history, Outlet, useLocation } from '@umijs/max'; import { PageContainer, ProLayout } from '@ant-design/pro-components'; import { Dropdown } from 'antd'; import { LogoutOutlined } from '@ant-design/icons'; -import type { MenuDataItem } from '@ant-design/pro-components'; -import type { ReactNode } from 'react'; import { pageMetadata, menu } from '@/utils/metadata'; // 菜单结构与 web.archived/src/app/Layout.jsx 完全一致(运营 / 资源 两组),页头标题规则对齐 pageMetadataRules。 // /login 独立页:不走 ProLayout(对齐归档版布局分支)。 -export default function Layout({ children, location }: { children: ReactNode; location: { pathname: string } }) { - if (location.pathname === '/login') return <>{children}; +// Umi 4(react-router 6)全局布局:location 用 useLocation(),子路由用 渲染 +// (渲染器不传 children prop,用 children 会导致整页空白且无任何报错)。 +export default function Layout() { + const location = useLocation(); + if (location.pathname === '/login') return ; const metadata = pageMetadata(location.pathname); return ( @@ -61,7 +61,7 @@ export default function Layout({ children, location }: { children: ReactNode; lo }} > - {children} + );