feat: 城中村租房平台 - 租客浏览、房东发布房源、图片上传

This commit is contained in:
Cuishibing
2026-03-22 10:15:31 +08:00
parent f03cf328dd
commit ce9dfae7c5
15 changed files with 1682 additions and 87 deletions

123
app/api/houses/route.ts Normal file
View File

@@ -0,0 +1,123 @@
import { NextRequest, NextResponse } from 'next/server';
import fs from 'fs/promises';
import path from 'path';
import crypto from 'crypto';
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<T>(filePath: string, defaultValue: T): Promise<T> {
try {
const data = await fs.readFile(filePath, 'utf-8');
return JSON.parse(data);
} catch {
return defaultValue;
}
}
async function getUserFromToken(token: string): Promise<User | null> {
const users = await readFile<User[]>(USERS_FILE, []);
return users.find(u => u.token === token) || null;
}
async function readHouses(): Promise<House[]> {
return readFile<House[]>(HOUSES_FILE, []);
}
async function writeHouses(houses: House[]): Promise<void> {
await fs.writeFile(HOUSES_FILE, JSON.stringify(houses, null, 2));
}
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const district = searchParams.get('district');
const keyword = searchParams.get('keyword');
let houses = await readHouses();
if (district && district !== '全部') {
houses = houses.filter(h => h.district === district);
}
if (keyword) {
const kw = keyword.toLowerCase();
houses = houses.filter(h =>
h.title.toLowerCase().includes(kw) ||
h.address.toLowerCase().includes(kw) ||
h.description.toLowerCase().includes(kw)
);
}
houses.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
return NextResponse.json({ houses });
} catch (error) {
console.error('Get houses error:', error);
return NextResponse.json({ error: '获取房屋列表失败' }, { status: 500 });
}
}
export async function POST(request: NextRequest) {
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 body = await request.json();
const { title, description, price, district, address, phone, images } = body;
if (!title || !price || !district || !address || !phone) {
return NextResponse.json({ error: '请填写完整信息' }, { status: 400 });
}
const house: House = {
id: crypto.randomUUID(),
owner: user.username,
title,
description: description || '',
price: Number(price),
district,
address,
phone,
images: images || [],
createdAt: new Date().toISOString()
};
const houses = await readHouses();
houses.push(house);
await writeHouses(houses);
return NextResponse.json({ success: true, house });
} catch (error) {
console.error('Create house error:', error);
return NextResponse.json({ error: '创建房屋失败' }, { status: 500 });
}
}