|
|
@ -1,26 +1,42 @@ |
|
|
|
|
|
import { defineStore } from 'pinia'; |
|
|
import { MESSAGE_TYPE, MESSAGE_STATUS } from "../api/enums.js" |
|
|
import { MESSAGE_TYPE, MESSAGE_STATUS } from "../api/enums.js" |
|
|
import userStore from './userStore'; |
|
|
import useUserStore from './userStore.js'; |
|
|
import localForage from 'localforage'; |
|
|
import localForage from 'localforage'; |
|
|
|
|
|
|
|
|
/* 为了加速拉取离线消息效率,拉取时消息暂时存储到cacheChats,等 |
|
|
/** |
|
|
待所有离线消息拉取完成后,再统一放至vuex中进行渲染*/ |
|
|
* 优化1(冷热消息分区): |
|
|
|
|
|
* 热消息:登录后的消息 |
|
|
|
|
|
* 冷消息: 登录前的消息 |
|
|
|
|
|
* 每个会话的冷热消息分别用一个key进行存储,当有新的消息时,只更新热消息key,冷消息key保持不变 |
|
|
|
|
|
* 由于热消息数量不会很大,所以localForage.setItem耗时很低,可以防止消息过多时出现卡顿的情况 |
|
|
|
|
|
* |
|
|
|
|
|
* 优化2(延迟渲染): |
|
|
|
|
|
* 拉取消息时,如果直接用state.chats接收,页面就开始渲染,一边渲染页面一边大量接消息会导致很严重的卡顿 |
|
|
|
|
|
* 为了加速拉取离线消息效率,拉取时消息暂时存储到cacheChats,等待所有离线消息拉取完成后,再统一放至state中进行渲染 |
|
|
|
|
|
* |
|
|
|
|
|
* 优化3(pinia代替vuex) |
|
|
|
|
|
* 实测pinia的远超vuex,且语法更简洁清晰 |
|
|
|
|
|
* |
|
|
|
|
|
* */ |
|
|
|
|
|
|
|
|
let cacheChats = []; |
|
|
let cacheChats = []; |
|
|
|
|
|
|
|
|
export default { |
|
|
export default defineStore('chatStore', { |
|
|
state: { |
|
|
state: () => { |
|
|
activeChat: null, |
|
|
return { |
|
|
privateMsgMaxId: 0, |
|
|
activeChat: null, |
|
|
groupMsgMaxId: 0, |
|
|
privateMsgMaxId: 0, |
|
|
loadingPrivateMsg: false, |
|
|
groupMsgMaxId: 0, |
|
|
loadingGroupMsg: false, |
|
|
loadingPrivateMsg: false, |
|
|
chats: [] |
|
|
loadingGroupMsg: false, |
|
|
|
|
|
chats: [] |
|
|
|
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
actions: { |
|
|
mutations: { |
|
|
initChats(chatsData) { |
|
|
initChats(state, chatsData) { |
|
|
this.chats = []; |
|
|
state.chats = []; |
|
|
this.privateMsgMaxId = chatsData.privateMsgMaxId || 0; |
|
|
state.privateMsgMaxId = chatsData.privateMsgMaxId || 0; |
|
|
this.groupMsgMaxId = chatsData.groupMsgMaxId || 0; |
|
|
state.groupMsgMaxId = chatsData.groupMsgMaxId || 0; |
|
|
|
|
|
cacheChats = chatsData.chats || []; |
|
|
cacheChats = chatsData.chats || []; |
|
|
// 防止图片一直处在加载中状态
|
|
|
// 防止图片一直处在加载中状态
|
|
|
cacheChats.forEach((chat) => { |
|
|
cacheChats.forEach((chat) => { |
|
|
@ -31,15 +47,15 @@ export default { |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}, |
|
|
}, |
|
|
openChat(state, chatInfo) { |
|
|
openChat(chatInfo) { |
|
|
let chats = this.getters.findChats() |
|
|
let chats = this.findChats() |
|
|
let chat = null; |
|
|
let chat = null; |
|
|
for (let idx in chats) { |
|
|
for (let idx in chats) { |
|
|
if (chats[idx].type == chatInfo.type && |
|
|
if (chats[idx].type == chatInfo.type && |
|
|
chats[idx].targetId === chatInfo.targetId) { |
|
|
chats[idx].targetId === chatInfo.targetId) { |
|
|
chat = chats[idx]; |
|
|
chat = chats[idx]; |
|
|
// 放置头部
|
|
|
// 放置头部
|
|
|
this.commit("moveTop", idx) |
|
|
this.moveTop(idx) |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -53,6 +69,7 @@ export default { |
|
|
lastContent: "", |
|
|
lastContent: "", |
|
|
lastSendTime: new Date().getTime(), |
|
|
lastSendTime: new Date().getTime(), |
|
|
unreadCount: 0, |
|
|
unreadCount: 0, |
|
|
|
|
|
hotMinIdx: 0, |
|
|
messages: [], |
|
|
messages: [], |
|
|
atMe: false, |
|
|
atMe: false, |
|
|
atAll: false, |
|
|
atAll: false, |
|
|
@ -62,12 +79,12 @@ export default { |
|
|
chats.unshift(chat); |
|
|
chats.unshift(chat); |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
activeChat(state, idx) { |
|
|
setActiveChat(idx) { |
|
|
let chats = this.getters.findChats(); |
|
|
let chats = this.findChats(); |
|
|
state.activeChat = chats[idx]; |
|
|
this.activeChat = chats[idx]; |
|
|
}, |
|
|
}, |
|
|
resetUnreadCount(state, chatInfo) { |
|
|
resetUnreadCount(chatInfo) { |
|
|
let chats = this.getters.findChats(); |
|
|
let chats = this.findChats(); |
|
|
for (let idx in chats) { |
|
|
for (let idx in chats) { |
|
|
if (chats[idx].type == chatInfo.type && |
|
|
if (chats[idx].type == chatInfo.type && |
|
|
chats[idx].targetId == chatInfo.targetId) { |
|
|
chats[idx].targetId == chatInfo.targetId) { |
|
|
@ -75,13 +92,13 @@ export default { |
|
|
chats[idx].atMe = false; |
|
|
chats[idx].atMe = false; |
|
|
chats[idx].atAll = false; |
|
|
chats[idx].atAll = false; |
|
|
chats[idx].stored = false; |
|
|
chats[idx].stored = false; |
|
|
this.commit("saveToStorage"); |
|
|
this.saveToStorage(); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
readedMessage(state, pos) { |
|
|
readedMessage(pos) { |
|
|
let chat = this.getters.findChatByFriend(pos.friendId); |
|
|
let chat = this.findChatByFriend(pos.friendId); |
|
|
if (!chat) return; |
|
|
if (!chat) return; |
|
|
chat.messages.forEach((m) => { |
|
|
chat.messages.forEach((m) => { |
|
|
if (m.id && m.selfSend && m.status < MESSAGE_STATUS.RECALL) { |
|
|
if (m.id && m.selfSend && m.status < MESSAGE_STATUS.RECALL) { |
|
|
@ -92,68 +109,69 @@ export default { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
this.commit("saveToStorage"); |
|
|
this.saveToStorage(); |
|
|
}, |
|
|
}, |
|
|
removeChat(state, idx) { |
|
|
removeChat(idx) { |
|
|
let chats = this.getters.findChats(); |
|
|
let chats = this.findChats(); |
|
|
if (chats[idx] == state.activeChat) { |
|
|
if (chats[idx] == this.activeChat) { |
|
|
state.activeChat = null; |
|
|
this.activeChat = null; |
|
|
} |
|
|
} |
|
|
chats[idx].delete = true; |
|
|
chats[idx].delete = true; |
|
|
chats[idx].stored = false; |
|
|
chats[idx].stored = false; |
|
|
this.commit("saveToStorage"); |
|
|
this.saveToStorage(); |
|
|
}, |
|
|
}, |
|
|
removePrivateChat(state, friendId) { |
|
|
removePrivateChat(friendId) { |
|
|
let chats = this.getters.findChats(); |
|
|
let chats = this.findChats(); |
|
|
for (let idx in chats) { |
|
|
for (let idx in chats) { |
|
|
if (chats[idx].type == 'PRIVATE' && |
|
|
if (chats[idx].type == 'PRIVATE' && |
|
|
chats[idx].targetId === friendId) { |
|
|
chats[idx].targetId === friendId) { |
|
|
this.commit("removeChat", idx) |
|
|
this.removeChat(idx); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
removeGroupChat(state, groupId) { |
|
|
removeGroupChat(groupId) { |
|
|
let chats = this.getters.findChats(); |
|
|
let chats = this.findChats(); |
|
|
for (let idx in chats) { |
|
|
for (let idx in chats) { |
|
|
if (chats[idx].type == 'GROUP' && |
|
|
if (chats[idx].type == 'GROUP' && |
|
|
chats[idx].targetId === groupId) { |
|
|
chats[idx].targetId === groupId) { |
|
|
this.commit("removeChat", idx) |
|
|
this.removeChat(idx); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
moveTop(state, idx) { |
|
|
moveTop(idx) { |
|
|
// 加载中不移动,很耗性能
|
|
|
// 加载中不移动,很耗性能
|
|
|
if (this.getters.isLoading()) { |
|
|
if (this.isLoading()) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
if (idx > 0) { |
|
|
if (idx > 0) { |
|
|
let chats = this.getters.findChats(); |
|
|
let chats = this.findChats(); |
|
|
let chat = chats[idx]; |
|
|
let chat = chats[idx]; |
|
|
chats.splice(idx, 1); |
|
|
chats.splice(idx, 1); |
|
|
chats.unshift(chat); |
|
|
chats.unshift(chat); |
|
|
chat.lastSendTime = new Date().getTime(); |
|
|
chat.lastSendTime = new Date().getTime(); |
|
|
chat.stored = false; |
|
|
chat.stored = false; |
|
|
this.commit("saveToStorage"); |
|
|
this.saveToStorage(); |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
insertMessage(state, [msgInfo, chatInfo]) { |
|
|
insertMessage(msgInfo, chatInfo) { |
|
|
|
|
|
let time = new Date().getTime() |
|
|
let type = chatInfo.type; |
|
|
let type = chatInfo.type; |
|
|
// 记录消息的最大id
|
|
|
// 记录消息的最大id
|
|
|
if (msgInfo.id && type == "PRIVATE" && msgInfo.id > state.privateMsgMaxId) { |
|
|
if (msgInfo.id && type == "PRIVATE" && msgInfo.id > this.privateMsgMaxId) { |
|
|
state.privateMsgMaxId = msgInfo.id; |
|
|
this.privateMsgMaxId = msgInfo.id; |
|
|
} |
|
|
} |
|
|
if (msgInfo.id && type == "GROUP" && msgInfo.id > state.groupMsgMaxId) { |
|
|
if (msgInfo.id && type == "GROUP" && msgInfo.id > this.groupMsgMaxId) { |
|
|
state.groupMsgMaxId = msgInfo.id; |
|
|
this.groupMsgMaxId = msgInfo.id; |
|
|
} |
|
|
} |
|
|
// 如果是已存在消息,则覆盖旧的消息数据
|
|
|
// 如果是已存在消息,则覆盖旧的消息数据
|
|
|
let chat = this.getters.findChat(chatInfo); |
|
|
let chat = this.findChat(chatInfo); |
|
|
let message = this.getters.findMessage(chat, msgInfo); |
|
|
let message = this.findMessage(chat, msgInfo); |
|
|
if (message) { |
|
|
if (message) { |
|
|
Object.assign(message, msgInfo); |
|
|
Object.assign(message, msgInfo); |
|
|
chat.stored = false; |
|
|
chat.stored = false; |
|
|
this.commit("saveToStorage"); |
|
|
this.saveToStorage(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
// 插入新的数据
|
|
|
// 插入新的数据
|
|
|
@ -182,7 +200,8 @@ export default { |
|
|
// 是否有人@我
|
|
|
// 是否有人@我
|
|
|
if (!msgInfo.selfSend && chat.type == "GROUP" && msgInfo.atUserIds && |
|
|
if (!msgInfo.selfSend && chat.type == "GROUP" && msgInfo.atUserIds && |
|
|
msgInfo.status != MESSAGE_STATUS.READED) { |
|
|
msgInfo.status != MESSAGE_STATUS.READED) { |
|
|
let userId = userStore.state.userInfo.id; |
|
|
let userStore = useUserStore(); |
|
|
|
|
|
let userId = userStore.userInfo.id; |
|
|
if (msgInfo.atUserIds.indexOf(userId) >= 0) { |
|
|
if (msgInfo.atUserIds.indexOf(userId) >= 0) { |
|
|
chat.atMe = true; |
|
|
chat.atMe = true; |
|
|
} |
|
|
} |
|
|
@ -212,39 +231,44 @@ export default { |
|
|
} |
|
|
} |
|
|
chat.messages.splice(insertPos, 0, msgInfo); |
|
|
chat.messages.splice(insertPos, 0, msgInfo); |
|
|
chat.stored = false; |
|
|
chat.stored = false; |
|
|
this.commit("saveToStorage"); |
|
|
this.saveToStorage(); |
|
|
|
|
|
console.log("耗时:", new Date().getTime() - time) |
|
|
}, |
|
|
}, |
|
|
updateMessage(state, [msgInfo, chatInfo]) { |
|
|
updateMessage(msgInfo, chatInfo) { |
|
|
// 获取对方id或群id
|
|
|
// 获取对方id或群id
|
|
|
let chat = this.getters.findChat(chatInfo); |
|
|
let chat = this.findChat(chatInfo); |
|
|
let message = this.getters.findMessage(chat, msgInfo); |
|
|
let message = this.findMessage(chat, msgInfo); |
|
|
if (message) { |
|
|
if (message) { |
|
|
// 属性拷贝
|
|
|
// 属性拷贝
|
|
|
Object.assign(message, msgInfo); |
|
|
Object.assign(message, msgInfo); |
|
|
chat.stored = false; |
|
|
chat.stored = false; |
|
|
this.commit("saveToStorage"); |
|
|
this.saveToStorage(); |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
deleteMessage(state, [msgInfo, chatInfo]) { |
|
|
deleteMessage(msgInfo, chatInfo) { |
|
|
let chat = this.getters.findChat(chatInfo); |
|
|
let chat = this.findChat(chatInfo); |
|
|
|
|
|
let isColdMessage = false; |
|
|
for (let idx in chat.messages) { |
|
|
for (let idx in chat.messages) { |
|
|
// 已经发送成功的,根据id删除
|
|
|
// 已经发送成功的,根据id删除
|
|
|
if (chat.messages[idx].id && chat.messages[idx].id == msgInfo.id) { |
|
|
if (chat.messages[idx].id && chat.messages[idx].id == msgInfo.id) { |
|
|
chat.messages.splice(idx, 1); |
|
|
chat.messages.splice(idx, 1); |
|
|
|
|
|
isColdMessage = idx < chat.hotMinIdx; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
// 正在发送中的消息可能没有id,只有临时id
|
|
|
// 正在发送中的消息可能没有id,只有临时id
|
|
|
if (chat.messages[idx].tmpId && chat.messages[idx].tmpId == msgInfo.tmpId) { |
|
|
if (chat.messages[idx].tmpId && chat.messages[idx].tmpId == msgInfo.tmpId) { |
|
|
chat.messages.splice(idx, 1); |
|
|
chat.messages.splice(idx, 1); |
|
|
|
|
|
isColdMessage = idx < chat.hotMinIdx; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
chat.stored = false; |
|
|
chat.stored = false; |
|
|
this.commit("saveToStorage"); |
|
|
this.saveToStorage(isColdMessage); |
|
|
}, |
|
|
}, |
|
|
recallMessage(state, [msgInfo, chatInfo]) { |
|
|
recallMessage(msgInfo, chatInfo) { |
|
|
let chat = this.getters.findChat(chatInfo); |
|
|
let chat = this.findChat(chatInfo); |
|
|
if (!chat) return; |
|
|
if (!chat) return; |
|
|
|
|
|
let isColdMessage = false; |
|
|
// 要撤回的消息id
|
|
|
// 要撤回的消息id
|
|
|
let id = msgInfo.content; |
|
|
let id = msgInfo.content; |
|
|
let name = msgInfo.selfSend ? '你' : chat.type == 'PRIVATE' ? '对方' : msgInfo.sendNickName; |
|
|
let name = msgInfo.selfSend ? '你' : chat.type == 'PRIVATE' ? '对方' : msgInfo.sendNickName; |
|
|
@ -262,6 +286,7 @@ export default { |
|
|
if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED) { |
|
|
if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED) { |
|
|
chat.unreadCount++; |
|
|
chat.unreadCount++; |
|
|
} |
|
|
} |
|
|
|
|
|
isColdMessage = idx < chat.hotMinIdx; |
|
|
} |
|
|
} |
|
|
// 被引用的消息也要撤回
|
|
|
// 被引用的消息也要撤回
|
|
|
if (m.quoteMessage && m.quoteMessage.id == msgInfo.id) { |
|
|
if (m.quoteMessage && m.quoteMessage.id == msgInfo.id) { |
|
|
@ -271,86 +296,100 @@ export default { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
chat.stored = false; |
|
|
chat.stored = false; |
|
|
this.commit("saveToStorage"); |
|
|
this.saveToStorage(isColdMessage); |
|
|
}, |
|
|
}, |
|
|
updateChatFromFriend(state, friend) { |
|
|
updateChatFromFriend(friend) { |
|
|
let chat = this.getters.findChatByFriend(friend.id); |
|
|
let chat = this.findChatByFriend(friend.id); |
|
|
// 更新会话中的群名和头像
|
|
|
// 更新会话中的群名和头像
|
|
|
if (chat && (chat.headImage != friend.headImage || |
|
|
if (chat && (chat.headImage != friend.headImage || |
|
|
chat.showName != friend.nickName)) { |
|
|
chat.showName != friend.nickName)) { |
|
|
chat.headImage = friend.headImage; |
|
|
chat.headImage = friend.headImage; |
|
|
chat.showName = friend.nickName; |
|
|
chat.showName = friend.nickName; |
|
|
chat.stored = false; |
|
|
chat.stored = false; |
|
|
this.commit("saveToStorage") |
|
|
this.saveToStorage() |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
updateChatFromUser(user) { |
|
|
updateChatFromUser(user) { |
|
|
let chat = this.getters.findChatByFriend(user.id); |
|
|
let chat = this.findChatByFriend(user.id); |
|
|
// 更新会话中的昵称和头像
|
|
|
// 更新会话中的昵称和头像
|
|
|
if (chat && (chat.headImage != user.headImageThumb || |
|
|
if (chat && (chat.headImage != user.headImageThumb || |
|
|
chat.showName != user.nickName)) { |
|
|
chat.showName != user.nickName)) { |
|
|
chat.headImage = user.headImageThumb; |
|
|
chat.headImage = user.headImageThumb; |
|
|
chat.showName = user.nickName; |
|
|
chat.showName = user.nickName; |
|
|
chat.stored = false; |
|
|
chat.stored = false; |
|
|
this.saveToStorage(); |
|
|
this.saveToStorage(); |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
updateChatFromGroup(state, group) { |
|
|
updateChatFromGroup(group) { |
|
|
let chat = this.getters.findChatByGroup(group.id); |
|
|
let chat = this.findChatByGroup(group.id); |
|
|
if (chat && (chat.headImage != group.headImageThumb || |
|
|
if (chat && (chat.headImage != group.headImageThumb || |
|
|
chat.showName != group.showGroupName)) { |
|
|
chat.showName != group.showGroupName)) { |
|
|
// 更新会话中的群名称和头像
|
|
|
// 更新会话中的群名称和头像
|
|
|
chat.headImage = group.headImageThumb; |
|
|
chat.headImage = group.headImageThumb; |
|
|
chat.showName = group.showGroupName; |
|
|
chat.showName = group.showGroupName; |
|
|
chat.stored = false; |
|
|
chat.stored = false; |
|
|
this.commit("saveToStorage") |
|
|
this.saveToStorage() |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
loadingPrivateMsg(state, loading) { |
|
|
setLoadingPrivateMsg(loading) { |
|
|
state.loadingPrivateMsg = loading; |
|
|
this.loadingPrivateMsg = loading; |
|
|
if (!this.getters.isLoading()) { |
|
|
if (!this.isLoading()) { |
|
|
this.commit("refreshChats") |
|
|
this.refreshChats(); |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
loadingGroupMsg(state, loading) { |
|
|
setLoadingGroupMsg(loading) { |
|
|
state.loadingGroupMsg = loading; |
|
|
this.loadingGroupMsg = loading; |
|
|
if (!this.getters.isLoading()) { |
|
|
if (!this.isLoading()) { |
|
|
this.commit("refreshChats") |
|
|
this.refreshChats(); |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
refreshChats(state) { |
|
|
refreshChats() { |
|
|
if (!cacheChats) { |
|
|
if (!cacheChats) return; |
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
// 排序
|
|
|
// 排序
|
|
|
cacheChats.sort((chat1, chat2) => { |
|
|
cacheChats.sort((chat1, chat2) => chat2.lastSendTime - chat1.lastSendTime); |
|
|
return chat2.lastSendTime - chat1.lastSendTime; |
|
|
// 记录热数据索引位置
|
|
|
}); |
|
|
cacheChats.forEach(chat => chat.hotMinIdx = chat.messages.length); |
|
|
// 将消息一次性装载回来
|
|
|
// 将消息一次性装载回来
|
|
|
state.chats = cacheChats; |
|
|
this.chats = cacheChats; |
|
|
// 清空缓存
|
|
|
// 清空缓存
|
|
|
cacheChats = null; |
|
|
cacheChats = null; |
|
|
this.commit("saveToStorage"); |
|
|
// 持久化消息
|
|
|
|
|
|
this.saveToStorage(true); |
|
|
}, |
|
|
}, |
|
|
saveToStorage(state) { |
|
|
saveToStorage(withColdMessage) { |
|
|
// 加载中不保存,防止卡顿
|
|
|
// 加载中不保存,防止卡顿
|
|
|
if (this.getters.isLoading()) { |
|
|
if (this.isLoading()) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
let userId = userStore.state.userInfo.id; |
|
|
let userStore = useUserStore(); |
|
|
|
|
|
let userId = userStore.userInfo.id; |
|
|
let key = "chats-" + userId; |
|
|
let key = "chats-" + userId; |
|
|
let chatKeys = []; |
|
|
let chatKeys = []; |
|
|
// 按会话为单位存储,
|
|
|
// 按会话为单位存储,
|
|
|
state.chats.forEach((chat) => { |
|
|
this.chats.forEach((chat) => { |
|
|
// 只存储有改动的会话
|
|
|
// 只存储有改动的会话
|
|
|
let chatKey = `${key}-${chat.type}-${chat.targetId}` |
|
|
let chatKey = `${key}-${chat.type}-${chat.targetId}` |
|
|
if (!chat.stored) { |
|
|
if (!chat.stored) { |
|
|
|
|
|
chat.stored = true; |
|
|
if (chat.delete) { |
|
|
if (chat.delete) { |
|
|
localForage.removeItem(chatKey); |
|
|
localForage.removeItem(chatKey); |
|
|
} else { |
|
|
} else { |
|
|
localForage.setItem(chatKey, chat); |
|
|
// 存储冷数据
|
|
|
|
|
|
if (withColdMessage) { |
|
|
|
|
|
let coldChat = Object.assign({}, chat); |
|
|
|
|
|
coldChat.messages = chat.messages.slice(0, chat.hotMinIdx); |
|
|
|
|
|
localForage.setItem(chatKey, coldChat) |
|
|
|
|
|
} |
|
|
|
|
|
// 存储热消息
|
|
|
|
|
|
let hotKey = chatKey + '-hot'; |
|
|
|
|
|
if (chat.messages.length > chat.hotMinIdx) { |
|
|
|
|
|
let hotChat = Object.assign({}, chat); |
|
|
|
|
|
hotChat.messages = chat.messages.slice(chat.hotMinIdx) |
|
|
|
|
|
localForage.setItem(hotKey, hotChat) |
|
|
|
|
|
} else { |
|
|
|
|
|
localForage.removeItem(hotKey); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
chat.stored = true; |
|
|
|
|
|
} |
|
|
} |
|
|
if (!chat.delete) { |
|
|
if (!chat.delete) { |
|
|
chatKeys.push(chatKey); |
|
|
chatKeys.push(chatKey); |
|
|
@ -358,46 +397,56 @@ export default { |
|
|
}) |
|
|
}) |
|
|
// 会话核心信息
|
|
|
// 会话核心信息
|
|
|
let chatsData = { |
|
|
let chatsData = { |
|
|
privateMsgMaxId: state.privateMsgMaxId, |
|
|
privateMsgMaxId: this.privateMsgMaxId, |
|
|
groupMsgMaxId: state.groupMsgMaxId, |
|
|
groupMsgMaxId: this.groupMsgMaxId, |
|
|
chatKeys: chatKeys |
|
|
chatKeys: chatKeys |
|
|
} |
|
|
} |
|
|
localForage.setItem(key, chatsData) |
|
|
localForage.setItem(key, chatsData) |
|
|
// 清理已删除的会话
|
|
|
// 清理已删除的会话
|
|
|
state.chats = state.chats.filter(chat => !chat.delete) |
|
|
this.chats = this.chats.filter(chat => !chat.delete) |
|
|
}, |
|
|
}, |
|
|
clear(state) { |
|
|
clear() { |
|
|
cacheChats = [] |
|
|
cacheChats = [] |
|
|
state.chats = []; |
|
|
this.chats = []; |
|
|
state.activeChat = null; |
|
|
this.activeChat = null; |
|
|
} |
|
|
}, |
|
|
}, |
|
|
loadChat() { |
|
|
actions: { |
|
|
|
|
|
loadChat(context) { |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
let userId = userStore.state.userInfo.id; |
|
|
let userStore = useUserStore(); |
|
|
|
|
|
let userId = userStore.userInfo.id; |
|
|
let key = "chats-" + userId; |
|
|
let key = "chats-" + userId; |
|
|
localForage.getItem(key).then((chatsData) => { |
|
|
localForage.getItem(key).then((chatsData) => { |
|
|
if (!chatsData) { |
|
|
if (!chatsData) { |
|
|
resolve(); |
|
|
resolve(); |
|
|
} else if (chatsData.chats) { |
|
|
|
|
|
// 兼容旧版本
|
|
|
|
|
|
context.commit("initChats", chatsData); |
|
|
|
|
|
resolve(); |
|
|
|
|
|
} else if (chatsData.chatKeys) { |
|
|
} else if (chatsData.chatKeys) { |
|
|
const promises = []; |
|
|
const promises = []; |
|
|
chatsData.chatKeys.forEach(key => { |
|
|
chatsData.chatKeys.forEach(key => { |
|
|
promises.push(localForage.getItem(key)) |
|
|
promises.push(localForage.getItem(key)) |
|
|
|
|
|
promises.push(localForage.getItem(key + "-hot")) |
|
|
}) |
|
|
}) |
|
|
Promise.all(promises).then(chats => { |
|
|
Promise.all(promises).then(chats => { |
|
|
chatsData.chats = chats.filter(o => o); |
|
|
chatsData.chats = []; |
|
|
context.commit("initChats", chatsData); |
|
|
// 偶数下标为冷消息,奇数下标为热消息
|
|
|
|
|
|
for (let i = 0; i < chats.length; i += 2) { |
|
|
|
|
|
if (!chats[i] && !chats[i + 1]) { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
let coldChat = chats[i]; |
|
|
|
|
|
let hotChat = chats[i + 1]; |
|
|
|
|
|
// 冷热消息合并
|
|
|
|
|
|
let chat = Object.assign({}, coldChat, hotChat); |
|
|
|
|
|
if (hotChat && coldChat) { |
|
|
|
|
|
chat.messages = coldChat.messages.concat(hotChat.messages) |
|
|
|
|
|
} |
|
|
|
|
|
chatsData.chats.push(chat); |
|
|
|
|
|
} |
|
|
|
|
|
this.initChats(chatsData); |
|
|
resolve(); |
|
|
resolve(); |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
}).catch((e) => { |
|
|
}).catch(e => { |
|
|
console.log("加载消息失败") |
|
|
console.log("加载消息失败") |
|
|
reject(); |
|
|
reject(e); |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
@ -406,14 +455,14 @@ export default { |
|
|
isLoading: (state) => () => { |
|
|
isLoading: (state) => () => { |
|
|
return state.loadingPrivateMsg || state.loadingGroupMsg |
|
|
return state.loadingPrivateMsg || state.loadingGroupMsg |
|
|
}, |
|
|
}, |
|
|
findChats: (state, getters) => () => { |
|
|
findChats: (state) => () => { |
|
|
if (cacheChats && getters.isLoading()) { |
|
|
if (cacheChats && state.isLoading()) { |
|
|
return cacheChats; |
|
|
return cacheChats; |
|
|
} |
|
|
} |
|
|
return state.chats; |
|
|
return state.chats; |
|
|
}, |
|
|
}, |
|
|
findChatIdx: (state, getters) => (chat) => { |
|
|
findChatIdx: (state) => (chat) => { |
|
|
let chats = getters.findChats(); |
|
|
let chats = state.findChats(); |
|
|
for (let idx in chats) { |
|
|
for (let idx in chats) { |
|
|
if (chats[idx].type == chat.type && |
|
|
if (chats[idx].type == chat.type && |
|
|
chats[idx].targetId === chat.targetId) { |
|
|
chats[idx].targetId === chat.targetId) { |
|
|
@ -422,18 +471,18 @@ export default { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
findChat: (state, getters) => (chat) => { |
|
|
findChat: (state) => (chat) => { |
|
|
let chats = getters.findChats(); |
|
|
let chats = state.findChats(); |
|
|
let idx = getters.findChatIdx(chat); |
|
|
let idx = state.findChatIdx(chat); |
|
|
return chats[idx]; |
|
|
return chats[idx]; |
|
|
}, |
|
|
}, |
|
|
findChatByFriend: (state, getters) => (fid) => { |
|
|
findChatByFriend: (state) => (fid) => { |
|
|
let chats = getters.findChats(); |
|
|
let chats = state.findChats(); |
|
|
return chats.find(chat => chat.type == 'PRIVATE' && |
|
|
return chats.find(chat => chat.type == 'PRIVATE' && |
|
|
chat.targetId == fid) |
|
|
chat.targetId == fid) |
|
|
}, |
|
|
}, |
|
|
findChatByGroup: (state, getters) => (gid) => { |
|
|
findChatByGroup: (state) => (gid) => { |
|
|
let chats = getters.findChats(); |
|
|
let chats = state.findChats(); |
|
|
return chats.find(chat => chat.type == 'GROUP' && |
|
|
return chats.find(chat => chat.type == 'GROUP' && |
|
|
chat.targetId == gid) |
|
|
chat.targetId == gid) |
|
|
}, |
|
|
}, |
|
|
@ -454,4 +503,4 @@ export default { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}); |