Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
何 广一
chatroom
Commits
35afac8f
Commit
35afac8f
authored
Aug 22, 2025
by
何 广一
Browse files
backend without database
parent
29493cc4
Changes
15
Hide whitespace changes
Inline
Side-by-side
chat-room/src/app/api/message/add/route.tsx
0 → 100644
View file @
35afac8f
import
{
NextRequest
,
NextResponse
}
from
'
next/server
'
;
import
*
as
Ty
from
'
../../../interface
'
import
*
as
Db
from
'
../../room-api
'
;
/*
- url: /api/message/add
- method: POST
- argument:
interface MessageAddArgs {
roomId: number;
content: string;
sender: string;
}
- response: null (只要code为0即为成功)
*/
export
async
function
POST
(
req
:
NextRequest
):
Promise
<
NextResponse
<
Ty
.
BackendResponse
<
null
>>>
{
const
{
roomId
,
content
,
sender
}
=
await
req
.
json
()
as
Ty
.
MessageAddArgs
;
const
response
=
await
Db
.
AddMessage
({
roomId
,
content
,
sender
});
return
NextResponse
.
json
(
response
);
}
chat-room/src/app/api/room-api.tsx
0 → 100644
View file @
35afac8f
import
{
NextRequest
,
NextResponse
}
from
'
next/server
'
;
import
*
as
Ty
from
'
../interface
'
import
{
RoomInfo
,
State
}
from
'
./tmp-state
'
export
async
function
MakeError
(
message
:
string
,
code
:
number
=
1
):
Promise
<
NextResponse
<
Ty
.
BackendResponse
<
null
>>>
{
return
NextResponse
.
json
({
code
,
message
,
data
:
null
});
}
export
async
function
GetRoomList
():
Promise
<
Ty
.
BackendResponse
<
Ty
.
RoomListRes
>>
{
console
.
log
(
'
获取到的房间列表:
'
,
State
.
fakeRooms
as
Ty
.
RoomPreviewInfo
[]);
return
{
code
:
0
,
message
:
'
房间获取成功
'
,
data
:
{
rooms
:
State
.
fakeRooms
as
Ty
.
RoomPreviewInfo
[]
}
};
}
export
async
function
AddRoom
(
data
:
Ty
.
RoomAddArgs
):
Promise
<
Ty
.
BackendResponse
<
Ty
.
RoomAddResult
>>
{
const
newRoom
:
RoomInfo
=
{
roomId
:
State
.
maxRoomId
+
1
,
roomName
:
data
.
roomName
,
lastMessage
:
null
,
creator
:
data
.
user
};
State
.
fakeRooms
=
[...
State
.
fakeRooms
,
newRoom
];
console
.
log
(
'
当前房间列表:
'
,
State
.
fakeRooms
);
State
.
maxRoomId
++
;
return
{
code
:
0
,
message
:
'
房间创建成功
'
,
data
:
{
roomId
:
newRoom
.
roomId
}
};
}
export
async
function
GetMessageList
(
data
:
Ty
.
RoomMessageListArgs
):
Promise
<
Ty
.
BackendResponse
<
Ty
.
RoomMessageListRes
>>
{
const
{
roomId
}
=
data
;
const
messages
=
State
.
fakeMsg
.
filter
(
msg
=>
msg
.
roomId
===
roomId
);
return
{
code
:
0
,
message
:
'
消息获取成功
'
,
data
:
{
messages
}
};
}
export
async
function
UpdateMessageList
(
data
:
Ty
.
RoomMessageGetUpdateArgs
):
Promise
<
Ty
.
BackendResponse
<
Ty
.
RoomMessageGetUpdateRes
>>
{
const
{
roomId
,
sinceMessageId
}
=
data
;
const
messages
=
State
.
fakeMsg
.
filter
(
msg
=>
msg
.
roomId
===
roomId
&&
msg
.
messageId
>
sinceMessageId
);
return
{
code
:
0
,
message
:
'
消息更新成功
'
,
data
:
{
messages
}
};
}
//后续需要补上房间建立者和删除者不同的无权限错误
export
async
function
DeleteRoom
(
data
:
Ty
.
RoomDeleteArgs
):
Promise
<
Ty
.
BackendResponse
<
null
>>
{
const
{
user
,
roomId
}
=
data
;
State
.
fakeRooms
=
State
.
fakeRooms
.
filter
(
room
=>
room
.
roomId
!==
roomId
);
State
.
fakeMsg
=
State
.
fakeMsg
.
filter
(
msg
=>
msg
.
roomId
!==
roomId
);
return
{
code
:
0
,
message
:
'
房间删除成功
'
,
data
:
null
};
}
export
async
function
AddMessage
(
data
:
Ty
.
MessageAddArgs
):
Promise
<
Ty
.
BackendResponse
<
null
>>
{
const
{
roomId
,
sender
,
content
}
=
data
;
const
newMessage
:
Ty
.
Message
=
{
messageId
:
State
.
maxMessageId
+
1
,
roomId
,
sender
,
content
,
time
:
Date
.
now
()
};
State
.
fakeMsg
=
[...
State
.
fakeMsg
,
newMessage
];
State
.
maxMessageId
++
;
return
{
code
:
0
,
message
:
'
消息发送成功
'
,
data
:
null
};
}
\ No newline at end of file
chat-room/src/app/api/room/add/route.tsx
0 → 100644
View file @
35afac8f
import
{
NextRequest
,
NextResponse
}
from
'
next/server
'
;
import
*
as
Ty
from
'
../../../interface
'
import
*
as
Db
from
'
../../room-api
'
;
/*
- url: /api/room/add
- method: POST
- argument:
interface RoomAddArgs {
user: string;
roomName: string;
}
- response:
interface RoomAddRes {
roomId: number;
}
*/
export
async
function
POST
(
req
:
NextRequest
)
{
const
{
user
,
roomName
}:
Ty
.
RoomAddArgs
=
await
req
.
json
();
const
response
=
await
Db
.
AddRoom
({
user
,
roomName
});
return
NextResponse
.
json
(
response
);
}
\ No newline at end of file
chat-room/src/app/api/room/delete/route.tsx
0 → 100644
View file @
35afac8f
import
{
NextRequest
,
NextResponse
}
from
'
next/server
'
;
import
*
as
Ty
from
'
../../../interface
'
import
*
as
Db
from
'
../../room-api
'
;
/*
- url: /api/room/delete
- method: POST
- argument:
interface RoomDeleteArgs {
user: string;
roomId: number;
}
- response: null (只要code为0即为成功)
*/
export
async
function
POST
(
req
:
NextRequest
):
Promise
<
NextResponse
<
Ty
.
BackendResponse
<
null
>>>
{
const
{
user
,
roomId
}
=
await
req
.
json
()
as
Ty
.
RoomDeleteArgs
;
const
response
=
await
Db
.
DeleteRoom
({
user
,
roomId
});
return
NextResponse
.
json
(
response
);
}
\ No newline at end of file
chat-room/src/app/api/room/list/route.tsx
0 → 100644
View file @
35afac8f
import
{
NextRequest
,
NextResponse
}
from
'
next/server
'
;
import
*
as
Ty
from
'
../../../interface
'
import
*
as
Db
from
'
../../room-api
'
;
/*
- url: /api/room/list
- method: GET
- argument: null
- response:
interface RoomListRes {
rooms: RoomPreviewInfo[];
}
*/
export
async
function
GET
(
req
:
NextRequest
)
:
Promise
<
NextResponse
<
Ty
.
BackendResponse
<
Ty
.
RoomListRes
>>>
{
const
response
=
await
Db
.
GetRoomList
();
return
NextResponse
.
json
(
response
);
}
\ No newline at end of file
chat-room/src/app/api/room/message/getUpdate/route.tsx
0 → 100644
View file @
35afac8f
import
{
NextRequest
,
NextResponse
}
from
'
next/server
'
;
import
*
as
Ty
from
'
../../../../interface
'
import
*
as
Db
from
'
../../../room-api
'
;
/*
- url: /api/room/message/getUpdate
- method: GET
- argument:
interface RoomMessageGetUpdateArgs {
roomId: number;
sinceMessageId: number;
}
- response:
interface RoomMessageGetUpdateRes {
messages: Message[];
}
*/
export
async
function
GET
(
req
:
NextRequest
):
Promise
<
NextResponse
<
Ty
.
BackendResponse
<
Ty
.
RoomMessageGetUpdateRes
>>>
{
const
Params
=
req
.
nextUrl
.
searchParams
;
const
result
=
await
Db
.
UpdateMessageList
({
roomId
:
Number
(
Params
.
get
(
'
roomId
'
)),
sinceMessageId
:
Number
(
Params
.
get
(
'
sinceMessageId
'
))
});
return
NextResponse
.
json
(
result
);
}
\ No newline at end of file
chat-room/src/app/api/room/message/list/route.tsx
0 → 100644
View file @
35afac8f
import
{
NextRequest
,
NextResponse
}
from
'
next/server
'
;
import
*
as
Ty
from
'
../../../../interface
'
import
*
as
Db
from
'
../../../room-api
'
;
/*
- url: /api/room/message/list
- method: GET
- argument:
interface RoomMessageListArgs {
roomId: number;
}
- response:
interface RoomMessageListRes {
messages: Message[];
}
*/
export
async
function
GET
(
req
:
NextRequest
):
Promise
<
NextResponse
<
Ty
.
BackendResponse
<
Ty
.
RoomMessageListRes
>>>
{
const
Params
=
req
.
nextUrl
.
searchParams
;
const
response
=
await
Db
.
GetMessageList
({
roomId
:
Number
(
Params
.
get
(
'
roomId
'
))
});
return
NextResponse
.
json
(
response
);
}
chat-room/src/app/api/tmp-state.tsx
0 → 100644
View file @
35afac8f
import
*
as
Ty
from
'
../interface
'
export
interface
RoomInfo
extends
Ty
.
RoomPreviewInfo
{
creator
:
string
;
}
export
let
State
=
{
maxRoomId
:
2
as
number
,
maxMessageId
:
3
as
number
,
fakeRooms
:
[
{
roomId
:
1
,
roomName
:
'
房间1
'
,
lastMessage
:
null
,
creator
:
'
用户1
'
},
{
roomId
:
2
,
roomName
:
'
房间2
'
,
lastMessage
:
null
,
creator
:
'
用户2
'
}
]
as
RoomInfo
[],
fakeMsg
:
[
{
messageId
:
1
,
roomId
:
1
,
sender
:
'
用户1
'
,
content
:
'
你好
'
,
time
:
Date
.
now
()
},
{
messageId
:
2
,
roomId
:
1
,
sender
:
'
用户2
'
,
content
:
'
你好啊
'
,
time
:
Date
.
now
()
},
{
messageId
:
3
,
roomId
:
2
,
sender
:
'
用户1
'
,
content
:
'
房间2的消息
'
,
time
:
Date
.
now
()
}
]
as
Ty
.
Message
[],
}
chat-room/src/app/chat/confirm-modal.tsx
View file @
35afac8f
'
use client
'
import
{
useState
,
useEffect
}
from
'
react
'
import
*
as
Ty
from
'
./interface
'
import
{
useEffect
}
from
'
react
'
;
import
{
useRoom
}
from
'
./room-context
'
export
default
function
ConfirmModalProvider
()
{
...
...
@@ -15,6 +14,12 @@ export default function ConfirmModalProvider() {
errorModalMsg
,
triggerCreateRoom
}
=
useRoom
();
useEffect
(()
=>
{
if
(
showCreateModal
)
{
setNewRoomName
(
''
);
}
},
[
showCreateModal
,
setNewRoomName
]);
return
(
<>
{
showCreateModal
&&
...
...
chat-room/src/app/chat/page.tsx
View file @
35afac8f
import
*
as
Ty
from
'
./interface
'
import
*
as
Ty
from
'
.
.
/interface
'
import
RoomListProvider
from
'
./room-list
'
;
import
RoomHeaderProvider
from
'
./current-room
'
;
import
RoomMessageProvider
from
'
./room-message
'
;
...
...
chat-room/src/app/chat/room-context.tsx
View file @
35afac8f
'
use client
'
import
{
createContext
,
useContext
,
useState
,
ReactNode
,
useCallback
}
from
'
react
'
import
*
as
Ty
from
'
./interface
'
import
*
as
Ty
from
'
.
.
/interface
'
import
{
useSearchParams
}
from
'
next/navigation
'
;
interface
RoomCtxValue
{
...
...
chat-room/src/app/chat/room-list.tsx
View file @
35afac8f
'
use client
'
import
{
useState
,
useEffect
}
from
'
react
'
import
*
as
Ty
from
'
./interface
'
import
*
as
Ty
from
'
.
.
/interface
'
import
{
useRoom
}
from
'
./room-context
'
export
default
function
RoomListProvider
()
{
...
...
@@ -236,11 +236,11 @@ export default function RoomListProvider() {
>
—
</
button
>
<
h3
>
{
room
.
roomName
}
(ID:
{
room
.
roomId
}
)
</
h3
>
<
h3
>
{
room
.
roomName
}
</
h3
>
{
room
.
lastMessage
?
(
<
p
>
{
room
.
lastMessage
.
sender
}
:
{
room
.
lastMessage
.
content
}
</
p
>
)
:
(
<
p
>
暂无消息
。
</
p
>
<
p
className
=
"text-gray-500"
>
暂无消息
</
p
>
)
}
</
div
>
</
li
>
...
...
chat-room/src/app/chat/room-message.tsx
View file @
35afac8f
'
use client
'
import
{
useState
,
useEffect
,
useRef
,
use
}
from
'
react
'
import
*
as
Ty
from
'
./interface
'
import
*
as
Ty
from
'
.
.
/interface
'
import
{
useRoom
}
from
'
./room-context
'
export
default
function
RoomMessageProvider
()
{
...
...
chat-room/src/app/chat/send-message.tsx
View file @
35afac8f
...
...
@@ -2,7 +2,7 @@
import
{
useRoom
}
from
'
./room-context
'
import
{
useEffect
,
useState
}
from
'
react
'
import
*
as
Ty
from
'
./interface
'
import
*
as
Ty
from
'
.
.
/interface
'
export
default
function
SendMessageProvider
()
{
...
...
chat-room/src/app/
chat/
interface.tsx
→
chat-room/src/app/interface.tsx
View file @
35afac8f
export
const
urlPrefix
=
'
https://chatroom.zjuxlab.com
'
/*
======================================
===============后端选择===============
======================================
*/
const
useLocalBackend
:
boolean
=
true
;
/*
======================================
======================================
======================================
*/
export
const
urlPrefix
=
useLocalBackend
?
''
:
'
https://chatroom.zjuxlab.com
'
export
interface
BackendResponse
<
T
>
{
code
:
number
;
...
...
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