Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
成尧 江
ChatRoom2
Commits
44715b3f
Commit
44715b3f
authored
Aug 30, 2025
by
成尧 江
Browse files
bugs fixed
parent
5b05db40
Changes
9
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
44715b3f
docker-compose down -v --remove-orphans
docker compose up --build
docker-compose build --no-cache
\ No newline at end of file
docker-compose up
docker-compose.yml
View file @
44715b3f
services
:
services
:
app
:
app
:
build
:
.
build
:
.
container_name
:
nextjs_app
container_name
:
chatroom2
restart
:
always
ports
:
ports
:
-
"
3000:3000"
-
"
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"
dockerfile
View file @
44715b3f
...
@@ -5,7 +5,8 @@ FROM node:18-slim AS builder
...
@@ -5,7 +5,8 @@ FROM node:18-slim AS builder
WORKDIR
/app
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 ./
COPY
package*.json ./
...
@@ -16,7 +17,6 @@ RUN npm install
...
@@ -16,7 +17,6 @@ RUN npm install
# 复制所有代码
# 复制所有代码
COPY
. .
COPY
. .
# 构建 Next.js 应用
RUN
npm run build
RUN
npm run build
# ================
# ================
...
...
next.config.ts
View file @
44715b3f
...
@@ -4,6 +4,7 @@ const nextConfig: NextConfig = {
...
@@ -4,6 +4,7 @@ const nextConfig: NextConfig = {
eslint
:
{
eslint
:
{
ignoreDuringBuilds
:
true
,
ignoreDuringBuilds
:
true
,
},
},
output
:
'
standalone
'
,
};
};
export
default
nextConfig
;
export
default
nextConfig
;
prisma/dev.db
View file @
44715b3f
No preview for this file type
src/app/api/room/delete/route.ts
View file @
44715b3f
...
@@ -3,7 +3,7 @@ import { prisma } from "@/lib/prisma";
...
@@ -3,7 +3,7 @@ import { prisma } from "@/lib/prisma";
export
async
function
POST
(
req
:
Request
)
{
export
async
function
POST
(
req
:
Request
)
{
try
{
try
{
const
{
roomId
}
=
await
req
.
json
();
const
{
user
,
roomId
}
=
await
req
.
json
();
if
(
!
roomId
)
{
if
(
!
roomId
)
{
return
NextResponse
.
json
({
return
NextResponse
.
json
({
...
@@ -13,6 +13,18 @@ export async function POST(req: Request) {
...
@@ -13,6 +13,18 @@ export async function POST(req: Request) {
}
}
const
roomIdInt
=
Number
(
roomId
);
// ✅ 统一转成 Int
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️⃣ 先清空房间的用户关联
// 1️⃣ 先清空房间的用户关联
await
prisma
.
room
.
update
({
await
prisma
.
room
.
update
({
...
...
src/app/chat/page.tsx
View file @
44715b3f
...
@@ -17,20 +17,10 @@ export default function ChatRoomPage() {
...
@@ -17,20 +17,10 @@ export default function ChatRoomPage() {
const
router
=
useRouter
();
const
router
=
useRouter
();
// ✅ 挂载时获取用户名 & 房间列表
const
getMessage
=
async
()
=>
{
useEffect
(()
=>
{
const
res
=
await
getRooms
();
setMounted
(
true
);
return
res
.
data
.
rooms
.
map
((
r
:
any
)
=>
({
const
username
=
localStorage
.
getItem
(
"
username
"
);
roomId
:
r
.
id
,
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
),
// 确保是字符串
roomName
:
r
.
roomName
,
roomName
:
r
.
roomName
,
lastMessage
:
{
lastMessage
:
{
roomid
:
r
.
id
,
roomid
:
r
.
id
,
...
@@ -39,9 +29,19 @@ export default function ChatRoomPage() {
...
@@ -39,9 +29,19 @@ export default function ChatRoomPage() {
sender
:
r
.
lastMessage
?.
sender
||
""
,
sender
:
r
.
lastMessage
?.
sender
||
""
,
time
:
r
.
lastMessage
?.
time
||
new
Date
().
toISOString
(),
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
();
fetchRooms
();
},
[]);
},
[]);
...
@@ -79,24 +79,20 @@ export default function ChatRoomPage() {
...
@@ -79,24 +79,20 @@ export default function ChatRoomPage() {
const
res
=
await
addRoom
(
currentUser
,
name
);
const
res
=
await
addRoom
(
currentUser
,
name
);
if
(
res
.
code
===
0
)
{
if
(
res
.
code
===
0
)
{
const
listRes
=
await
getRooms
();
const
listRes
=
await
getMessage
();
if
(
listRes
.
code
===
0
)
{
setRooms
(
listRes
);
setRooms
(
listRes
.
data
.
rooms
);
}
}
}
};
};
// ✅ 删除房间
// ✅ 删除房间
const
handleDeleteRoom
=
async
(
roomId
:
number
)
=>
{
const
handleDeleteRoom
=
async
(
roomId
:
number
)
=>
{
if
(
!
currentUser
)
return
alert
(
"
请先登录!
"
);
const
confirmDelete
=
confirm
(
"
确定要删除这个房间吗?
"
);
const
confirmDelete
=
confirm
(
"
确定要删除这个房间吗?
"
);
if
(
!
confirmDelete
)
return
;
if
(
!
confirmDelete
)
return
;
const
res
=
await
deleteRoom
(
roomId
);
const
res
=
await
deleteRoom
(
currentUser
,
roomId
);
if
(
res
.
code
===
0
)
{
if
(
res
.
code
===
0
)
{
const
listRes
=
await
getRooms
();
const
listRes
=
await
getMessage
();
if
(
listRes
.
code
===
0
)
{
setRooms
(
listRes
);
setRooms
(
listRes
.
data
.
rooms
);
}
if
(
currentRoomId
===
roomId
)
{
if
(
currentRoomId
===
roomId
)
{
setCurrentRoomId
(
null
);
setCurrentRoomId
(
null
);
setMessages
([]);
setMessages
([]);
...
@@ -248,7 +244,7 @@ return (
...
@@ -248,7 +244,7 @@ return (
</
div
>
</
div
>
)
:
(
)
:
(
messages
.
map
((
msg
)
=>
(
messages
.
map
((
msg
)
=>
(
<
MessageItem
key
=
{
msg
.
me
ssageId
}
message
=
{
msg
}
/>
<
MessageItem
key
=
{
msg
.
ti
me
}
message
=
{
msg
}
/>
))
))
)
}
)
}
</
div
>
</
div
>
...
...
src/app/page.tsx
View file @
44715b3f
...
@@ -9,9 +9,9 @@ export default function HomePage() {
...
@@ -9,9 +9,9 @@ export default function HomePage() {
useEffect
(()
=>
{
useEffect
(()
=>
{
const
username
=
localStorage
.
getItem
(
"
username
"
);
const
username
=
localStorage
.
getItem
(
"
username
"
);
if
(
username
)
{
if
(
username
)
{
router
.
replace
(
"
/chat
"
);
// ✅ 已登录,去聊天室
router
.
replace
(
"
/chat
"
);
}
else
{
}
else
{
router
.
replace
(
"
/login
"
);
// ❌ 未登录,去登录页
router
.
replace
(
"
/login
"
);
}
}
},
[
router
]);
},
[
router
]);
...
...
src/lib/api.ts
View file @
44715b3f
...
@@ -8,6 +8,7 @@ const apiFetch = async (url: string, options?: RequestInit) => {
...
@@ -8,6 +8,7 @@ const apiFetch = async (url: string, options?: RequestInit) => {
...(
options
?.
headers
||
{}),
...(
options
?.
headers
||
{}),
},
},
});
});
return
res
.
json
();
return
res
.
json
();
};
};
...
@@ -25,27 +26,12 @@ export async function getRooms() {
...
@@ -25,27 +26,12 @@ export async function getRooms() {
return
apiFetch
(
"
/api/room/list
"
);
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
"
,
{
const
res
=
await
fetch
(
"
/api/room/delete
"
,
{
method
:
"
POST
"
,
method
:
"
POST
"
,
headers
:
{
"
Content-Type
"
:
"
application/json
"
},
headers
:
{
"
Content-Type
"
:
"
application/json
"
},
body
:
JSON
.
stringify
({
roomId
}),
body
:
JSON
.
stringify
({
user
,
roomId
}),
});
});
return
res
.
json
();
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
}
`
);
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment