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

fix profile display bug

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