前端:
修复了已知问题 后端: 修复了已知问题
This commit is contained in:
@@ -36,6 +36,7 @@ export const useChatStore = defineStore('chat', {
|
||||
this.messages = [];
|
||||
//先从浏览器缓存加载一部分消息列表
|
||||
const localHistory = await messagesDb.getPageMessages(sessionId, new Date().toISOString(), this.pageSize);
|
||||
console.log(localHistory)
|
||||
if (localHistory.length > 0) {
|
||||
this.messages = localHistory;
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { contactDb } from "@/utils/db/contactDB";
|
||||
import { friendService } from "@/services/friend";
|
||||
import { useMessage } from "@/components/messages/useAlert";
|
||||
|
||||
export const useContactStore = defineStore('contact', {
|
||||
state: () => ({
|
||||
contacts: [],
|
||||
|
||||
}),
|
||||
actions: {
|
||||
async addContact(contact) {
|
||||
this.contacts.push(contact);
|
||||
await contactDb.save(contact);
|
||||
},
|
||||
async loadContactList() {
|
||||
if (this.contacts.length == 0) {
|
||||
this.contacts = await contactDb.getAll();
|
||||
}
|
||||
this.fetchContactFromServer();
|
||||
},
|
||||
async fetchContactFromServer() {
|
||||
const message = useMessage();
|
||||
const res = await friendService.getFriendList();
|
||||
if (res.code == 0) {
|
||||
const localMap = new Map(this.contacts.map(item => [item.id, item]));
|
||||
res.data.forEach(item => {
|
||||
const existingItem = localMap.get(item.id);
|
||||
if (existingItem) {
|
||||
// --- 局部更新 ---
|
||||
// 使用 Object.assign 将新数据合并到旧对象上,保持响应式引用
|
||||
Object.assign(existingItem, item);
|
||||
} else {
|
||||
// --- 插入新会话 ---
|
||||
this.contacts.push(item);
|
||||
}
|
||||
// 同步到本地数据库
|
||||
contactDb.save(item);
|
||||
});
|
||||
} else {
|
||||
message.error(res.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -6,6 +6,8 @@ import { useChatStore } from "./chat";
|
||||
import { authService } from "@/services/auth";
|
||||
import { generateSessionId } from "@/utils/sessionIdTools";
|
||||
import { messageHandler } from "@/handler/messageHandler";
|
||||
import { useBrowserNotification } from "@/services/useBrowserNotification";
|
||||
import { useConversationStore } from "./conversation";
|
||||
|
||||
export const useSignalRStore = defineStore('signalr', {
|
||||
state: () => ({
|
||||
@@ -40,9 +42,17 @@ export const useSignalRStore = defineStore('signalr', {
|
||||
},
|
||||
registerHandlers() {
|
||||
const chatStore = useChatStore()
|
||||
const browserNotification = useBrowserNotification();
|
||||
|
||||
this.connection.on('ReceiveMessage', (msg) => {
|
||||
const sessionId = generateSessionId(msg.senderId, msg.receiverId);
|
||||
messageHandler(msg);
|
||||
chatStore.addMessage(msg);
|
||||
chatStore.addMessage(msg, sessionId);
|
||||
const conversation = useConversationStore().conversations.find(x => x.targetId == msg.senderId);
|
||||
browserNotification.send(`${conversation.targetName}发来一条消息`, {
|
||||
body: msg.content,
|
||||
icon: conversation.targetAvatar
|
||||
});
|
||||
});
|
||||
|
||||
this.connection.onclose(() => { this.isConnected = false });
|
||||
|
||||
Reference in New Issue
Block a user