From d6b4f7531ec5a230fea1e79918b3e5fca749e24e Mon Sep 17 00:00:00 2001 From: mackt <1033530438@qq.com> Date: Sun, 28 Apr 2024 15:05:51 +0800 Subject: [PATCH] =?UTF-8?q?update(redirect):=20=E6=9C=9F=E5=88=8A=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E8=B7=B3=E8=BD=AC=E7=A7=BB=E5=8A=A8=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/vol/[journalId]/page.tsx | 14 ++++++++++++-- src/middleware.ts | 13 +++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) 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*'], };