Commit 44715b3f authored by 成尧 江's avatar 成尧 江
Browse files

bugs fixed

parent 5b05db40
docker-compose down -v --remove-orphans
docker-compose build --no-cache
docker-compose up
docker compose up --build
\ No newline at end of file
services:
app:
build: .
container_name: nextjs_app
restart: always
container_name: chatroom2
ports:
- "3000:3000"
environment:
NODE_ENV: production
DATABASE_URL: "file:/app/prisma/dev.db"
volumes:
- ./prisma/dev.db:/app/prisma/dev.db # ✅ 只挂载数据库文件
command: >
sh -c "npx prisma generate &&
npx prisma migrate deploy &&
npm run build &&
npm run start"
......@@ -5,7 +5,8 @@ FROM node:18-slim AS builder
WORKDIR /app
RUN apt-get update -y && apt-get install -y openssl
RUN apt-get update && apt-get install -y openssl \
&& rm -rf /var/lib/apt/lists/*
# 先复制依赖文件并安装
COPY package*.json ./
......@@ -16,7 +17,6 @@ RUN npm install
# 复制所有代码
COPY . .
# 构建 Next.js 应用
RUN npm run build
# ================
......
......@@ -4,6 +4,7 @@ const nextConfig: NextConfig = {
eslint: {
ignoreDuringBuilds: true,
},
output: 'standalone',
};
export default nextConfig;
No preview for this file type
......@@ -3,7 +3,7 @@ import { prisma } from "@/lib/prisma";
export async function POST(req: Request) {
try {
const { roomId } = await req.json();
const { user, roomId } = await req.json();
if (!roomId) {
return NextResponse.json({
......@@ -13,6 +13,18 @@ export async function POST(req: Request) {
}
const roomIdInt = Number(roomId); // ✅ 统一转成 Int
const deleter = await prisma.user.findUnique({
where: { username: user},
});
const room = await prisma.room.findUnique({
where: { id: roomIdInt },
});
if(deleter?.id !== room?.createdBy) {
return NextResponse.json({
code: 1,
error: "you have no right to delete this room",
});
}
// 1️⃣ 先清空房间的用户关联
await prisma.room.update({
......
......@@ -17,20 +17,10 @@ export default function ChatRoomPage() {
const router = useRouter();
// ✅ 挂载时获取用户名 & 房间列表
useEffect(() => {
setMounted(true);
const username = localStorage.getItem("username");
if (username) {
setCurrentUser(username);
}
async function fetchRooms() {
const res = await getRooms();
if (res.code === 0) {
setRooms(
res.data.rooms.map((r: any) => ({
roomId: String(r.id), // 确保是字符串
const getMessage = async () => {
const res = await getRooms();
return res.data.rooms.map((r: any) => ({
roomId: r.id,
roomName: r.roomName,
lastMessage: {
roomid: r.id,
......@@ -39,9 +29,19 @@ export default function ChatRoomPage() {
sender: r.lastMessage?.sender || "",
time: r.lastMessage?.time || new Date().toISOString(),
},
}))
);
}
}))
};
// ✅ 挂载时获取用户名 & 房间列表
useEffect(() => {
setMounted(true);
const username = localStorage.getItem("username");
if (username) {
setCurrentUser(username);
}
async function fetchRooms() {
const res = await getMessage();
setRooms(res);
}
fetchRooms();
}, []);
......@@ -79,24 +79,20 @@ export default function ChatRoomPage() {
const res = await addRoom(currentUser, name);
if (res.code === 0) {
const listRes = await getRooms();
if (listRes.code === 0) {
setRooms(listRes.data.rooms);
}
const listRes = await getMessage();
setRooms(listRes);
}
};
// ✅ 删除房间
const handleDeleteRoom = async (roomId: number) => {
if (!currentUser) return alert("请先登录!");
const confirmDelete = confirm("确定要删除这个房间吗?");
if (!confirmDelete) return;
const res = await deleteRoom(roomId);
const res = await deleteRoom(currentUser, roomId);
if (res.code === 0) {
const listRes = await getRooms();
if (listRes.code === 0) {
setRooms(listRes.data.rooms);
}
const listRes = await getMessage();
setRooms(listRes);
if (currentRoomId === roomId) {
setCurrentRoomId(null);
setMessages([]);
......@@ -248,7 +244,7 @@ return (
</div>
) : (
messages.map((msg) => (
<MessageItem key={msg.messageId} message={msg} />
<MessageItem key={msg.time} message={msg} />
))
)}
</div>
......
......@@ -9,9 +9,9 @@ export default function HomePage() {
useEffect(() => {
const username = localStorage.getItem("username");
if (username) {
router.replace("/chat"); // ✅ 已登录,去聊天室
router.replace("/chat");
} else {
router.replace("/login"); // ❌ 未登录,去登录页
router.replace("/login");
}
}, [router]);
......
......@@ -8,6 +8,7 @@ const apiFetch = async (url: string, options?: RequestInit) => {
...(options?.headers || {}),
},
});
return res.json();
};
......@@ -25,27 +26,12 @@ export async function getRooms() {
return apiFetch("/api/room/list");
}
export async function deleteRoom(roomId: number) {
export async function deleteRoom(user: string, roomId: number) {
const res = await fetch("/api/room/delete", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ roomId }),
body: JSON.stringify({ user, roomId }),
});
return res.json();
}
// 消息相关
export async function addMessage(roomId: number, content: string, sender: string) {
return apiFetch("/api/message/add", {
method: "POST",
body: JSON.stringify({ roomId, content, sender }),
});
}
export async function getRoomMessages(roomId: number) {
return apiFetch(`/api/room/message/list?roomId=${roomId}`);
}
export async function getMessageUpdates(roomId: number, sinceMessageId: number) {
return apiFetch(`/api/room/message/getUpdate?roomId=${roomId}&sinceMessageId=${sinceMessageId}`);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment