Browse Source

add:新增给部门下的用户发送消息,以及给自己发消息

master
qiutianhong 7 months ago
parent
commit
c0293e23ab
  1. 2
      frontend/src/components/chat/Chat.vue
  2. 2
      frontend/src/components/chat/ChatMessage.vue
  3. 18
      frontend/src/components/chat/ChatUserList.vue
  4. 51
      frontend/src/stores/chat.ts

2
frontend/src/components/chat/Chat.vue

@ -115,6 +115,8 @@
<el-form label-position="top"> <el-form label-position="top">
<el-form-item label="群聊名称:"> <el-form-item label="群聊名称:">
<el-input <el-input
maxlength="8"
show-word-limit
style="width: 240px; height: 30px" style="width: 240px; height: 30px"
class="department-name" class="department-name"
v-model="store.departmentName" v-model="store.departmentName"

2
frontend/src/components/chat/ChatMessage.vue

@ -58,6 +58,7 @@
<el-image <el-image
fit="cover" fit="cover"
loading="lazy" loading="lazy"
:preview-src-list="[item.message]"
:src="item.message" :src="item.message"
/> />
</div> </div>
@ -138,6 +139,7 @@
<el-image <el-image
fit="cover" fit="cover"
loading="lazy" loading="lazy"
:preview-src-list="[item.message]"
:src="item.message" :src="item.message"
/> />
</div> </div>

18
frontend/src/components/chat/ChatUserList.vue

@ -9,7 +9,9 @@
} from "element-plus"; } from "element-plus";
const store = useChatStore(); const store = useChatStore();
const handleNodeClick = (data) => { const handleNodeClick = (data) => {
console.log(data); if (data.isUser) {
store.getSessionInfo(data.id, "user");
}
}; };
const defaultProps = { const defaultProps = {
@ -29,8 +31,11 @@
function transformUsers(users) { function transformUsers(users) {
if (!users) return []; if (!users) return [];
console.log(users);
return users.map((user) => ({ return users.map((user) => ({
label: `${user.user_name} (${user.user_id})`, label: `${user.nick_name}`,
id: user.user_id, // ID
isUser: true,
children: [], children: [],
})); }));
} }
@ -82,7 +87,9 @@
/> />
</el-col> </el-col>
<el-col :span="18"> <el-col :span="18">
<el-row style="display: flex; align-items: center;"> <el-row
style="display: flex; align-items: center"
>
<el-col> <el-col>
<div class="previewName"> <div class="previewName">
{{ item.nickname }} {{ item.nickname }}
@ -100,7 +107,7 @@
</el-row> </el-row>
<el-row> <el-row>
<div class="previewIP"> <div class="previewIP">
{{ item.login_ip}} {{ item.login_ip }}
</div> </div>
</el-row> </el-row>
</el-col> </el-col>
@ -127,7 +134,6 @@
>部门</span >部门</span
> >
</template> </template>
<div <div
class="tree-container" class="tree-container"
v-if="data.length > 0" v-if="data.length > 0"
@ -211,7 +217,7 @@
} }
.list-item:hover { .list-item:hover {
background-color: #F5F5F5; background-color: #f5f5f5;
} }
.avatar { .avatar {

51
frontend/src/stores/chat.ts

@ -143,9 +143,37 @@ export const useChatStore = defineStore('chatStore', () => {
console.log(res); console.log(res);
if (res.ok) { if (res.ok) {
const data = await res.json(); const data = await res.json();
console.log(data) console.log(data.data.users)
groups.value = data.data.groups; groups.value = data.data.groups;
departmentList.value = data.data.users; 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 // 更新chatlist
// 更新聊天记录 // 更新聊天记录
// 判断是否是自己发的消息
if (data.userId === userInfo.value.id) {
return
}
const isPresence = await db.getByField('workbenchChatUser', 'chatId', data.userId) const isPresence = await db.getByField('workbenchChatUser', 'chatId', data.userId)
if (isPresence[0].id !== data.userId) { if (isPresence[0].id !== data.userId) {
return return
} }
// 添加消息记录 // 添加消息记录
const addMessageHistory = { const addMessageHistory = {
type: data.type, type: data.type,
@ -669,6 +706,12 @@ export const useChatStore = defineStore('chatStore', () => {
const onlineUserData = async (data: OnlineUserInfoType[]) => { const onlineUserData = async (data: OnlineUserInfoType[]) => {
if (data.length === 0) {
onlineUserList.value = []
return
}
// 创建一个新的用户数组,用于更新在线用户列表 // 创建一个新的用户数组,用于更新在线用户列表
const updatedOnlineUsers = data.map(item => ({ const updatedOnlineUsers = data.map(item => ({
id: item.id, id: item.id,
@ -680,7 +723,6 @@ export const useChatStore = defineStore('chatStore', () => {
username: item.username, username: item.username,
nickname: item.nickname nickname: item.nickname
})).filter(item => item.id && item.login_ip); // 确保所有项都有有效的id和ip })).filter(item => item.id && item.login_ip); // 确保所有项都有有效的id和ip
// 更新在线用户列表,只添加不存在的用户 // 更新在线用户列表,只添加不存在的用户
updatedOnlineUsers.forEach(newUser => { updatedOnlineUsers.forEach(newUser => {
if (!onlineUserList.value.some(existingUser => existingUser.id === newUser.id)) { if (!onlineUserList.value.some(existingUser => existingUser.id === newUser.id)) {
@ -800,13 +842,12 @@ export const useChatStore = defineStore('chatStore', () => {
// 设置目标用户的信息 // 设置目标用户的信息
const setTargetUserInfo = async (id: string) => { const setTargetUserInfo = async (id: string) => {
console.log(id)
const userInfoArray = await db.getByField("workbenchChatUser", "chatId", id); const userInfoArray = await db.getByField("workbenchChatUser", "chatId", id);
// 封装用户信息 // 封装用户信息
const userInfo = { const userInfo = {
type: "user", type: "user",
avatar: userInfoArray[0].avatar, avatar: userInfoArray[0].avatar || "",
displayName: userInfoArray[0].nickname, displayName: userInfoArray[0].nickname || "",
toUserId: config.userInfo.id, toUserId: config.userInfo.id,
chatId: userInfoArray[0].chatId chatId: userInfoArray[0].chatId
} }

Loading…
Cancel
Save