feat: 创建房间时支持自定义筛选条件(距离/人均/菜系)

替换硬编码搜索参数,用户可选择距离范围(1/3/5km)、
人均价格区间(50以下/50-100/100+)和菜系偏好(火锅、
日料、烧烤等),直接提升推荐质量。
This commit is contained in:
2026-02-24 18:29:38 +08:00
parent 059c009a8b
commit d83e5ec6c4
2 changed files with 126 additions and 5 deletions
+42 -4
View File
@@ -70,12 +70,42 @@ function mapPoiToRestaurant(poi: AmapPoiV5): Restaurant {
};
}
function filterByPrice(
restaurants: Restaurant[],
priceRange: string,
): Restaurant[] {
if (priceRange === "any") return restaurants;
return restaurants.filter((r) => {
if (r.price === "未知") return true;
const cost = parseInt(r.price.replace(/[¥¥]/g, ""), 10);
if (isNaN(cost)) return true;
switch (priceRange) {
case "under50":
return cost < 50;
case "50to100":
return cost >= 50 && cost <= 100;
case "over100":
return cost > 100;
default:
return true;
}
});
}
export async function POST(req: Request) {
let restaurants: Restaurant[] = fallbackRestaurants;
try {
const body = await req.json();
const { lat, lng } = body;
const {
lat,
lng,
radius = 3000,
priceRange = "any",
cuisine = "不限",
} = body;
if (lat && lng) {
const apiKey = process.env.AMAP_API_KEY;
@@ -85,17 +115,25 @@ export async function POST(req: Request) {
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("radius", String(radius));
url.searchParams.set("types", "050000");
url.searchParams.set("show_fields", "business,photos");
url.searchParams.set("page_size", "15");
url.searchParams.set("sortrule", "weight");
const needsPriceFilter = priceRange !== "any";
url.searchParams.set("page_size", needsPriceFilter ? "25" : "15");
if (cuisine && cuisine !== "不限") {
url.searchParams.set("keywords", cuisine);
}
const amapRes = await fetch(url.toString());
const amapData = await amapRes.json();
if (amapData.status === "1" && amapData.pois?.length > 0) {
restaurants = amapData.pois.map(mapPoiToRestaurant);
let results: Restaurant[] = amapData.pois.map(mapPoiToRestaurant);
results = filterByPrice(results, priceRange);
restaurants = results.slice(0, 15);
}
}
}