|
@ -1,30 +1,26 @@ |
|
|
import emojiList from "@/assets/emoji.json"; |
|
|
import emojiList from "@/assets/emoji.json"; |
|
|
import { fetchGet, fetchPost, getSystemConfig } from '@/system/config'; |
|
|
import { fetchGet, fetchPost, getSystemConfig } from '@/system/config'; |
|
|
import { notifyError } from "@/util/msg"; |
|
|
import { notifySuccess } from "@/util/msg"; |
|
|
import { defineStore } from 'pinia'; |
|
|
import { defineStore } from 'pinia'; |
|
|
import { db } from "./db"; |
|
|
import { db } from "./db"; |
|
|
|
|
|
|
|
|
export const useChatStore = defineStore('chatStore', () => { |
|
|
export const useChatStore = defineStore('chatStore', () => { |
|
|
|
|
|
|
|
|
interface ChatMessage { |
|
|
interface OnlineUserInfoType { |
|
|
id?: any; |
|
|
id: string; |
|
|
type: any; |
|
|
ip: string; |
|
|
time?: any; |
|
|
avatar: string; |
|
|
message: any; // 消息内容
|
|
|
online: boolean; |
|
|
userId: any; // 发送者id
|
|
|
type: string; |
|
|
toUserId?: any; // 接收者id
|
|
|
chatId: string; |
|
|
to_groupid?: any; |
|
|
username: string; |
|
|
groupId?: any; |
|
|
nickname: string; |
|
|
messageType?: string; // 消息类型,0表示文字消息,1表示图片消息,2表示文件消息
|
|
|
|
|
|
userInfo: { // 发送者信息
|
|
|
|
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 文件消息类型
|
|
|
// 文件消息类型
|
|
|
interface ChatFileMessage { |
|
|
interface ChatMessageType { |
|
|
type: string; |
|
|
type: string; |
|
|
content_type: string; |
|
|
content_type: string; |
|
|
time: Date | null; |
|
|
time: string; |
|
|
userId: number; |
|
|
userId: number; |
|
|
toUserId: number; |
|
|
toUserId: number; |
|
|
message: string; |
|
|
message: string; |
|
@ -33,10 +29,10 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 文件发送模型
|
|
|
// 文件发送模型
|
|
|
const fileMessage: ChatFileMessage = { |
|
|
const Message: ChatMessageType = { |
|
|
type: '', |
|
|
type: '', |
|
|
content_type: '', |
|
|
content_type: '', |
|
|
time: null, |
|
|
time: new Date().toISOString(), |
|
|
userId: 0, |
|
|
userId: 0, |
|
|
toUserId: 0, |
|
|
toUserId: 0, |
|
|
message: '', |
|
|
message: '', |
|
@ -47,6 +43,9 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
// 发起群聊对话框显示
|
|
|
// 发起群聊对话框显示
|
|
|
const groupChatInvitedDialogVisible = ref(false); |
|
|
const groupChatInvitedDialogVisible = ref(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fileSendActive = ref() |
|
|
|
|
|
|
|
|
// 群信息设置抽屉
|
|
|
// 群信息设置抽屉
|
|
|
const groupInfoSettingDrawerVisible = ref(false); |
|
|
const groupInfoSettingDrawerVisible = ref(false); |
|
|
// 设置群聊邀请对话框状态
|
|
|
// 设置群聊邀请对话框状态
|
|
@ -63,20 +62,8 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
const departmentName = ref(''); |
|
|
const departmentName = ref(''); |
|
|
|
|
|
|
|
|
const sendInfo: any = ref() |
|
|
const sendInfo: any = ref() |
|
|
|
|
|
// 在线用户列表
|
|
|
// 定义用户类型
|
|
|
const onlineUserList = ref<OnlineUserInfoType[]>([]); |
|
|
type User = { |
|
|
|
|
|
id: number; |
|
|
|
|
|
ip: string; |
|
|
|
|
|
isOnline: boolean; |
|
|
|
|
|
avatar: string; |
|
|
|
|
|
nickname: string; |
|
|
|
|
|
username: string; |
|
|
|
|
|
updatedAt?: number; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 将 userList 的类型设置为 User[]
|
|
|
|
|
|
const userList = ref<User[]>([]); |
|
|
|
|
|
|
|
|
|
|
|
// 聊天列表
|
|
|
// 聊天列表
|
|
|
const chatList: any = ref([]); |
|
|
const chatList: any = ref([]); |
|
@ -103,6 +90,9 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
const targetChatId = ref(); |
|
|
const targetChatId = ref(); |
|
|
const search = ref(''); |
|
|
const search = ref(''); |
|
|
|
|
|
|
|
|
|
|
|
// 群成员列表
|
|
|
|
|
|
const groupMemberList = ref([]) |
|
|
|
|
|
|
|
|
// 所有用户列表
|
|
|
// 所有用户列表
|
|
|
const allUserList = ref([]) |
|
|
const allUserList = ref([]) |
|
|
|
|
|
|
|
@ -123,20 +113,19 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const initChat = async () => { |
|
|
|
|
|
|
|
|
const initChat = () => { |
|
|
|
|
|
if (config.userInfo.avatar == '') { |
|
|
if (config.userInfo.avatar == '') { |
|
|
config.userInfo.avatar = '/logo.png'; |
|
|
config.userInfo.avatar = '/logo.png'; |
|
|
} |
|
|
} |
|
|
userInfo.value = config.userInfo; |
|
|
userInfo.value = config.userInfo; |
|
|
getUserList() |
|
|
// 初始化用户列表
|
|
|
getGroupList() |
|
|
|
|
|
getDepartmentList() |
|
|
|
|
|
initUserList() |
|
|
initUserList() |
|
|
|
|
|
// 获取群列表
|
|
|
|
|
|
await getGroupList() |
|
|
|
|
|
// 初始化聊天列表
|
|
|
initChatList() |
|
|
initChatList() |
|
|
// initOnlineUserList()
|
|
|
// 获取部门列表
|
|
|
console.log(userList.value); |
|
|
getDepartmentList() |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 获取部门列表
|
|
|
// 获取部门列表
|
|
@ -151,10 +140,15 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
|
|
|
|
|
|
// 初始化用户列表
|
|
|
// 初始化用户列表
|
|
|
const initChatList = async () => { |
|
|
const initChatList = async () => { |
|
|
const userchatList = await db.getAll('conversationList'); |
|
|
const userSessionList = await db.getAll("workbenchSessionList"); |
|
|
console.log(userchatList) |
|
|
// 确保groupList已加载
|
|
|
|
|
|
if (groupList.value.length > 0) { |
|
|
// 合并两个数组
|
|
|
// 合并两个数组
|
|
|
chatList.value = [...userchatList, ...groupList.value]; |
|
|
chatList.value = [...userSessionList, ...groupList.value]; |
|
|
|
|
|
} else { |
|
|
|
|
|
// 如果groupList为空,只使用userSessionList
|
|
|
|
|
|
chatList.value = [...userSessionList]; |
|
|
|
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const setCurrentNavId = (id: number) => { |
|
|
const setCurrentNavId = (id: number) => { |
|
@ -162,113 +156,195 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const sendMessage = async (messageType: string) => { |
|
|
const sendMessage = async (messageType: string) => { |
|
|
|
|
|
console.log("sendMessage:", messageType) |
|
|
if (messageType == 'text') { |
|
|
if (messageType == 'text') { |
|
|
sendTextMessage() |
|
|
await sendTextMessage() |
|
|
|
|
|
} |
|
|
|
|
|
if (messageType == 'image') { |
|
|
|
|
|
sendImageMessage() |
|
|
} |
|
|
} |
|
|
// if (messageType == 'image') {
|
|
|
|
|
|
// sendImageMessage()
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(messageType) |
|
|
if (messageType == 'applyfile') { |
|
|
if (messageType == 'applyfile') { |
|
|
fileMessage.type = "user" |
|
|
|
|
|
fileMessage.content_type = 'file' |
|
|
|
|
|
fileMessage.time = null |
|
|
|
|
|
fileMessage.userId = userInfo.value.id |
|
|
|
|
|
fileMessage.toUserId = targetChatId.value |
|
|
|
|
|
fileMessage.message = sendInfo.value[0] |
|
|
|
|
|
fileMessage.to_groupid = targetGroupInfo.value.group_id || '' |
|
|
|
|
|
try { |
|
|
|
|
|
await sendFileMessage() |
|
|
await sendFileMessage() |
|
|
} catch (error) { |
|
|
|
|
|
notifyError('文件发送失败'); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 发送图片消息
|
|
|
|
|
|
const sendImageMessage = async () => { |
|
|
|
|
|
|
|
|
|
|
|
// 判断是群聊发送还是单聊发送
|
|
|
|
|
|
if (targetGroupInfo.value && Object.keys(targetGroupInfo.value).length > 0) { |
|
|
|
|
|
console.log('群聊发送文件'); |
|
|
|
|
|
Message.type = 'group'; |
|
|
|
|
|
Message.content_type = 'image'; |
|
|
|
|
|
Message.userId = userInfo.value.id; |
|
|
|
|
|
Message.to_groupid = targetGroupInfo.value.group_id; |
|
|
|
|
|
Message.message = sendInfo.value[0]; |
|
|
|
|
|
Message.userInfo = {}; |
|
|
|
|
|
console.log("群聊发送文件", Message) |
|
|
|
|
|
} else if (targetUserInfo.value && Object.keys(targetUserInfo.value).length > 0) { |
|
|
|
|
|
console.log('单聊发送文件'); |
|
|
|
|
|
Message.type = 'user'; |
|
|
|
|
|
Message.content_type = 'image'; |
|
|
|
|
|
Message.userId = userInfo.value.id; |
|
|
|
|
|
Message.toUserId = targetChatId.value; |
|
|
|
|
|
Message.message = sendInfo.value[0]; |
|
|
|
|
|
Message.to_groupid = targetGroupInfo.value?.group_id || ''; |
|
|
|
|
|
Message.userInfo = {}; |
|
|
|
|
|
} |
|
|
|
|
|
console.log(Message) |
|
|
|
|
|
// 发送文件消息
|
|
|
|
|
|
const res = await fetchPost(config.userInfo.url + '/chat/send/file', JSON.stringify(Message)); |
|
|
|
|
|
if (!res.ok) { |
|
|
|
|
|
fileSendActive.value = false; |
|
|
|
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
const data = await res.json(); |
|
|
|
|
|
console.log(data); |
|
|
|
|
|
|
|
|
const sendFileMessage = async () => { |
|
|
// 封装成消息历史记录
|
|
|
|
|
|
const messageHistory = { |
|
|
|
|
|
...Message, |
|
|
|
|
|
isMe: true, |
|
|
|
|
|
chatId: Message.type === 'group' ? targetGroupInfo.value.chatId : targetUserInfo.value.chatId, |
|
|
|
|
|
avatar: Message.type === 'group' ? targetUserInfo.value.avatar : '', |
|
|
|
|
|
createdAt: Date.now() |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
console.log(fileMessage) |
|
|
console.log("messageHistory------", messageHistory) |
|
|
|
|
|
|
|
|
const res = await fetchPost(config.userInfo.url + '/chat/send/file', JSON.stringify(fileMessage)); |
|
|
// 添加到聊天记录
|
|
|
if (res.ok) { |
|
|
await db.addOne("workbenchGroupChatRecord", messageHistory); |
|
|
const data = await res.json(); |
|
|
chatHistory.value.push(messageHistory); |
|
|
console.log(data) |
|
|
fileSendActive.value = true; |
|
|
|
|
|
notifySuccess('文件发送成功'); |
|
|
|
|
|
|
|
|
|
|
|
// 两秒后关闭
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
|
fileSendActive.value = false; |
|
|
|
|
|
}, 2000); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 发送文件消息
|
|
|
|
|
|
const sendFileMessage = async () => { |
|
|
|
|
|
|
|
|
|
|
|
// 判断是群聊发送还是单聊发送
|
|
|
|
|
|
if (targetGroupInfo.value && Object.keys(targetGroupInfo.value).length > 0) { |
|
|
|
|
|
console.log('群聊发送文件'); |
|
|
|
|
|
Message.type = 'group'; |
|
|
|
|
|
Message.content_type = 'file'; |
|
|
|
|
|
Message.userId = userInfo.value.id; |
|
|
|
|
|
Message.to_groupid = targetGroupInfo.value.group_id; |
|
|
|
|
|
Message.message = sendInfo.value[0]; |
|
|
|
|
|
Message.userInfo = {}; |
|
|
|
|
|
console.log("群聊发送文件", Message) |
|
|
|
|
|
} else if (targetUserInfo.value && Object.keys(targetUserInfo.value).length > 0) { |
|
|
|
|
|
console.log('单聊发送文件'); |
|
|
|
|
|
Message.type = 'user'; |
|
|
|
|
|
Message.content_type = 'file'; |
|
|
|
|
|
Message.userId = userInfo.value.id; |
|
|
|
|
|
Message.toUserId = targetChatId.value; |
|
|
|
|
|
Message.message = sendInfo.value[0]; |
|
|
|
|
|
Message.to_groupid = targetGroupInfo.value?.group_id || ''; |
|
|
|
|
|
Message.userInfo = {}; |
|
|
|
|
|
} |
|
|
|
|
|
console.log(Message) |
|
|
|
|
|
// 发送文件消息
|
|
|
|
|
|
const res = await fetchPost(config.userInfo.url + '/chat/send/file', JSON.stringify(Message)); |
|
|
|
|
|
if (!res.ok) { |
|
|
|
|
|
fileSendActive.value = false; |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
const data = await res.json(); |
|
|
|
|
|
console.log(data); |
|
|
|
|
|
|
|
|
|
|
|
// 封装成消息历史记录
|
|
|
|
|
|
const messageHistory = { |
|
|
|
|
|
...Message, |
|
|
|
|
|
isMe: true, |
|
|
|
|
|
chatId: Message.type === 'group' ? targetGroupInfo.value.chatId : targetUserInfo.value.chatId, |
|
|
|
|
|
avatar: Message.type === 'group' ? targetUserInfo.value.avatar : '', |
|
|
|
|
|
createdAt: Date.now() |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
console.log("messageHistory------", messageHistory) |
|
|
|
|
|
|
|
|
|
|
|
// 添加到聊天记录
|
|
|
|
|
|
await db.addOne("workbenchGroupChatRecord", messageHistory); |
|
|
|
|
|
chatHistory.value.push(messageHistory); |
|
|
|
|
|
fileSendActive.value = true; |
|
|
|
|
|
notifySuccess('文件发送成功'); |
|
|
|
|
|
|
|
|
|
|
|
// 两秒后关闭
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
|
fileSendActive.value = false; |
|
|
|
|
|
}, 2000); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 发送文字消息
|
|
|
// 发送文字消息
|
|
|
const sendTextMessage = async () => { |
|
|
const sendTextMessage = async () => { |
|
|
let messageHistory: ChatMessage; |
|
|
|
|
|
|
|
|
|
|
|
// 判断是群聊发送还是单聊发送
|
|
|
// 判断是群聊发送还是单聊发送
|
|
|
if (targetGroupInfo.value && Object.keys(targetGroupInfo.value).length) { |
|
|
if (targetGroupInfo.value && Object.keys(targetGroupInfo.value).length) { |
|
|
console.log('群聊发送'); |
|
|
console.log('群聊发送'); |
|
|
// 群聊发送消息的逻辑
|
|
|
Message.type = 'group' |
|
|
messageHistory = { |
|
|
Message.content_type = 'text' |
|
|
type: 'group', |
|
|
Message.to_groupid = targetGroupInfo.value?.group_id |
|
|
messageType: 'text', // 明确指定消息类型
|
|
|
Message.message = message.value |
|
|
time: Date.now(), |
|
|
Message.userId = userInfo.value.id |
|
|
groupId: targetGroupInfo.value?.group_id, // 使用可选链操作符
|
|
|
Message.userInfo = {} |
|
|
message: message.value, |
|
|
console.log(Message) |
|
|
userId: userInfo.value.id, |
|
|
|
|
|
to_groupid: targetGroupInfo.value?.group_id, // 使用可选链操作符
|
|
|
|
|
|
userInfo: {}, |
|
|
|
|
|
}; |
|
|
|
|
|
} else if (targetUserInfo.value && Object.keys(targetUserInfo.value).length > 0) { |
|
|
} else if (targetUserInfo.value && Object.keys(targetUserInfo.value).length > 0) { |
|
|
console.log('单聊发送'); |
|
|
console.log('单聊发送'); |
|
|
// 单聊发送消息
|
|
|
Message.type = 'user' |
|
|
// 封装成消息历史记录
|
|
|
Message.content_type = 'text' |
|
|
messageHistory = { |
|
|
Message.userId = userInfo.value.id |
|
|
type: 'user', |
|
|
Message.toUserId = targetChatId.value |
|
|
messageType: 'text', // 明确指定消息类型
|
|
|
Message.message = message.value |
|
|
time: Date.now(), |
|
|
Message.content_type = 'text' |
|
|
message: message.value, |
|
|
Message.to_groupid = targetGroupInfo.value?.group_id || '' |
|
|
userId: userInfo.value.id, |
|
|
Message.userInfo = {} |
|
|
toUserId: targetChatId.value, |
|
|
|
|
|
userInfo: {}, |
|
|
|
|
|
}; |
|
|
|
|
|
} else { |
|
|
|
|
|
notifyError('请先选择聊天对象'); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!messageHistory) { |
|
|
|
|
|
// 这是一个额外的检查,确保 messageHistory 已经被赋值
|
|
|
|
|
|
console.log('消息发送失败') |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 创建没有 `id` 属性的副本
|
|
|
|
|
|
const { id, ...messageHistoryWithoutId } = messageHistory; |
|
|
|
|
|
console.log(messageHistoryWithoutId); |
|
|
|
|
|
|
|
|
|
|
|
// 发送消息
|
|
|
// 发送消息
|
|
|
const res = await fetchPost(config.userInfo.url + '/chat/send', JSON.stringify(messageHistory)); |
|
|
const res = await fetchPost(config.userInfo.url + '/chat/send', JSON.stringify(Message)); |
|
|
|
|
|
|
|
|
if (res.ok) { |
|
|
if (res.ok) { |
|
|
|
|
|
// 封装成消息历史记录
|
|
|
|
|
|
var messageHistory |
|
|
// 本地存储一份聊天记录
|
|
|
// 本地存储一份聊天记录
|
|
|
if (messageHistory.type === 'user') { |
|
|
if (Message.type === 'user') { |
|
|
await db.addOne('chatRecord', messageHistory); |
|
|
messageHistory = { |
|
|
// changeChatListAndGetChatHistory(userInfo.value.userId);
|
|
|
...Message, |
|
|
|
|
|
isMe: true, |
|
|
|
|
|
displayName: userInfo.value.nickname, |
|
|
|
|
|
chatId: targetUserInfo.value.chatId.toString(), |
|
|
|
|
|
avatar: userInfo.value.avatar, |
|
|
|
|
|
createdAt: Date.now() |
|
|
|
|
|
} |
|
|
|
|
|
await db.addOne("workbenchChatRecord", messageHistory); |
|
|
|
|
|
|
|
|
} else if (messageHistory.type === 'group') { |
|
|
} else if (Message.type === 'group') { |
|
|
await db.addOne('groupChatRecord', messageHistory); |
|
|
messageHistory = { |
|
|
|
|
|
...Message, |
|
|
|
|
|
isMe: true, |
|
|
|
|
|
chatId: targetGroupInfo.value.chatId.toString(), |
|
|
|
|
|
createdAt: Date.now() |
|
|
|
|
|
} |
|
|
|
|
|
await db.addOne("workbenchGroupChatRecord", messageHistory); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 更新聊天历史
|
|
|
// 更新聊天历史
|
|
|
chatHistory.value.push(messageHistory); |
|
|
chatHistory.value.push(messageHistory); |
|
|
|
|
|
|
|
|
|
|
|
console.log(await res.json()) |
|
|
|
|
|
|
|
|
// 更新 chatList 和 conversationList
|
|
|
|
|
|
// changeChatListAndGetChatHistory(userInfo.value.userId);
|
|
|
|
|
|
|
|
|
|
|
|
// 清空输入框
|
|
|
// 清空输入框
|
|
|
clearMessage(); |
|
|
clearMessage(); |
|
|
|
|
|
|
|
|
// 聊天框滚动到底部
|
|
|
// 聊天框滚动到底部
|
|
|
await setScrollToBottom(); |
|
|
await setScrollToBottom(); |
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
@ -278,39 +354,39 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
const changeChatListAndChatHistory = async (data: any) => { |
|
|
const changeChatListAndChatHistory = async (data: any) => { |
|
|
|
|
|
|
|
|
// 从 conversationList 数据库中查找是否存在对应的会话
|
|
|
// 从 conversationList 数据库中查找是否存在对应的会话
|
|
|
const conversation = await db.getByField('conversationList', 'userId', data.userId); |
|
|
const conversation = await db.getByField("workbenchSessionList", 'userId', data.userId); |
|
|
|
|
|
|
|
|
// 准备会话更新数据
|
|
|
// 准备会话更新数据
|
|
|
const updatedConversation = { |
|
|
const updatedConversation = { |
|
|
userId: data.userId, |
|
|
userId: data.userId, |
|
|
avatar: data.userInfo.avatar || "logo.png", // 如果没有头像使用默认图片
|
|
|
avatar: data.userInfo.avatar, // 如果没有头像使用默认图片
|
|
|
toUserId: data.toUserId, |
|
|
toUserId: data.toUserId, |
|
|
chatId: data.userId, |
|
|
chatId: data.userId, |
|
|
type: data.type, |
|
|
type: data.type, |
|
|
messages: data.message, |
|
|
messages: data.message, |
|
|
displayName: data.userInfo.nickname, |
|
|
displayName: data.userInfo.nickname, |
|
|
nickname: data.userInfo.nickname, |
|
|
nickname: data.userInfo.nickname, |
|
|
time: data.time || Date.now(), |
|
|
time: Date.now().toString, |
|
|
previewMessage: data.message, |
|
|
// previewMessage: data.message,
|
|
|
previewTimeFormat: formatTime(Date.now()), // 时间格式化函数
|
|
|
// previewTimeFormat: formatTime(Date.now()), // 时间格式化函数
|
|
|
createdAt: Date.now() |
|
|
createdAt: Date.now() |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
if (conversation.length === 0) { |
|
|
if (conversation.length === 0) { |
|
|
// 如果会话不存在,则添加到数据库和 chatList
|
|
|
// 如果会话不存在,则添加到数据库和 chatList
|
|
|
await db.addOne('conversationList', updatedConversation); |
|
|
await db.addOne('workbenchSessionList', updatedConversation); |
|
|
|
|
|
|
|
|
chatList.value.push(updatedConversation); |
|
|
chatList.value.push(updatedConversation); |
|
|
} else { |
|
|
} else { |
|
|
// 如果会话存在,则更新数据库和 chatList
|
|
|
// 如果会话存在,则更新数据库和 chatList
|
|
|
// 只更新变化的字段,而非全部覆盖,以减少写入数据的量
|
|
|
// 只更新变化的字段,而非全部覆盖,以减少写入数据的量
|
|
|
await db.update('conversationList', conversation[0].id, { |
|
|
await db.update('workbenchSessionList', conversation[0].id, { |
|
|
avatar: data.userInfo.avatar || "logo.png", |
|
|
avatar: data.userInfo.avatar, |
|
|
nickname: data.userInfo.nickname, |
|
|
nickname: data.userInfo.nickname, |
|
|
displayName: data.userInfo.nickname, |
|
|
displayName: data.userInfo.nickname, |
|
|
previewMessage: data.message, |
|
|
// previewMessage: data.message,
|
|
|
time: data.time || Date.now(), |
|
|
time: data.time || Date.now().toString, |
|
|
previewTimeFormat: formatTime(Date.now()) |
|
|
// previewTimeFormat: formatTime(Date.now())
|
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -326,8 +402,8 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 点击目标用户时,将其添加到 chatList,并添加到数据库
|
|
|
// 点击目标用户时,将其添加到 chatList,并添加到数据库,以及获取到聊天记录
|
|
|
const changeChatListAndGetChatHistory = async (chatId: number) => { |
|
|
const addChatListAndGetChatHistory = async (chatId: string) => { |
|
|
const chatIdSet = new Set(chatList.value.map((chat: { chatId: any }) => chat.chatId)); |
|
|
const chatIdSet = new Set(chatList.value.map((chat: { chatId: any }) => chat.chatId)); |
|
|
|
|
|
|
|
|
// 如果会话存在于 chatList,则获取聊天记录并更新 chatHistory
|
|
|
// 如果会话存在于 chatList,则获取聊天记录并更新 chatHistory
|
|
@ -336,30 +412,24 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
chatHistory.value = await getHistory(chatId, userInfo.value.id, "user"); |
|
|
chatHistory.value = await getHistory(chatId, userInfo.value.id, "user"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
console.log("不存在") |
|
|
// 如果会话不存在,则从用户表中获取该用户的基本信息
|
|
|
// 如果会话不存在,则从用户表中获取该用户的基本信息
|
|
|
const user = await db.getOne("workbenchusers", chatId); |
|
|
const user = await db.getOne("workbenchChatUser", Number(chatId)); |
|
|
if (!user) { |
|
|
|
|
|
console.warn("User not found in workbenchusers with userId:", chatId); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 将新用户信息添加到 chatList
|
|
|
// 将新用户信息添加到 chatList
|
|
|
const newChat = { |
|
|
const newChat = { |
|
|
userId: user.id, |
|
|
|
|
|
type: "user", |
|
|
type: "user", |
|
|
chatId: user.id, |
|
|
chatId: user.id, |
|
|
nickname: user.nickname, |
|
|
nickname: user.nickname, |
|
|
avatar: user.avatar, |
|
|
avatar: user.avatar, |
|
|
|
|
|
toUserId: userInfo.value.id, |
|
|
|
|
|
time: Date.now(), |
|
|
displayName: user.nickname, |
|
|
displayName: user.nickname, |
|
|
previewTimeFormat: formatTime(Date.now()), |
|
|
|
|
|
previewMessage: "", |
|
|
|
|
|
}; |
|
|
}; |
|
|
chatList.value.push(newChat); |
|
|
chatList.value.push(newChat); |
|
|
|
|
|
|
|
|
// 持久化数据到数据库
|
|
|
// 持久化数据到数据库
|
|
|
await db.addOne("conversationList", { |
|
|
await db.addOne("workbenchSessionList", { |
|
|
userId: user.id, |
|
|
|
|
|
type: "user", |
|
|
type: "user", |
|
|
chatId: user.id, |
|
|
chatId: user.id, |
|
|
displayName: user.nickname, |
|
|
displayName: user.nickname, |
|
@ -368,9 +438,11 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
avatar: user.avatar, |
|
|
avatar: user.avatar, |
|
|
toUserId: userInfo.value.id, |
|
|
toUserId: userInfo.value.id, |
|
|
time: Date.now(), |
|
|
time: Date.now(), |
|
|
previewMessage: "", |
|
|
|
|
|
createdAt: Date.now(), |
|
|
createdAt: Date.now(), |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 获取聊天记录
|
|
|
|
|
|
chatHistory.value = await getHistory(chatId, userInfo.value.id, "user"); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -402,7 +474,7 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
|
|
|
|
|
|
// 获取用户表中所有用户
|
|
|
// 获取用户表中所有用户
|
|
|
const getAllUser = async () => { |
|
|
const getAllUser = async () => { |
|
|
allUserList.value = await db.getAll("workbenchusers"); |
|
|
allUserList.value = await db.getAll("workbenchChatUser"); |
|
|
console.log(allUserList.value) |
|
|
console.log(allUserList.value) |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
@ -423,24 +495,18 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
if (!res.ok) { |
|
|
if (!res.ok) { |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// getGroupList()
|
|
|
|
|
|
// initChatList()
|
|
|
const groupData = await res.json(); |
|
|
const groupData = await res.json(); |
|
|
console.log(groupData) |
|
|
console.log(groupData) |
|
|
// 构建数据入库
|
|
|
// 构建数据入库
|
|
|
// 群数据
|
|
|
// 群数据
|
|
|
const group_id = groupData.data.group_id |
|
|
const group_id = groupData.data.group_id |
|
|
|
|
|
|
|
|
// 群成员数据
|
|
|
|
|
|
const groupMembers = { |
|
|
|
|
|
userId: currUserId, |
|
|
|
|
|
groupId: group_id, |
|
|
|
|
|
createdAt: new Date() |
|
|
|
|
|
} |
|
|
|
|
|
db.addOne("groupMembers", groupMembers) |
|
|
|
|
|
|
|
|
|
|
|
// 添加到会话列表中
|
|
|
// 添加到会话列表中
|
|
|
const groupConversation = { |
|
|
const groupConversation = { |
|
|
group_id: group_id, |
|
|
group_id: group_id, |
|
|
name: departmentName.value, |
|
|
|
|
|
avatar: "./logo.png", |
|
|
avatar: "./logo.png", |
|
|
messages: "", |
|
|
messages: "", |
|
|
chatId: group_id, |
|
|
chatId: group_id, |
|
@ -450,7 +516,8 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
previewTimeFormat: formatTime(Date.now()), |
|
|
previewTimeFormat: formatTime(Date.now()), |
|
|
createdAt: new Date() |
|
|
createdAt: new Date() |
|
|
} |
|
|
} |
|
|
chatList.value.push(groupConversation) |
|
|
groupList.value.push(groupConversation) |
|
|
|
|
|
initChatList() |
|
|
// 关闭对话弹窗
|
|
|
// 关闭对话弹窗
|
|
|
setGroupChatInvitedDialogVisible(false) |
|
|
setGroupChatInvitedDialogVisible(false) |
|
|
}; |
|
|
}; |
|
@ -462,7 +529,8 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
// 更新会话列表数据库
|
|
|
// 更新会话列表数据库
|
|
|
// 更新chatlist
|
|
|
// 更新chatlist
|
|
|
// 更新聊天记录
|
|
|
// 更新聊天记录
|
|
|
const isPresence = await db.getByField('workbenchusers', 'id', data.userId) |
|
|
console.log("userChatMessage:", data) |
|
|
|
|
|
const isPresence = await db.getByField('workbenchChatUser', 'chatId', data.userId) |
|
|
if (isPresence[0].id !== data.userId) { |
|
|
if (isPresence[0].id !== data.userId) { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
@ -474,22 +542,16 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
userId: data.userId, |
|
|
userId: data.userId, |
|
|
message: data.message, |
|
|
message: data.message, |
|
|
toUserId: data.toUserId, |
|
|
toUserId: data.toUserId, |
|
|
chatId: data.userId, |
|
|
chatId: data.toUserId, |
|
|
|
|
|
isMe: false, |
|
|
|
|
|
content_type: data.content_type, |
|
|
|
|
|
to_groupid: data.to_groupid, |
|
|
displayName: data.userInfo.nickname, |
|
|
displayName: data.userInfo.nickname, |
|
|
|
|
|
avatar: data.userInfo.avatar, |
|
|
createdAt: Date.now(), |
|
|
createdAt: Date.now(), |
|
|
// 用户信息
|
|
|
|
|
|
userInfo: { |
|
|
|
|
|
id: data.userId, |
|
|
|
|
|
nickname: data.userInfo.nickname || "未知用户", |
|
|
|
|
|
avatar: data.userInfo.avatar || "logo.png", // 使用默认头像。
|
|
|
|
|
|
email: data.userInfo.email, |
|
|
|
|
|
phone: data.userInfo.phone, |
|
|
|
|
|
remark: data.userInfo.remark, |
|
|
|
|
|
role_id: data.userInfo.role_id, |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
await db.addOne('chatRecord', addMessageHistory) |
|
|
await db.addOne("workbenchChatRecord", addMessageHistory) |
|
|
|
|
|
|
|
|
console.log("添加成功") |
|
|
console.log("添加成功") |
|
|
// 更新 chatList 和 conversationList表
|
|
|
// 更新 chatList 和 conversationList表
|
|
@ -518,40 +580,60 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
}; |
|
|
}; |
|
|
// 获取群列表信息
|
|
|
// 获取群列表信息
|
|
|
const getGroupList = async () => { |
|
|
const getGroupList = async () => { |
|
|
console.log('获取群列表') |
|
|
|
|
|
const res = await fetchGet(userInfo.value.url + '/chat/group/list'); |
|
|
const res = await fetchGet(userInfo.value.url + '/chat/group/list'); |
|
|
if (!res.ok) { |
|
|
if (!res.ok) { |
|
|
console.warn("Error fetching group list:", res); |
|
|
console.warn("Error fetching group list:", res); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
const list = await res.json() |
|
|
const list = await res.json() |
|
|
console.log(list.data.groups) |
|
|
console.log(list) |
|
|
|
|
|
if (list.data.groups == null) { |
|
|
|
|
|
list.data.groups = [] |
|
|
|
|
|
} |
|
|
// 封装 list.data.groups
|
|
|
// 封装 list.data.groups
|
|
|
const formattedGroups = list.data.groups.map((group: any) => ({ |
|
|
const formattedGroups = list.data.groups.map((group: any) => ({ |
|
|
group_id: group.id, |
|
|
group_id: group.id, |
|
|
name: group.name, |
|
|
|
|
|
avatar: group.avatar || '', // 使用默认头像
|
|
|
avatar: group.avatar || '', // 使用默认头像
|
|
|
messages: "", |
|
|
// messages: "",
|
|
|
displayName: group.name, |
|
|
displayName: group.name, |
|
|
chatId: group.id, |
|
|
chatId: group.id, |
|
|
type: 'group', |
|
|
type: 'group', |
|
|
previewMessage: "", |
|
|
|
|
|
previewTimeFormat: formatTime(Date.now()), |
|
|
|
|
|
createdAt: group.createdAt |
|
|
|
|
|
})); |
|
|
})); |
|
|
groupList.value = formattedGroups; |
|
|
groupList.value = formattedGroups; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const handleUserData = async (data: any[]) => { |
|
|
// 将群成员添加到数据库
|
|
|
|
|
|
for (const group of list.data.groups) { |
|
|
|
|
|
// 查询数据库中是否已存在该群组的成员列表
|
|
|
|
|
|
const existingGroup = await db.getByField("workbenchGroupUserList", "group_id", group.id); |
|
|
|
|
|
if (existingGroup.length > 0) { |
|
|
|
|
|
// 如果存在,更新userIdArray
|
|
|
|
|
|
await db.update("workbenchGroupUserList", existingGroup[0].id, { |
|
|
|
|
|
userIdArray: group.members |
|
|
|
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
// 如果不存在,添加新记录
|
|
|
|
|
|
await db.addOne("workbenchGroupUserList", { |
|
|
|
|
|
group_id: group.id, |
|
|
|
|
|
userIdArray: group.members |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const onlineUserData = async (data: OnlineUserInfoType[]) => { |
|
|
// 创建一个用户数组,将所有在线的用户提取出来
|
|
|
// 创建一个用户数组,将所有在线的用户提取出来
|
|
|
const users: any[] = []; |
|
|
const onlineUsers: OnlineUserInfoType[] = []; |
|
|
// 遍历每个数据项
|
|
|
// 遍历每个数据项
|
|
|
data.forEach((item: any) => { |
|
|
data.forEach((item: any) => { |
|
|
if (item.id && item.login_ip) { |
|
|
if (item.id && item.login_ip) { |
|
|
users.push({ |
|
|
onlineUsers.push({ |
|
|
id: item.id, |
|
|
id: item.id, |
|
|
ip: item.login_ip, |
|
|
ip: item.login_ip, |
|
|
|
|
|
type: "user", |
|
|
|
|
|
chatId: item.id, |
|
|
|
|
|
online: true, |
|
|
avatar: item.avatar, |
|
|
avatar: item.avatar, |
|
|
username: item.username, |
|
|
username: item.username, |
|
|
nickname: item.nickname |
|
|
nickname: item.nickname |
|
@ -559,22 +641,21 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 将提取到的用户数据传递给
|
|
|
// 将提取到的用户数据传递给 setUserList
|
|
|
if (onlineUsers.length > 0) { |
|
|
if (users.length > 0) { |
|
|
await setOnlineUserList(onlineUsers); |
|
|
await setUserList(users); |
|
|
|
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const setUserList = async (data: any[]) => { |
|
|
const setOnlineUserList = async (data: OnlineUserInfoType[]) => { |
|
|
if (data.length < 1) { |
|
|
if (data.length < 1) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 从当前用户列表中获取已有用户的 IP 和完整用户映射
|
|
|
// 从当前用户列表中获取已有用户的 IP 和完整用户映射
|
|
|
const existingIps = new Set(userList.value.map((d: any) => d.ip)); |
|
|
const existingIps = new Set(onlineUserList.value.map((d: any) => d.ip)); |
|
|
const userMap = new Map<string, User>( |
|
|
const userMap = new Map<string, OnlineUserInfoType>( |
|
|
userList.value.map((user: User) => [user.ip, user]) |
|
|
onlineUserList.value.map((user: OnlineUserInfoType) => [user.ip, user]) |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
const updates: any[] = []; |
|
|
const updates: any[] = []; |
|
@ -600,6 +681,8 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
newEntries.push({ |
|
|
newEntries.push({ |
|
|
id: d.id, |
|
|
id: d.id, |
|
|
ip: d.ip, |
|
|
ip: d.ip, |
|
|
|
|
|
type: "user", |
|
|
|
|
|
chatId: d.id, |
|
|
isOnline: true, |
|
|
isOnline: true, |
|
|
avatar: d.avatar, |
|
|
avatar: d.avatar, |
|
|
nickname: d.nickname, |
|
|
nickname: d.nickname, |
|
@ -613,20 +696,18 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
|
|
|
|
|
|
// 批量更新和添加用户数据
|
|
|
// 批量更新和添加用户数据
|
|
|
if (updates.length > 0) { |
|
|
if (updates.length > 0) { |
|
|
await db.table('workbenchusers').bulkUpdate(updates); |
|
|
await db.table("workbenchChatUser").bulkUpdate(updates); |
|
|
} |
|
|
} |
|
|
if (newEntries.length > 0) { |
|
|
if (newEntries.length > 0) { |
|
|
await db.table('workbenchusers').bulkPut(newEntries); |
|
|
await db.table("workbenchChatUser").bulkPut(newEntries); |
|
|
} |
|
|
} |
|
|
|
|
|
initOnlineUserList() |
|
|
// 刷新用户列表
|
|
|
|
|
|
await getUserList(); |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const getUserList = async () => { |
|
|
const initOnlineUserList = async () => { |
|
|
try { |
|
|
|
|
|
// 从数据库中获取所有用户信息
|
|
|
// 从数据库中获取所有用户信息
|
|
|
const list = await db.getAll("workbenchusers"); |
|
|
const list = await db.getAll("workbenchChatUser"); |
|
|
// 创建一个 Map,用于存储每个用户的唯一 ID 地址
|
|
|
// 创建一个 Map,用于存储每个用户的唯一 ID 地址
|
|
|
let uniqueIdMap = new Map<string, any>(); |
|
|
let uniqueIdMap = new Map<string, any>(); |
|
|
|
|
|
|
|
@ -637,22 +718,18 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
|
|
|
|
|
|
// 将 Map 的值转换为数组(去重后的用户列表)
|
|
|
// 将 Map 的值转换为数组(去重后的用户列表)
|
|
|
const uniqueIdList = Array.from(uniqueIdMap.values()); |
|
|
const uniqueIdList = Array.from(uniqueIdMap.values()); |
|
|
|
|
|
|
|
|
// 按照 updatedAt 时间进行升序排序
|
|
|
// 按照 updatedAt 时间进行升序排序
|
|
|
uniqueIdList.sort((a: any, b: any) => a.time - b.time); |
|
|
uniqueIdList.sort((a: any, b: any) => a.time - b.time); |
|
|
// 更新用户列表
|
|
|
// 更新用户列表
|
|
|
userList.value = uniqueIdList; |
|
|
onlineUserList.value = uniqueIdList; |
|
|
} catch (error) { |
|
|
|
|
|
console.error("获取用户列表失败:", error); |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 初始化统一用户列表状态
|
|
|
// 初始化统一用户列表状态
|
|
|
const initUserList = async () => { |
|
|
const initUserList = async () => { |
|
|
if (!userList.value.length) return; |
|
|
if (!onlineUserList.value.length) return; |
|
|
// 获取需要更新的用户数据(只选取在线的用户并设为离线状态)
|
|
|
// 获取需要更新的用户数据(只选取在线的用户并设为离线状态)
|
|
|
const updates = userList.value.reduce((acc: any[], user: any) => { |
|
|
const updates = onlineUserList.value.reduce((acc: any[], user: any) => { |
|
|
console.log(user); |
|
|
|
|
|
if (user.isOnline) { |
|
|
if (user.isOnline) { |
|
|
console.log(user); |
|
|
console.log(user); |
|
|
acc.push({ |
|
|
acc.push({ |
|
@ -665,63 +742,31 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
|
|
|
|
|
|
// 如果有需要更新的用户,批量更新数据库状态
|
|
|
// 如果有需要更新的用户,批量更新数据库状态
|
|
|
if (updates.length) { |
|
|
if (updates.length) { |
|
|
await db.table('workbenchusers').bulkUpdate(updates); |
|
|
await db.table('workbenchChatUser').bulkUpdate(updates); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 设置会话列表
|
|
|
const initOnlineUserList = async () => { |
|
|
const getSessionInfo = async (chatId: string, type: string) => { |
|
|
const msgAll = await db.getAll('workbenchusers'); |
|
|
|
|
|
|
|
|
|
|
|
const list = msgAll.reduce((acc: any, msg: any) => { |
|
|
|
|
|
if (!msg.isMe) { |
|
|
|
|
|
const targetId = msg.targetId; |
|
|
|
|
|
if (!acc[targetId]) { |
|
|
|
|
|
acc[targetId] = { chatArr: [], readNum: 0 }; |
|
|
|
|
|
} |
|
|
|
|
|
acc[targetId].chatArr.push(msg); |
|
|
|
|
|
// 计算未读消息数量
|
|
|
|
|
|
if (!msg.isRead) { |
|
|
|
|
|
acc[targetId].readNum++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return acc; |
|
|
|
|
|
}, {}); |
|
|
|
|
|
|
|
|
|
|
|
const res = Object.keys(list).map(targetId => { |
|
|
|
|
|
const { chatArr, readNum } = list[targetId]; |
|
|
|
|
|
const lastMessage = chatArr[chatArr.length - 1]; |
|
|
|
|
|
if (lastMessage) { |
|
|
|
|
|
lastMessage.readNum = readNum; |
|
|
|
|
|
return lastMessage; |
|
|
|
|
|
} |
|
|
|
|
|
return null; // 防止返回空值
|
|
|
|
|
|
}).filter(Boolean); // 过滤掉空值
|
|
|
|
|
|
|
|
|
|
|
|
userList.value = res.sort((a, b) => b.createdAt - a.createdAt); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const changeChatList = async (chatId: number, type: string) => { |
|
|
|
|
|
|
|
|
|
|
|
// 设置 targetUserId
|
|
|
// 设置 targetUserId
|
|
|
// 根据type去判断
|
|
|
// 根据type去判断
|
|
|
// user会话,查发送和接收发方id
|
|
|
// user会话,查发送和接收发方id
|
|
|
// group会话,查群id(chatId)查到所有消息记录
|
|
|
// group会话,查群id(chatId)查到所有消息记录
|
|
|
|
|
|
// 清空聊天记录
|
|
|
|
|
|
chatHistory.value = [] |
|
|
targetChatId.value = chatId |
|
|
targetChatId.value = chatId |
|
|
if (type === 'user') { |
|
|
if (type === 'user') { |
|
|
console.log("user") |
|
|
console.log("user") |
|
|
// 获取当前用户和目标用户的聊天记录
|
|
|
// 获取当前用户和目标用户的聊天记录
|
|
|
console.log(chatId, type) |
|
|
|
|
|
const history = await getHistory(userInfo.value.id, chatId, type) |
|
|
const history = await getHistory(userInfo.value.id, chatId, type) |
|
|
console.log(history) |
|
|
console.log(history) |
|
|
chatHistory.value = history; |
|
|
chatHistory.value = [...history]; |
|
|
// 设置目标用户的信息
|
|
|
// 设置目标用户的信息
|
|
|
await setTargetUserInfo(chatId); |
|
|
await setTargetUserInfo(chatId); |
|
|
} else if (type === 'group') { |
|
|
} else if (type === 'group') { |
|
|
console.log('group') |
|
|
console.log('group') |
|
|
// 获取当前用户和目标用户的聊天记录
|
|
|
// 获取当前用户和目标用户的聊天记录
|
|
|
|
|
|
console.log(userInfo.value.id, chatId, type) |
|
|
const history = await getHistory(userInfo.value.id, chatId, type) |
|
|
const history = await getHistory(userInfo.value.id, chatId, type) |
|
|
chatHistory.value = history; |
|
|
chatHistory.value = history; |
|
|
// 设置目标用户的信息
|
|
|
// 设置目标用户的信息
|
|
@ -729,10 +774,10 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const getHistory = async (sendUserId: number, toUserId: number, type: string) => { |
|
|
const getHistory = async (sendUserId: string, toUserId: string, type: string) => { |
|
|
var messagesHistory |
|
|
var messagesHistory |
|
|
if (type === 'user') { |
|
|
if (type === 'user') { |
|
|
messagesHistory = await db.filter('chatRecord', (record: any) => { |
|
|
messagesHistory = await db.filter("workbenchChatRecord", (record: any) => { |
|
|
return ( |
|
|
return ( |
|
|
(record.userId === sendUserId && record.toUserId === toUserId) || |
|
|
(record.userId === sendUserId && record.toUserId === toUserId) || |
|
|
(record.toUserId === sendUserId && record.userId === toUserId) |
|
|
(record.toUserId === sendUserId && record.userId === toUserId) |
|
@ -740,21 +785,30 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
}); |
|
|
}); |
|
|
} else if (type === 'group') { |
|
|
} else if (type === 'group') { |
|
|
console.log('group') |
|
|
console.log('group') |
|
|
messagesHistory = await db.getByField('groupChatRecord', "groupId", toUserId); |
|
|
messagesHistory = await db.getByField("workbenchGroupChatRecord", "chatId", toUserId); |
|
|
|
|
|
console.log("messagesHistory", messagesHistory) |
|
|
} |
|
|
} |
|
|
return messagesHistory |
|
|
return messagesHistory |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 设置目标用户的信息
|
|
|
// 设置目标用户的信息
|
|
|
const setTargetUserInfo = async (id: number) => { |
|
|
const setTargetUserInfo = async (id: string) => { |
|
|
console.log(id) |
|
|
console.log(id) |
|
|
const userInfoArray = await db.getByField('conversationList', "userId", id); |
|
|
const userInfoArray = await db.getByField("workbenchChatUser", "chatId", id); |
|
|
targetUserInfo.value = userInfoArray.length > 0 ? userInfoArray[0] : {}; |
|
|
// 封装用户信息
|
|
|
|
|
|
const userInfo = { |
|
|
|
|
|
type: "user", |
|
|
|
|
|
avatar: userInfoArray[0].avatar, |
|
|
|
|
|
displayName: userInfoArray[0].nickname, |
|
|
|
|
|
toUserId: config.userInfo.id, |
|
|
|
|
|
chatId: userInfoArray[0].chatId |
|
|
|
|
|
} |
|
|
|
|
|
targetUserInfo.value = userInfoArray.length > 0 ? userInfo : {}; |
|
|
targetGroupInfo.value = {} |
|
|
targetGroupInfo.value = {} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 设置目标群信息
|
|
|
// 设置目标群信息
|
|
|
const setTargetGrouprInfo = async (id: number) => { |
|
|
const setTargetGrouprInfo = async (id: string) => { |
|
|
for (const group of groupList.value) { |
|
|
for (const group of groupList.value) { |
|
|
console.log(group) |
|
|
console.log(group) |
|
|
if (group.group_id === id) { |
|
|
if (group.group_id === id) { |
|
@ -775,34 +829,31 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
const messageRecord = { |
|
|
const messageRecord = { |
|
|
userId: data.userId, |
|
|
userId: data.userId, |
|
|
groupId: data.to_groupid, |
|
|
groupId: data.to_groupid, |
|
|
messageType: data.messageType, |
|
|
content_type: data.content_type, |
|
|
message: data.message, |
|
|
message: data.message, |
|
|
time: data.time, |
|
|
time: data.time, |
|
|
type: data.type, |
|
|
type: data.type, |
|
|
createdAt: Date.now(), |
|
|
chatId: data.to_groupid, |
|
|
userInfo: { |
|
|
isMe: false, |
|
|
id: data.userId, |
|
|
displayName: data.userInfo.nickname, |
|
|
nickname: data.userInfo.nickname || "未知用户", |
|
|
avatar: data.userInfo.avatar, |
|
|
avatar: data.userInfo.avatar || "logo.png", // 使用默认头像。
|
|
|
|
|
|
email: data.userInfo.email, |
|
|
|
|
|
phone: data.userInfo.phone, |
|
|
|
|
|
remark: data.userInfo.remark, |
|
|
|
|
|
role_id: data.userInfo.role_id, |
|
|
role_id: data.userInfo.role_id, |
|
|
} |
|
|
createdAt: Date.now(), |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 判断当前消息是否是自己发送的
|
|
|
|
|
|
if (messageRecord.userId === userInfo.value.id) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
console.log(messageRecord) |
|
|
console.log(messageRecord) |
|
|
|
|
|
|
|
|
// 将消息记录添加到数据库
|
|
|
// 将消息记录添加到数据库
|
|
|
await db.addOne('groupChatRecord', messageRecord); |
|
|
const res = await db.addOne("workbenchGroupChatRecord", messageRecord); |
|
|
|
|
|
console.log(res) |
|
|
// 更改聊天记录
|
|
|
// 更改聊天记录
|
|
|
chatHistory.value.push(messageRecord) |
|
|
chatHistory.value.push(messageRecord) |
|
|
|
|
|
|
|
|
// // 更新 chatHistory
|
|
|
|
|
|
// chatHistory.value.push(messageRecord);
|
|
|
|
|
|
|
|
|
|
|
|
// // 更新 chatList 和 conversationList表
|
|
|
|
|
|
// changeChatListAndChatHistory(data);
|
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const showContextMenu = (event: any, id: number) => { |
|
|
const showContextMenu = (event: any, id: number) => { |
|
@ -812,9 +863,36 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
contextMenu.value.y = event.y; |
|
|
contextMenu.value.y = event.y; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 退出群聊
|
|
|
|
|
|
const quitGroup = async (group_id: string) => { |
|
|
|
|
|
const url = config.userInfo.url + '/chat/group/leave'; |
|
|
|
|
|
const res = await fetchPost(url, JSON.stringify({ group_id })) |
|
|
|
|
|
if (!res.ok) { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
console.log(await res.json()) |
|
|
|
|
|
// 从groupList中删除
|
|
|
|
|
|
groupList.value = groupList.value.filter((group: any) => group.group_id !== group_id) |
|
|
|
|
|
const a = await db.deleteByField("workbenchGroupUserList", "group_id", group_id) |
|
|
|
|
|
console.log(a) |
|
|
|
|
|
initChatList() |
|
|
|
|
|
notifySuccess("退出群聊成功") |
|
|
|
|
|
|
|
|
|
|
|
targetGroupInfo.value = {} |
|
|
|
|
|
targetChatId.value = '' |
|
|
|
|
|
|
|
|
|
|
|
// const group = await db.getByField("workbenchGroupUserList", "group_id", group_id);
|
|
|
|
|
|
// if (group.length > 0) {
|
|
|
|
|
|
// // 删除当前用户
|
|
|
|
|
|
// console.log(group[0])
|
|
|
|
|
|
// const updatedUserIdArray = group[0].userIdArray.filter((user: any) => user.id !== userInfo.value.id);
|
|
|
|
|
|
// await db.update("workbenchGroupUserList", group[0].id, { userIdArray: updatedUserIdArray });
|
|
|
|
|
|
// }
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
emojiList, |
|
|
emojiList, |
|
|
userList, |
|
|
onlineUserList, |
|
|
chatList, |
|
|
chatList, |
|
|
groupList, |
|
|
groupList, |
|
|
chatHistory, |
|
|
chatHistory, |
|
@ -838,22 +916,24 @@ export const useChatStore = defineStore('chatStore', () => { |
|
|
departmentName, |
|
|
departmentName, |
|
|
targetGroupInfo, |
|
|
targetGroupInfo, |
|
|
sendInfo, |
|
|
sendInfo, |
|
|
|
|
|
fileSendActive, |
|
|
|
|
|
groupMemberList, |
|
|
initChat, |
|
|
initChat, |
|
|
showContextMenu, |
|
|
showContextMenu, |
|
|
setCurrentNavId, |
|
|
setCurrentNavId, |
|
|
sendMessage, |
|
|
sendMessage, |
|
|
changeChatList, |
|
|
getSessionInfo, |
|
|
handleContextMenu, |
|
|
handleContextMenu, |
|
|
changeChatListAndGetChatHistory, |
|
|
addChatListAndGetChatHistory, |
|
|
handleUserData, |
|
|
|
|
|
initUserList, |
|
|
initUserList, |
|
|
setGroupChatInvitedDialogVisible, |
|
|
setGroupChatInvitedDialogVisible, |
|
|
setGroupInfoDrawerVisible, |
|
|
setGroupInfoDrawerVisible, |
|
|
createGroupChat, |
|
|
createGroupChat, |
|
|
|
|
|
onlineUserData, |
|
|
|
|
|
groupChatMessage, |
|
|
userChatMessage, |
|
|
userChatMessage, |
|
|
initOnlineUserList, |
|
|
|
|
|
getDepartmentList, |
|
|
getDepartmentList, |
|
|
getAllUser, |
|
|
getAllUser, |
|
|
groupChatMessage |
|
|
quitGroup, |
|
|
}; |
|
|
}; |
|
|
}); |
|
|
}); |