Commit cf2d57c6 authored by 健杭 徐's avatar 健杭 徐
Browse files

fix profile display bug

parent 710c0f82
......@@ -20,12 +20,12 @@ const (
)
type Message struct {
MessageId int `json:"message_id"`
RoomId int `json:"room_id"`
Profile_id int `json:"profile_id"`
Sender string `json:"sender"`
Content string `json:"content"`
Timestamp string `json:"timestamp"`
MessageId int `json:"message_id"`
RoomId int `json:"room_id"`
Profile int `json:"profile"`
Sender string `json:"sender"`
Content string `json:"content"`
Time string `json:"time"`
}
type RoomPreviewInfo struct {
......@@ -214,7 +214,7 @@ func DeleteRoom(c *gin.Context) {
func AddMessage(c *gin.Context) {
var message struct {
RoomId int `json:"roomId"`
ProfileId int `json:"profile_id"`
ProfileId int `json:"profile"`
Sender string `json:"sender"`
Content string `json:"content"`
}
......@@ -225,7 +225,7 @@ func AddMessage(c *gin.Context) {
}
query := `
INSERT INTO messages (room_id, profile_id, sender, content, "time")
INSERT INTO messages (room_id, profile, sender, content, "time")
VALUES ($1, $2, $3, $4, NOW())
RETURNING message_id
`
......@@ -242,6 +242,8 @@ func AddMessage(c *gin.Context) {
)
if err != nil {
log.Printf("Failed to update room preview: %v", err)
c.JSON(http.StatusInternalServerError, Response{Code: 500, Msg: "Failed to updata lastest message: " + err.Error(), Data: nil})
return
}
c.JSON(http.StatusOK, Response{
......@@ -259,7 +261,7 @@ func GetMessageList(c *gin.Context) {
return
}
rows, err := db.Query("SELECT profile_id, sender, content, time FROM messages WHERE room_id = $1 ORDER BY time ASC", roomId)
rows, err := db.Query("SELECT profile, sender, content, time FROM messages WHERE room_id = $1 ORDER BY time ASC", roomId)
if err != nil {
c.JSON(500, Response{Code: 500, Msg: "Failed to retrieve messages", Data: nil})
return
......@@ -269,7 +271,7 @@ func GetMessageList(c *gin.Context) {
var messages []Message
for rows.Next() {
var msg Message
if err := rows.Scan(&msg.Profile_id, &msg.Sender, &msg.Content, &msg.Timestamp); err != nil {
if err := rows.Scan(&msg.Profile, &msg.Sender, &msg.Content, &msg.Time); err != nil {
c.JSON(500, Response{Code: 500, Msg: "Failed to scan message", Data: nil})
return
}
......@@ -300,7 +302,7 @@ func createTable() {
CREATE TABLE IF NOT EXISTS messages (
message_id SERIAL PRIMARY KEY,
room_id INT NOT NULL,
profile_id INT NOT NULL,
profile INT NOT NULL,
sender VARCHAR(100) NOT NULL,
content TEXT NOT NULL,
"time" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
......
......@@ -5,7 +5,6 @@ import React, { useEffect, useState } from "react";
const backEnd:string = "http://10.171.208.94:8080";
// profile
const Profile = [ 'https://pic4.zhimg.com/v2-c5a0d0d57c1a85c6db56e918707f54a3_r.jpg',
'https://pic2.zhimg.com/v2-c2e79191533fdc7fced2f658eef987c9_r.jpg',
'https://pic4.zhimg.com/v2-bf5f58e7b583cd69ac228db9fdff377f_r.jpg',
......@@ -16,22 +15,21 @@ const Profile = [ 'https://pic4.zhimg.com/v2-c5a0d0d57c1a85c6db56e918707f54a3_r.
const RoomProfile = 'https://tse1-mm.cn.bing.net/th/id/OIP-C.0KyBJKAdIGi9SAQc_X62tQHaLr?cb=thvnextc2&rs=1&pid=ImgDetMain';
interface RoomEntryProps {
roomId: number; // room id
roomName: string; // room name
lastSender: { String: string, Valid: boolean }; // the lasted User name
lastContent: { String: string, Valid: boolean }; // content
lastTime: { Time: string, Valid: boolean }; // the lasted message time
roomId: number;
roomName: string;
lastSender: { String: string, Valid: boolean };
lastContent: { String: string, Valid: boolean };
lastTime: { Time: string, Valid: boolean };
}
// 单个聊天房间组件
interface MessageProps {
roomId: number; // room id
roomName: string; // room name
roomId: number;
roomName: string;
messages: Array<{
profile: number; // profile index
sender: string; // sender name
content: string; // message content
time: string; // message time
profile: number;
sender: string;
content: string;
time: string;
}>;
}
......@@ -55,7 +53,7 @@ function RoomEntry ({rooms, onRoomClick} : {rooms: RoomEntryProps[], onRoomClick
<div className="chat-info">
<h3>{room.roomName}</h3>
<span className="chat-message">{room.lastContent && room.lastContent.Valid ? room.lastContent.String : ''}</span>
<span className="chat-time">{room.lastTime && room.lastTime.Valid ? room.lastTime.Time : ''}</span>
<span className="chat-time">{room.lastTime && room.lastTime.Valid ? formatTimeToHoursMinutes(room.lastTime.Time) : ''}</span>
</div>
</div>
))}
......@@ -65,6 +63,13 @@ function RoomEntry ({rooms, onRoomClick} : {rooms: RoomEntryProps[], onRoomClick
// Button From Uiverse.io by njesenberger
}
function formatTimeToHoursMinutes(isoString: string) {
const date = new Date(isoString);
const hours = String(date.getHours()).padStart(2, '0'); // 确保两位数
const minutes = String(date.getMinutes()).padStart(2, '0'); // 确保两位数
return `${hours}:${minutes}`;
}
function InputRoomNameArea({ onAddNewRoom }: { onAddNewRoom: (roomName: string) => void}) {
const [roomNameInput, setRoomNameInput] = useState("");
......@@ -116,7 +121,7 @@ function closeOpenDiv() {
(document.getElementsByClassName("RoomNameInput")[0] as HTMLInputElement).value = '';
}
function MessageItem (props: MessageProps & { userName: string; onAddNewComment: (content: string) => void}) {
function MessageItem (props: MessageProps & { onAddNewComment: (content: string) => void}) {
const [inputValue, setInputValue] = useState("");
if (props.roomId === 0) {
......@@ -144,7 +149,7 @@ function MessageItem (props: MessageProps & { userName: string; onAddNewComment:
<div className="message-content">
<div className="message-info">
<span className="message-sender">{msg.sender}</span>
<span className="message-time">{new Date(msg.time).toLocaleTimeString()}</span>
<span className="message-time">{formatTimeToHoursMinutes(msg.time)}</span>
</div>
<p className="message-text">{msg.content}</p>
</div>
......@@ -225,7 +230,6 @@ export function ChatRoom({ userName }: { userName: string }) {
const response = await fetch(backEnd+`/room/message/list?roomId=${roomId}`)
const result = await response.json();
debugger;
if (result.code === 0) {
setCurrentRoom({
roomId: roomId,
......@@ -336,7 +340,6 @@ export function ChatRoom({ userName }: { userName: string }) {
roomId={currentRoom?.roomId || 0}
roomName={currentRoom?.roomName || ""}
messages={currentRoom?.messages || []}
userName={userName}
onAddNewComment={addNewComment}
/>
<InputRoomNameArea onAddNewRoom={addNewRoom}/>
......
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