diff --git a/src/app/vol/[journalId]/page.tsx b/src/app/vol/[journalId]/page.tsx index 82dc51c..067b3b3 100644 --- a/src/app/vol/[journalId]/page.tsx +++ b/src/app/vol/[journalId]/page.tsx @@ -1,5 +1,5 @@ /** 期刊详情 */ -import { notFound } from 'next/navigation'; +import { notFound, redirect, RedirectType } from 'next/navigation'; import { SongCardList, JournalRecommendList, Comment, CollectButton, VolDetailCoverCard } from '@/components'; import { apiGetJournalInfoById, apiGetSongsByJournalNo, apiGetJournalRecommendById } from '@/services'; @@ -20,9 +20,19 @@ const getData = async (journalId: string) => { return { journalInfo: journalInfoRes.data, songList: songListRes.data, recommendList: recommendList.data }; }; -export default async function JournalDetail({ params: { journalId } }: { params: { journalId: string } }) { +export default async function JournalDetail({ + params: { journalId }, + searchParams: { mobile }, +}: { + params: { journalId: string }; + searchParams: { mobile: string }; +}) { const { journalInfo, songList, recommendList } = await getData(journalId); + if (mobile) { + redirect(`${process.env.NEXT_PUBLIC_MOBILE_HOST}/html/journal.html?id=${journalInfo.id}`, RedirectType.replace); + } + return (
diff --git a/src/middleware.ts b/src/middleware.ts index c62aad1..b66afbc 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -3,13 +3,18 @@ import { NextResponse, userAgent } from 'next/server'; import type { NextRequest } from 'next/server'; export async function middleware(request: NextRequest) { - const { pathname, origin, basePath } = request.nextUrl; + const { href, pathname, searchParams, origin, basePath } = request.nextUrl; + + if (searchParams.get('next')) { + return NextResponse.next(); + } // 跳转移动端 const { device } = userAgent(request); const isMobile = device.type && ['mobile', 'tablet'].includes(device.type); - if (isMobile) { - return NextResponse.redirect(`${process.env.NEXT_PUBLIC_MOBILE_HOST}/index.html`); + if (isMobile && request.nextUrl.pathname.startsWith('/vol')) { + const redirectUrl: URL = new URL(`${href}?next=1&mobile=1`); + return NextResponse.redirect(redirectUrl, 307); } // 鉴权 @@ -34,5 +39,5 @@ export async function middleware(request: NextRequest) { } export const config = { - matcher: ['/:path*'], + matcher: ['/vol/:path*', '/my/:path*'], };