"use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import { useParams, useRouter } from "next/navigation"; interface House { id: string; owner: string; title: string; description: string; price: number; district: string; address: string; phone: string; images: string[]; createdAt: string; } export default function HouseDetail() { const params = useParams(); const router = useRouter(); const [house, setHouse] = useState(null); const [loading, setLoading] = useState(true); const [currentImage, setCurrentImage] = useState(0); const [showContact, setShowContact] = useState(false); useEffect(() => { fetchHouse(); }, []); async function fetchHouse() { try { const res = await fetch(`/api/houses/${params.id}`); const data = await res.json(); if (data.house) { setHouse(data.house); } } catch (error) { console.error("获取房屋详情失败", error); } finally { setLoading(false); } } function formatDate(dateStr: string) { return new Date(dateStr).toLocaleDateString("zh-CN"); } if (loading) { return (
🏠

加载中...

); } if (!house) { return (
😢

房屋不存在或已下架

返回首页
); } const images = house.images && house.images.length > 0 ? house.images : []; return (

房屋详情

{images.length > 0 ? (
{images.map((img, index) => ( {`${house.title} ))}
{images.length > 1 && (
{images.map((_, index) => (
)}
) : (
🏠
)}

{house.title}

{house.district}
¥{house.price} /月
📍 {house.address}
👤 房东:{house.owner}
{house.description && (

房屋描述

{house.description}

)}

发布时间

{formatDate(house.createdAt)}

{showContact && (

联系方式

📱

{house.phone}

拨打前请说明在平台上看到

立即拨打
)}
); }