import { NextRequest, NextResponse } from 'next/server'; import fs from 'fs/promises'; import path from 'path'; const DATA_DIR = path.join(process.cwd(), 'data'); const HOUSES_FILE = path.join(DATA_DIR, 'houses.json'); const USERS_FILE = path.join(DATA_DIR, 'users.json'); interface House { id: string; owner: string; title: string; description: string; price: number; district: string; address: string; phone: string; images: string[]; createdAt: string; } interface User { id: string; username: string; token: string; } async function readFile(filePath: string, defaultValue: T): Promise { try { const data = await fs.readFile(filePath, 'utf-8'); return JSON.parse(data); } catch { return defaultValue; } } async function getUserFromToken(token: string): Promise { const users = await readFile(USERS_FILE, []); return users.find(u => u.token === token) || null; } async function readHouses(): Promise { return readFile(HOUSES_FILE, []); } async function writeHouses(houses: House[]): Promise { await fs.writeFile(HOUSES_FILE, JSON.stringify(houses, null, 2)); } export async function GET( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const { id } = await params; const houses = await readHouses(); const house = houses.find(h => h.id === id); if (!house) { return NextResponse.json({ error: '房屋不存在' }, { status: 404 }); } return NextResponse.json({ house }); } catch (error) { console.error('Get house error:', error); return NextResponse.json({ error: '获取房屋信息失败' }, { status: 500 }); } } export async function PUT( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const token = request.cookies.get('auth_token')?.value; if (!token) { return NextResponse.json({ error: '请先登录' }, { status: 401 }); } const user = await getUserFromToken(token); if (!user) { return NextResponse.json({ error: '用户不存在' }, { status: 401 }); } const { id } = await params; const houses = await readHouses(); const houseIndex = houses.findIndex(h => h.id === id); if (houseIndex === -1) { return NextResponse.json({ error: '房屋不存在' }, { status: 404 }); } if (houses[houseIndex].owner !== user.username) { return NextResponse.json({ error: '无权修改此房屋' }, { status: 403 }); } const body = await request.json(); const { title, description, price, district, address, phone, images } = body; houses[houseIndex] = { ...houses[houseIndex], title: title || houses[houseIndex].title, description: description ?? houses[houseIndex].description, price: price !== undefined ? Number(price) : houses[houseIndex].price, district: district || houses[houseIndex].district, address: address || houses[houseIndex].address, phone: phone || houses[houseIndex].phone, images: images || houses[houseIndex].images }; await writeHouses(houses); return NextResponse.json({ success: true, house: houses[houseIndex] }); } catch (error) { console.error('Update house error:', error); return NextResponse.json({ error: '更新房屋失败' }, { status: 500 }); } } export async function DELETE( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const token = request.cookies.get('auth_token')?.value; if (!token) { return NextResponse.json({ error: '请先登录' }, { status: 401 }); } const user = await getUserFromToken(token); if (!user) { return NextResponse.json({ error: '用户不存在' }, { status: 401 }); } const { id } = await params; const houses = await readHouses(); const house = houses.find(h => h.id === id); if (!house) { return NextResponse.json({ error: '房屋不存在' }, { status: 404 }); } if (house.owner !== user.username) { return NextResponse.json({ error: '无权删除此房屋' }, { status: 403 }); } const newHouses = houses.filter(h => h.id !== id); await writeHouses(newHouses); return NextResponse.json({ success: true }); } catch (error) { console.error('Delete house error:', error); return NextResponse.json({ error: '删除房屋失败' }, { status: 500 }); } }