From c0293e23ab3a69b0279972b98932d4a9104016f2 Mon Sep 17 00:00:00 2001 From: qiutianhong Date: Tue, 5 Nov 2024 19:14:03 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=96=B0=E5=A2=9E=E7=BB=99?= =?UTF-8?q?=E9=83=A8=E9=97=A8=E4=B8=8B=E7=9A=84=E7=94=A8=E6=88=B7=E5=8F=91?= =?UTF-8?q?=E9=80=81=E6=B6=88=E6=81=AF=EF=BC=8C=E4=BB=A5=E5=8F=8A=E7=BB=99?= =?UTF-8?q?=E8=87=AA=E5=B7=B1=E5=8F=91=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/chat/Chat.vue | 2 + frontend/src/components/chat/ChatMessage.vue | 2 + frontend/src/components/chat/ChatUserList.vue | 18 ++++--- frontend/src/stores/chat.ts | 51 +++++++++++++++++-- 4 files changed, 62 insertions(+), 11 deletions(-) diff --git a/frontend/src/components/chat/Chat.vue b/frontend/src/components/chat/Chat.vue index d714b65..a8a29db 100644 --- a/frontend/src/components/chat/Chat.vue +++ b/frontend/src/components/chat/Chat.vue @@ -115,6 +115,8 @@ @@ -138,6 +139,7 @@ diff --git a/frontend/src/components/chat/ChatUserList.vue b/frontend/src/components/chat/ChatUserList.vue index 6747167..c77c5be 100644 --- a/frontend/src/components/chat/ChatUserList.vue +++ b/frontend/src/components/chat/ChatUserList.vue @@ -9,7 +9,9 @@ } from "element-plus"; const store = useChatStore(); const handleNodeClick = (data) => { - console.log(data); + if (data.isUser) { + store.getSessionInfo(data.id, "user"); + } }; const defaultProps = { @@ -29,8 +31,11 @@ function transformUsers(users) { if (!users) return []; + console.log(users); return users.map((user) => ({ - label: `${user.user_name} (${user.user_id})`, + label: `${user.nick_name}`, + id: user.user_id, // 添加用户ID + isUser: true, children: [], })); } @@ -82,7 +87,9 @@ /> - +
{{ item.nickname }} @@ -100,7 +107,7 @@
- {{ item.login_ip}} + {{ item.login_ip }}
@@ -127,7 +134,6 @@ >部门 -
{ console.log(res); if (res.ok) { const data = await res.json(); - console.log(data) + console.log(data.data.users) groups.value = data.data.groups; departmentList.value = data.data.users; + + // 新增代码:提取部门成员并去重,按指定字段保存 + const uniqueUsers = new Set(); + + data.data.users.forEach((department: { users: any[]; }) => { + department.users?.forEach(async (user) => { + if (!uniqueUsers.has(user.user_id)) { + uniqueUsers.add(user.user_id); + const userToProcess = { + id: user.user_id, + login_ip: user.login_ip || '', // 假设 login_ip 可能未提供 + type: "user", + chatId: user.user_id, + online: user.is_online, + avatar: user.avatar || '/default_avatar.png', // 使用默认头像如果没有提供 + username: user.user_name, + nickname: user.nick_name + }; + + const existingUser = await db.getOne("workbenchChatUser", user.user_id); + if (existingUser) { + await db.update("workbenchChatUser", user.user_id, userToProcess); + } else { + await db.addOne("workbenchChatUser", userToProcess); + } + } + }); + }); } } @@ -565,11 +593,20 @@ export const useChatStore = defineStore('chatStore', () => { // 更新会话列表数据库 // 更新chatlist // 更新聊天记录 + + // 判断是否是自己发的消息 + if (data.userId === userInfo.value.id) { + return + } + const isPresence = await db.getByField('workbenchChatUser', 'chatId', data.userId) if (isPresence[0].id !== data.userId) { return } + + + // 添加消息记录 const addMessageHistory = { type: data.type, @@ -669,6 +706,12 @@ export const useChatStore = defineStore('chatStore', () => { const onlineUserData = async (data: OnlineUserInfoType[]) => { + + if (data.length === 0) { + onlineUserList.value = [] + return + } + // 创建一个新的用户数组,用于更新在线用户列表 const updatedOnlineUsers = data.map(item => ({ id: item.id, @@ -680,7 +723,6 @@ export const useChatStore = defineStore('chatStore', () => { username: item.username, nickname: item.nickname })).filter(item => item.id && item.login_ip); // 确保所有项都有有效的id和ip - // 更新在线用户列表,只添加不存在的用户 updatedOnlineUsers.forEach(newUser => { if (!onlineUserList.value.some(existingUser => existingUser.id === newUser.id)) { @@ -800,13 +842,12 @@ export const useChatStore = defineStore('chatStore', () => { // 设置目标用户的信息 const setTargetUserInfo = async (id: string) => { - console.log(id) const userInfoArray = await db.getByField("workbenchChatUser", "chatId", id); // 封装用户信息 const userInfo = { type: "user", - avatar: userInfoArray[0].avatar, - displayName: userInfoArray[0].nickname, + avatar: userInfoArray[0].avatar || "", + displayName: userInfoArray[0].nickname || "", toUserId: config.userInfo.id, chatId: userInfoArray[0].chatId }