feat: 实现 NoWhatever 别说随便餐厅决策 Web App
- Framer Motion 卡片滑动 UI,带物理阻尼动画 - 多人房间系统,4位房间号 + SWR 实时轮询 - 高德地图 POI v5 API 搜索附近餐厅 - Web Share API 一键邀请,剪贴板降级方案 - SQLite/Prisma 持久化存储 - 移动端优先响应式设计 (Tailwind CSS)
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getRoomData, updateRoomData } from "@/lib/store";
|
||||
|
||||
export async function POST(
|
||||
req: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
const { id } = await params;
|
||||
|
||||
try {
|
||||
const data = await getRoomData(id);
|
||||
|
||||
if (!data) {
|
||||
return NextResponse.json(
|
||||
{ error: "房间不存在或已过期" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
const { userId } = await req.json();
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: "userId required" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!data.users.includes(userId)) {
|
||||
data.users.push(userId);
|
||||
await updateRoomData(id, data);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
roomId: id,
|
||||
userCount: data.users.length,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("Failed to join room:", e);
|
||||
return NextResponse.json(
|
||||
{ error: "加入房间失败" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getRoomData } from "@/lib/store";
|
||||
|
||||
export async function GET(
|
||||
_req: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
const { id } = await params;
|
||||
|
||||
try {
|
||||
const data = await getRoomData(id);
|
||||
|
||||
if (!data) {
|
||||
return NextResponse.json(
|
||||
{ error: "房间不存在或已过期" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
roomId: id,
|
||||
userCount: data.users.length,
|
||||
match: data.match,
|
||||
restaurants: data.restaurants,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("Failed to get room:", e);
|
||||
return NextResponse.json(
|
||||
{ error: "获取房间信息失败" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getRoomData, updateRoomData } from "@/lib/store";
|
||||
|
||||
export async function POST(
|
||||
req: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
const { id } = await params;
|
||||
|
||||
try {
|
||||
const data = await getRoomData(id);
|
||||
|
||||
if (!data) {
|
||||
return NextResponse.json(
|
||||
{ error: "房间不存在或已过期" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
const { userId, restaurantId, action } = await req.json();
|
||||
|
||||
if (!userId || restaurantId == null || !action) {
|
||||
return NextResponse.json(
|
||||
{ error: "userId, restaurantId, and action are required" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const rid = String(restaurantId);
|
||||
let dirty = false;
|
||||
|
||||
if (action === "like") {
|
||||
if (!data.likes[rid]) {
|
||||
data.likes[rid] = [];
|
||||
}
|
||||
if (!data.likes[rid].includes(userId)) {
|
||||
data.likes[rid].push(userId);
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
if (
|
||||
data.users.length > 1 &&
|
||||
data.likes[rid].length === data.users.length
|
||||
) {
|
||||
data.match = rid;
|
||||
dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (dirty) {
|
||||
await updateRoomData(id, data);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
match: data.match,
|
||||
likeCount: data.likes[rid]?.length ?? 0,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("Failed to process swipe:", e);
|
||||
return NextResponse.json(
|
||||
{ error: "操作失败" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { createRoom } from "@/lib/store";
|
||||
import { Restaurant } from "@/types";
|
||||
import { fallbackRestaurants } from "@/data/restaurants";
|
||||
|
||||
interface AmapPoiV5 {
|
||||
id: string;
|
||||
name: string;
|
||||
distance?: string;
|
||||
type?: string;
|
||||
address?: string;
|
||||
location?: string;
|
||||
business?: {
|
||||
rating?: string;
|
||||
cost?: string;
|
||||
opentime_today?: string;
|
||||
opentime_week?: string;
|
||||
tel?: string;
|
||||
tag?: string;
|
||||
};
|
||||
photos?: { url: string }[];
|
||||
}
|
||||
|
||||
const DEFAULT_IMAGE =
|
||||
"https://images.unsplash.com/photo-1517248135467-4c7edcad34c4?w=800&q=80";
|
||||
|
||||
function extractCategory(type?: string): string {
|
||||
if (!type) return "";
|
||||
const parts = type.split(";");
|
||||
return parts[parts.length - 1] || parts[1] || "";
|
||||
}
|
||||
|
||||
function cleanField(val: unknown): string {
|
||||
if (!val || val === "[]" || (Array.isArray(val) && val.length === 0))
|
||||
return "";
|
||||
return String(val);
|
||||
}
|
||||
|
||||
function mapPoiToRestaurant(poi: AmapPoiV5): Restaurant {
|
||||
const ratingStr = poi.business?.rating;
|
||||
const rating =
|
||||
ratingStr && ratingStr !== "[]" ? parseFloat(ratingStr) || 4.0 : 4.0;
|
||||
|
||||
const costStr = poi.business?.cost;
|
||||
const price =
|
||||
costStr && costStr !== "[]" && costStr !== "0" ? `¥${costStr}` : "未知";
|
||||
|
||||
const image =
|
||||
poi.photos && poi.photos.length > 0 && poi.photos[0].url
|
||||
? poi.photos[0].url
|
||||
: DEFAULT_IMAGE;
|
||||
|
||||
const openTime =
|
||||
cleanField(poi.business?.opentime_week) ||
|
||||
cleanField(poi.business?.opentime_today);
|
||||
|
||||
return {
|
||||
id: poi.id,
|
||||
name: poi.name,
|
||||
rating,
|
||||
price,
|
||||
distance: poi.distance ? `${poi.distance}m` : "",
|
||||
image,
|
||||
category: extractCategory(poi.type),
|
||||
address: cleanField(poi.address),
|
||||
openTime,
|
||||
tel: cleanField(poi.business?.tel),
|
||||
tag: cleanField(poi.business?.tag),
|
||||
location: cleanField(poi.location),
|
||||
};
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
let restaurants: Restaurant[] = fallbackRestaurants;
|
||||
|
||||
try {
|
||||
const body = await req.json();
|
||||
const { lat, lng } = body;
|
||||
|
||||
if (lat && lng) {
|
||||
const apiKey = process.env.AMAP_API_KEY;
|
||||
if (!apiKey) {
|
||||
console.error("AMAP_API_KEY not configured");
|
||||
} else {
|
||||
const url = new URL("https://restapi.amap.com/v5/place/around");
|
||||
url.searchParams.set("key", apiKey);
|
||||
url.searchParams.set("location", `${lng},${lat}`);
|
||||
url.searchParams.set("radius", "3000");
|
||||
url.searchParams.set("types", "050000");
|
||||
url.searchParams.set("show_fields", "business,photos");
|
||||
url.searchParams.set("page_size", "15");
|
||||
url.searchParams.set("sortrule", "weight");
|
||||
|
||||
const amapRes = await fetch(url.toString());
|
||||
const amapData = await amapRes.json();
|
||||
|
||||
if (amapData.status === "1" && amapData.pois?.length > 0) {
|
||||
restaurants = amapData.pois.map(mapPoiToRestaurant);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Amap API error, using fallback data:", e);
|
||||
}
|
||||
|
||||
try {
|
||||
const roomId = await createRoom(restaurants);
|
||||
return NextResponse.json({ roomId, restaurants });
|
||||
} catch (e) {
|
||||
console.error("Failed to create room:", e);
|
||||
return NextResponse.json(
|
||||
{ error: "创建房间失败,请重试" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user