优化了本地存储和一些组件细分,完成了复制图片、适配可以直接发送的功能
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { defineStore } from "pinia"
|
||||
import { ref, computed } from "vue"
|
||||
import { chatDB } from "@/utils/db"
|
||||
|
||||
export const useChatStore = defineStore("chat", () => {
|
||||
const friends = ref([])
|
||||
@@ -37,80 +38,121 @@ export const useChatStore = defineStore("chat", () => {
|
||||
}
|
||||
})
|
||||
|
||||
// 获取聊天历史记录
|
||||
const getChatHistory = (friendId, currentUserId) => {
|
||||
const chatKey = `chat_${currentUserId}_${friendId}`
|
||||
const chatData = localStorage.getItem(chatKey)
|
||||
return chatData ? JSON.parse(chatData) : []
|
||||
}
|
||||
|
||||
// 保存聊天历史记录
|
||||
const saveChatHistory = (friendId, currentUserId, messageList) => {
|
||||
const chatKey = `chat_${currentUserId}_${friendId}`
|
||||
localStorage.setItem(chatKey, JSON.stringify(messageList))
|
||||
// 初始化数据库
|
||||
const initDB = async () => {
|
||||
try {
|
||||
await chatDB.init()
|
||||
console.log("数据库初始化成功")
|
||||
} catch (error) {
|
||||
console.error("数据库初始化失败:", error)
|
||||
}
|
||||
}
|
||||
|
||||
// 加载好友列表
|
||||
const loadFriends = (presetUsers, currentUserId) => {
|
||||
const friendList = presetUsers.filter((user) => user.id !== currentUserId)
|
||||
const loadFriends = async (presetUsers, currentUserId) => {
|
||||
try {
|
||||
// 从数据库获取好友列表
|
||||
let friendList = await chatDB.getFriends(currentUserId)
|
||||
|
||||
friendList.forEach((friend) => {
|
||||
const chatHistory = getChatHistory(friend.id, currentUserId)
|
||||
// 如果数据库中没有好友,使用预设用户初始化
|
||||
if (friendList.length === 0) {
|
||||
friendList = presetUsers.filter((user) => user.id !== currentUserId)
|
||||
|
||||
if (chatHistory.length > 0) {
|
||||
const lastMsg = chatHistory[chatHistory.length - 1]
|
||||
let lastMessage = lastMsg.content
|
||||
if (lastMsg.type === "image") lastMessage = "[图片]"
|
||||
if (lastMsg.type === "video") lastMessage = "[视频]"
|
||||
if (lastMsg.type === "audio") lastMessage = "[语音]"
|
||||
|
||||
friend.lastMessage = lastMessage.length > 20 ? lastMessage.substring(0, 20) + "..." : lastMessage
|
||||
// 保存到数据库
|
||||
for (const friend of friendList) {
|
||||
await chatDB.saveFriend(
|
||||
{
|
||||
...friend,
|
||||
lastMessage: "",
|
||||
unreadCount: 0,
|
||||
},
|
||||
currentUserId,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
friend.unreadCount = chatHistory.filter((msg) => msg.senderId === friend.id && !msg.read).length
|
||||
})
|
||||
|
||||
friends.value = friendList
|
||||
friends.value = friendList
|
||||
} catch (error) {
|
||||
console.error("加载好友列表失败:", error)
|
||||
// 降级到预设用户
|
||||
const friendList = presetUsers.filter((user) => user.id !== currentUserId)
|
||||
friendList.forEach((friend) => {
|
||||
friend.lastMessage = ""
|
||||
friend.unreadCount = 0
|
||||
})
|
||||
friends.value = friendList
|
||||
}
|
||||
}
|
||||
|
||||
// 切换当前聊天好友
|
||||
const switchFriend = (friend, currentUserId) => {
|
||||
const switchFriend = async (friend, currentUserId) => {
|
||||
currentFriend.value = friend
|
||||
|
||||
// 清空未读消息计数
|
||||
friend.unreadCount = 0
|
||||
await chatDB.clearUnreadCount(friend.id)
|
||||
|
||||
// 加载聊天记录
|
||||
const chatHistory = getChatHistory(friend.id, currentUserId)
|
||||
chatHistory.forEach((msg) => {
|
||||
if (msg.senderId !== currentUserId) {
|
||||
msg.read = true
|
||||
}
|
||||
})
|
||||
try {
|
||||
const chatHistory = await chatDB.getChatHistory(currentUserId, friend.id)
|
||||
messages.value = chatHistory.sort((a, b) => a.timestamp - b.timestamp)
|
||||
} catch (error) {
|
||||
console.error("加载聊天记录失败:", error)
|
||||
messages.value = []
|
||||
}
|
||||
}
|
||||
// 切换当前聊天好友
|
||||
const setCurrentFriend = async (friend, currentUserId) => {
|
||||
currentFriend.value = friend
|
||||
|
||||
messages.value = chatHistory
|
||||
saveChatHistory(friend.id, currentUserId, chatHistory)
|
||||
// 清空未读消息计数
|
||||
friend.unreadCount = 0
|
||||
await chatDB.clearUnreadCount(friend.id)
|
||||
|
||||
// 加载聊天记录
|
||||
try {
|
||||
const chatHistory = await chatDB.getChatHistory(currentUserId, friend.id)
|
||||
console.log("chatHistory:", chatHistory)
|
||||
console.log("currentUserId:", currentUserId)
|
||||
console.log("friend.id:", friend.id)
|
||||
messages.value = chatHistory.sort((a, b) => a.timestamp - b.timestamp)
|
||||
console.log("messages.value:", messages.value)
|
||||
} catch (error) {
|
||||
console.error("加载聊天记录失败:", error)
|
||||
messages.value = []
|
||||
}
|
||||
}
|
||||
|
||||
// 添加消息
|
||||
const addMessage = (message, currentUserId) => {
|
||||
const addMessage = async (message, currentUserId) => {
|
||||
messages.value.push(message)
|
||||
|
||||
if (currentFriend.value) {
|
||||
saveChatHistory(currentFriend.value.id, currentUserId, messages.value)
|
||||
try {
|
||||
// 保存到数据库
|
||||
await chatDB.saveMessage(message, currentUserId, currentFriend.value.id)
|
||||
|
||||
// 更新好友列表中的最后消息
|
||||
const friend = friends.value.find((f) => f.id === currentFriend.value.id)
|
||||
if (friend) {
|
||||
let lastMessage = message.content
|
||||
if (message.type === "image") lastMessage = "[图片]"
|
||||
if (message.type === "video") lastMessage = "[视频]"
|
||||
if (message.type === "audio") lastMessage = "[语音]"
|
||||
friend.lastMessage = lastMessage.length > 20 ? lastMessage.substring(0, 20) + "..." : lastMessage
|
||||
// 更新好友列表中的最后消息
|
||||
const friend = friends.value.find((f) => f.id === currentFriend.value.id)
|
||||
if (friend) {
|
||||
let lastMessage = message.content
|
||||
if (message.type === "image") lastMessage = "[图片]"
|
||||
if (message.type === "video") lastMessage = "[视频]"
|
||||
if (message.type === "audio") lastMessage = "[语音]"
|
||||
|
||||
friend.lastMessage = lastMessage.length > 20 ? lastMessage.substring(0, 20) + "..." : lastMessage
|
||||
|
||||
// 更新数据库中的好友信息
|
||||
await chatDB.updateFriendLastMessage(friend.id, currentUserId, friend.lastMessage, 0)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("保存消息失败:", error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理接收到的消息
|
||||
const handleIncomingMessage = (messageData, currentUserId) => {
|
||||
const handleIncomingMessage = async (messageData, currentUserId) => {
|
||||
const senderId = messageData.sender_user_id
|
||||
const receiverId = messageData.receiver_user_id
|
||||
|
||||
@@ -123,31 +165,36 @@ export const useChatStore = defineStore("chat", () => {
|
||||
senderId: senderId,
|
||||
read: false,
|
||||
duration: messageData.duration || 0,
|
||||
timestamp: Date.now(),
|
||||
}
|
||||
|
||||
// 保存消息到本地存储
|
||||
const chatHistory = getChatHistory(senderId, currentUserId)
|
||||
chatHistory.push(newMessage)
|
||||
saveChatHistory(senderId, currentUserId, chatHistory)
|
||||
try {
|
||||
// 保存消息到数据库
|
||||
await chatDB.saveMessage(newMessage, currentUserId, senderId)
|
||||
|
||||
// 如果消息来自当前聊天用户,直接显示
|
||||
if (currentFriend.value && senderId == currentFriend.value.id) {
|
||||
newMessage.read = true
|
||||
messages.value.push(newMessage)
|
||||
saveChatHistory(senderId, currentUserId, messages.value)
|
||||
} else {
|
||||
// 更新未读消息计数
|
||||
const friend = friends.value.find((f) => f.id == senderId)
|
||||
if (friend) {
|
||||
friend.unreadCount = (friend.unreadCount || 0) + 1
|
||||
// 如果消息来自当前聊天用户,直接显示
|
||||
if (currentFriend.value && senderId == currentFriend.value.id) {
|
||||
newMessage.read = true
|
||||
messages.value.push(newMessage)
|
||||
} else {
|
||||
// 更新未读消息计数
|
||||
const friend = friends.value.find((f) => f.id == senderId)
|
||||
if (friend) {
|
||||
friend.unreadCount = (friend.unreadCount || 0) + 1
|
||||
|
||||
// 更新最后消息
|
||||
let lastMessage = newMessage.content
|
||||
if (newMessage.type === "image") lastMessage = "[图片]"
|
||||
if (newMessage.type === "video") lastMessage = "[视频]"
|
||||
if (newMessage.type === "audio") lastMessage = "[语音]"
|
||||
friend.lastMessage = lastMessage
|
||||
// 更新最后消息
|
||||
let lastMessage = newMessage.content
|
||||
if (newMessage.type === "image") lastMessage = "[图片]"
|
||||
if (newMessage.type === "video") lastMessage = "[视频]"
|
||||
if (newMessage.type === "audio") lastMessage = "[语音]"
|
||||
friend.lastMessage = lastMessage
|
||||
|
||||
// 更新数据库
|
||||
await chatDB.updateFriendLastMessage(friend.id, currentUserId, lastMessage, friend.unreadCount)
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("处理接收消息失败:", error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,10 +213,10 @@ export const useChatStore = defineStore("chat", () => {
|
||||
isConnected,
|
||||
connectionStatusText,
|
||||
connectionStatusIcon,
|
||||
getChatHistory,
|
||||
saveChatHistory,
|
||||
initDB,
|
||||
loadFriends,
|
||||
switchFriend,
|
||||
setCurrentFriend,
|
||||
addMessage,
|
||||
handleIncomingMessage,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user