fix(frontend): H5 领取前缀整段接管,/h5 裸链接不再跳后台登录
根因:公开路径判定原先只认「带参数」形态(isH5ClaimPath 只认 /h5/<orderNo>), 裸前缀 https://<域名>/h5(后台 claimLinkBase / certLink 的基址)判定不命中, 于是落回主站 HashRouter 被后台路由接管成 https://<域名>/h5/#/admin/dashboard 后台登录页。 - utils/claim-path.ts:isClaimPath / isH5ClaimPath 改为命名空间级判定(含裸前缀与多段路径), 参数解析仍严格;新增 resolveH5ClaimOrderNoFromInput(纯订单号 / 整条链接 / 缺协议 / 带后台残留 hash 都能取出订单号,只有前缀时返回空串) - pages/claim/H5ClaimEntryPage.tsx:新增 /h5 领取入口页,买家输订单号或粘贴完整链接后 跳 /h5/<订单号>,走与分享链接同一套领取流程 - App.tsx:H5ClaimApplication 接管 /h5 整段前缀(/h5 入口页、/h5/:orderNo 领取页、 其它子路径回入口页);useClearStrayHash 抹掉历史残留的 #/admin/dashboard - tests/claim-path.test.ts:补齐裸前缀、相邻前缀 /h5x、粘贴链接解析等回归用例 验证:npm run check 全绿(prettier + eslint + tsc + 143 单测 + vite build)
This commit is contained in:
@@ -39,6 +39,7 @@ const AdminUsersPage = lazy(() => import('@/pages/admin/AdminUsersPage'))
|
||||
const AdminDevMockPage = lazy(() => import('@/pages/admin/AdminDevMockPage'))
|
||||
const AdminWorkerPlatformPage = lazy(() => import('@/pages/admin/AdminWorkerPlatformPage'))
|
||||
const ClaimPage = lazy(() => import('@/pages/claim/ClaimPage'))
|
||||
const H5ClaimEntryPage = lazy(() => import('@/pages/claim/H5ClaimEntryPage'))
|
||||
const CollectPage = lazy(() => import('@/pages/collect/CollectPage'))
|
||||
const NotFoundPage = lazy(() => import('@/pages/NotFoundPage'))
|
||||
const RedeemPage = lazy(() => import('@/pages/redeem/RedeemPage'))
|
||||
@@ -103,7 +104,10 @@ export default function App() {
|
||||
return <ClaimApplication />
|
||||
}
|
||||
|
||||
// 路径式 H5 订单号领取链接(`https://<域名>/h5/<订单号>`,不带 `#`)
|
||||
// H5 领取入口(`https://<域名>/h5` 及其子路径,不带 `#`)。
|
||||
// 判定是**命名空间级**:`/h5` 本身(只有前缀、没有订单号)也算命中。
|
||||
// 否则 `https://<域名>/h5` 会落回主站 HashRouter,被后台路由接管成
|
||||
// `https://<域名>/h5/#/admin/dashboard` 后台登录页(买家点领取链接看到后台登录,历史缺陷)。
|
||||
if (isH5ClaimPath(window.location.pathname)) {
|
||||
return <H5ClaimApplication />
|
||||
}
|
||||
@@ -186,10 +190,13 @@ function RedeemApplication() {
|
||||
* 复用主站 HashRouter 里的**同一个** `ClaimPage`:它只依赖 `useParams` 取 token,
|
||||
* 内部不 navigate、也不读 hash,所以换成 BrowserRouter 承载不需要复制页面。
|
||||
*
|
||||
* 只在 `isClaimPath` 命中时才会挂载,所以这里的兜底路由正常走不到;
|
||||
* 真走到也复用同一个页面(token 为空时页面自己展示「链接无效」),不会出现路由空屏。
|
||||
* `isClaimPath` 是命名空间判定(`/claim` 及子路径都命中),所以必须保留兜底路由:
|
||||
* 只有前缀(`/claim`)或多段路径(`/claim/a/b`)时 `ClaimPage` 取不到 token,
|
||||
* 由页面自己展示「链接无效」,既不会路由空屏,也不会漏回后台路由。
|
||||
*/
|
||||
function ClaimApplication() {
|
||||
useClearStrayHash()
|
||||
|
||||
return (
|
||||
<BrowserRouter>
|
||||
<Suspense fallback={<RouteLoading />}>
|
||||
@@ -202,19 +209,46 @@ function ClaimApplication() {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* H5 领取入口(`/h5` 命名空间)的独立路由树。
|
||||
*
|
||||
* - `/h5`:只有前缀、没有订单号 → 入口页(买家补订单号 / 粘贴完整链接);
|
||||
* - `/h5/<订单号>`:与分享链接完全相同的领取流程;
|
||||
* - 其余子路径(如 `/h5/a/b`):没有订单号可领,回入口页重新输入,
|
||||
* 避免"前缀下面任何一条路径"漏回后台路由。
|
||||
*
|
||||
* 全程路径式(不带 `#`):后台 HashRouter 永远拿不到 `/h5` 这几个路径。
|
||||
*/
|
||||
function H5ClaimApplication() {
|
||||
useClearStrayHash()
|
||||
|
||||
return (
|
||||
<BrowserRouter>
|
||||
<Suspense fallback={<RouteLoading />}>
|
||||
<Routes>
|
||||
<Route path="/h5" element={<H5ClaimEntryPage />} />
|
||||
<Route path="/h5/:orderNo" element={<ClaimPage />} />
|
||||
<Route path="*" element={<ClaimPage />} />
|
||||
<Route path="*" element={<Navigate to="/h5" replace />} />
|
||||
</Routes>
|
||||
</Suspense>
|
||||
</BrowserRouter>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 公开领取页是**路径式**的、从不使用 hash 路由,但历史链接可能带着后台残留的 hash
|
||||
* (例如 `https://<域名>/h5#/admin/dashboard`)。
|
||||
* 挂载时把 hash 抹掉,避免买家在地址栏看到、并转发一条"看着像后台地址"的领取链接。
|
||||
*/
|
||||
function useClearStrayHash() {
|
||||
useEffect(() => {
|
||||
if (!window.location.hash) return
|
||||
|
||||
const { pathname, search } = window.location
|
||||
window.history.replaceState(window.history.state, '', `${pathname}${search}`)
|
||||
}, [])
|
||||
}
|
||||
|
||||
function WorkerApplication() {
|
||||
return (
|
||||
<BrowserRouter>
|
||||
|
||||
Reference in New Issue
Block a user