From 9641acbcbda15fb75886a90534b7777841344bdd Mon Sep 17 00:00:00 2001 From: kurihada Date: Thu, 26 Feb 2026 19:31:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8F=90=E5=8F=96=20buildNavUrl=20?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0=EF=BC=8C=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=20room=20API=20=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 MatchResult.tsx 和 profile/page.tsx 中重复的导航 URL 构建逻辑提取到 src/lib/navigation.ts - 7 个 room API 路由从 return NextResponse.json({ error }, { status }) 统一改为 throw ApiError,由 apiHandler 统一捕获 --- src/app/api/room/[id]/join/route.ts | 7 +------ src/app/api/room/[id]/manage/route.ts | 7 +------ src/app/api/room/[id]/reset/route.ts | 9 ++------- src/app/api/room/[id]/route.ts | 10 ++-------- src/app/api/room/[id]/swipe/route.ts | 7 +------ src/app/api/room/[id]/undo/route.ts | 7 +------ src/app/api/room/create/route.ts | 7 +------ src/app/profile/page.tsx | 7 ++----- src/components/MatchResult.tsx | 9 +-------- src/lib/navigation.ts | 9 +++++++++ 10 files changed, 21 insertions(+), 58 deletions(-) create mode 100644 src/lib/navigation.ts diff --git a/src/app/api/room/[id]/join/route.ts b/src/app/api/room/[id]/join/route.ts index 230357e..7f7c96b 100644 --- a/src/app/api/room/[id]/join/route.ts +++ b/src/app/api/room/[id]/join/route.ts @@ -22,12 +22,7 @@ export const POST = apiHandler(async (req, { params }) => { return data; }); - if (!updated) { - return NextResponse.json( - { error: "房间不存在或已过期" }, - { status: 404 }, - ); - } + if (!updated) throw new ApiError("房间不存在或已过期", 404); notify(id); diff --git a/src/app/api/room/[id]/manage/route.ts b/src/app/api/room/[id]/manage/route.ts index b2b76b3..a806676 100644 --- a/src/app/api/room/[id]/manage/route.ts +++ b/src/app/api/room/[id]/manage/route.ts @@ -59,12 +59,7 @@ export const POST = apiHandler(async (req, { params }) => { return data; }); - if (!updated) { - return NextResponse.json( - { error: "房间不存在或已过期" }, - { status: 404 }, - ); - } + if (!updated) throw new ApiError("房间不存在或已过期", 404); notify(id); diff --git a/src/app/api/room/[id]/reset/route.ts b/src/app/api/room/[id]/reset/route.ts index e02a9d2..1570906 100644 --- a/src/app/api/room/[id]/reset/route.ts +++ b/src/app/api/room/[id]/reset/route.ts @@ -1,7 +1,7 @@ import { NextResponse } from "next/server"; import { atomicUpdateRoom } from "@/lib/store"; import { notify } from "@/lib/roomEvents"; -import { apiHandler } from "@/lib/api"; +import { apiHandler, ApiError } from "@/lib/api"; export const POST = apiHandler(async (req, { params }) => { const { id } = await params; @@ -27,12 +27,7 @@ export const POST = apiHandler(async (req, { params }) => { return data; }); - if (!updated) { - return NextResponse.json( - { error: "房间不存在或已过期" }, - { status: 404 }, - ); - } + if (!updated) throw new ApiError("房间不存在或已过期", 404); notify(id); diff --git a/src/app/api/room/[id]/route.ts b/src/app/api/room/[id]/route.ts index c7a309d..cda1d39 100644 --- a/src/app/api/room/[id]/route.ts +++ b/src/app/api/room/[id]/route.ts @@ -1,18 +1,12 @@ import { NextResponse } from "next/server"; import { buildRoomStatus } from "@/lib/buildRoomStatus"; -import { apiHandler } from "@/lib/api"; +import { apiHandler, ApiError } from "@/lib/api"; export const GET = apiHandler(async (_req, { params }) => { const { id } = await params; const status = await buildRoomStatus(id); - - if (!status) { - return NextResponse.json( - { error: "房间不存在或已过期" }, - { status: 404 }, - ); - } + if (!status) throw new ApiError("房间不存在或已过期", 404); return NextResponse.json(status); }); diff --git a/src/app/api/room/[id]/swipe/route.ts b/src/app/api/room/[id]/swipe/route.ts index 835368e..bc5325c 100644 --- a/src/app/api/room/[id]/swipe/route.ts +++ b/src/app/api/room/[id]/swipe/route.ts @@ -40,12 +40,7 @@ export const POST = apiHandler(async (req, { params }) => { return data; }); - if (!updated) { - return NextResponse.json( - { error: "房间不存在或已过期" }, - { status: 404 }, - ); - } + if (!updated) throw new ApiError("房间不存在或已过期", 404); notify(id); diff --git a/src/app/api/room/[id]/undo/route.ts b/src/app/api/room/[id]/undo/route.ts index f493849..92273a4 100644 --- a/src/app/api/room/[id]/undo/route.ts +++ b/src/app/api/room/[id]/undo/route.ts @@ -32,12 +32,7 @@ export const POST = apiHandler(async (req, { params }) => { return data; }); - if (!updated) { - return NextResponse.json( - { error: "房间不存在或已过期" }, - { status: 404 }, - ); - } + if (!updated) throw new ApiError("房间不存在或已过期", 404); notify(id); diff --git a/src/app/api/room/create/route.ts b/src/app/api/room/create/route.ts index 37d12f3..e0cfe35 100644 --- a/src/app/api/room/create/route.ts +++ b/src/app/api/room/create/route.ts @@ -150,12 +150,7 @@ export const POST = apiHandler(async (req) => { restaurants = results.slice(0, 15); } - if (restaurants.length === 0) { - return NextResponse.json( - { error: sceneConfig.emptyError }, - { status: 404 }, - ); - } + if (restaurants.length === 0) throw new ApiError(sceneConfig.emptyError, 404); const roomId = await createRoom(restaurants, userId, sceneConfig.key); return NextResponse.json({ roomId, restaurants }); diff --git a/src/app/profile/page.tsx b/src/app/profile/page.tsx index d8a0500..f9d0b7d 100644 --- a/src/app/profile/page.tsx +++ b/src/app/profile/page.tsx @@ -29,6 +29,7 @@ import RestaurantImage from "@/components/RestaurantImage"; import { ProfileCardSkeleton, RecordItemSkeleton } from "@/components/Skeleton"; import { getUserId, getCachedProfile, setCachedProfile, setCachedPreferences, logout } from "@/lib/userId"; import { getAvatarBg, AVATARS } from "@/lib/avatars"; +import { buildNavUrl } from "@/lib/navigation"; import type { UserProfile, UserPreferences, DecisionRecord, FavoriteRecord, Restaurant } from "@/types"; function firstImage(r: Restaurant): string { @@ -279,10 +280,6 @@ export default function ProfilePage() { if (!profile) return null; - const amapNavUrl = (r: Restaurant) => - r.location - ? `https://uri.amap.com/marker?position=${r.location}&name=${encodeURIComponent(r.name)}&callnative=1` - : `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(r.name)}`; return (
@@ -567,7 +564,7 @@ export default function ProfilePage() { {history.map((d) => (