|
|
@ -1,9 +1,22 @@ |
|
|
import { MESSAGE_TYPE, MESSAGE_STATUS } from '@/common/enums.js'; |
|
|
import { |
|
|
|
|
|
MESSAGE_TYPE, |
|
|
|
|
|
MESSAGE_STATUS |
|
|
|
|
|
} from '@/common/enums.js'; |
|
|
import userStore from './userStore'; |
|
|
import userStore from './userStore'; |
|
|
|
|
|
/* |
|
|
|
|
|
uniapp性能优化: |
|
|
|
|
|
1.由于uniapp渲染消息性能非常拉胯,所以先把离线消息存储到cacheChats,等 |
|
|
|
|
|
待所有离线消息拉取完成后,再统一进行渲染 |
|
|
|
|
|
2.在vuex中对数组进行unshift,splice特别卡,所以删除会话、会话置顶、删 |
|
|
|
|
|
除消息等操作进行优化,不通过unshift,splice实现,改造方案如下: |
|
|
|
|
|
删除会话: 通过delete标志判断是否删除 |
|
|
|
|
|
删除消息:通过delete标志判断是否删除 |
|
|
|
|
|
会话置顶:通过lastSendTime排序确定会话顺序 |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
let cacheChats = []; |
|
|
export default { |
|
|
export default { |
|
|
state: { |
|
|
state: { |
|
|
activeIndex: -1, |
|
|
|
|
|
chats: [], |
|
|
chats: [], |
|
|
privateMsgMaxId: 0, |
|
|
privateMsgMaxId: 0, |
|
|
groupMsgMaxId: 0, |
|
|
groupMsgMaxId: 0, |
|
|
@ -13,26 +26,42 @@ export default { |
|
|
|
|
|
|
|
|
mutations: { |
|
|
mutations: { |
|
|
initChats(state, chatsData) { |
|
|
initChats(state, chatsData) { |
|
|
state.chats = chatsData.chats ||[]; |
|
|
cacheChats = []; |
|
|
state.privateMsgMaxId = chatsData.privateMsgMaxId||0; |
|
|
for (let chat of chatsData.chats) { |
|
|
state.groupMsgMaxId = chatsData.groupMsgMaxId||0; |
|
|
// 已删除的会话直接丢弃
|
|
|
|
|
|
if (chat.delete) { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
// 暂存至缓冲区
|
|
|
|
|
|
cacheChats.push(JSON.parse(JSON.stringify(chat))); |
|
|
|
|
|
// 加载期间显示只前15个会话做做样子,一切都为了加快初始化时间
|
|
|
|
|
|
if (state.chats.length < 15) { |
|
|
|
|
|
chat.messages = []; |
|
|
|
|
|
state.chats.push(chat); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
state.privateMsgMaxId = chatsData.privateMsgMaxId || 0; |
|
|
|
|
|
state.groupMsgMaxId = chatsData.groupMsgMaxId || 0; |
|
|
// 防止图片一直处在加载中状态
|
|
|
// 防止图片一直处在加载中状态
|
|
|
state.chats.forEach((chat) => { |
|
|
cacheChats.forEach((chat) => { |
|
|
chat.messages.forEach((msg) => { |
|
|
chat.messages.forEach((msg) => { |
|
|
if (msg.loadStatus == "loading") { |
|
|
if (msg.loadStatus == "loading") { |
|
|
msg.loadStatus = "fail" |
|
|
msg.loadStatus = "fail" |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
}, |
|
|
}, |
|
|
openChat(state, chatInfo) { |
|
|
openChat(state, chatInfo) { |
|
|
|
|
|
let chats = this.getters.findChats(); |
|
|
let chat = null; |
|
|
let chat = null; |
|
|
for (let idx in state.chats) { |
|
|
for (let idx in chats) { |
|
|
if (state.chats[idx].type == chatInfo.type && |
|
|
if (chats[idx].type == chatInfo.type && |
|
|
state.chats[idx].targetId === chatInfo.targetId) { |
|
|
chats[idx].targetId === chatInfo.targetId) { |
|
|
chat = state.chats[idx]; |
|
|
chat = chats[idx]; |
|
|
|
|
|
chat.delete = false; |
|
|
// 放置头部
|
|
|
// 放置头部
|
|
|
this.commit("moveTop",idx) |
|
|
this.commit("moveTop", idx) |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -48,40 +77,42 @@ export default { |
|
|
unreadCount: 0, |
|
|
unreadCount: 0, |
|
|
messages: [], |
|
|
messages: [], |
|
|
atMe: false, |
|
|
atMe: false, |
|
|
atAll: false |
|
|
atAll: false, |
|
|
|
|
|
delete: false |
|
|
}; |
|
|
}; |
|
|
state.chats.unshift(chat); |
|
|
chats.push(chat); |
|
|
|
|
|
this.commit("moveTop", chats.length - 1) |
|
|
} |
|
|
} |
|
|
this.commit("saveToStorage"); |
|
|
|
|
|
}, |
|
|
}, |
|
|
activeChat(state, idx) { |
|
|
activeChat(state, idx) { |
|
|
state.activeIndex = idx; |
|
|
let chats = this.getters.findChats(); |
|
|
if (idx >= 0) { |
|
|
if (idx >= 0) { |
|
|
state.chats[idx].unreadCount = 0; |
|
|
chats[idx].unreadCount = 0; |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
resetUnreadCount(state, chatInfo) { |
|
|
resetUnreadCount(state, chatInfo) { |
|
|
for (let idx in state.chats) { |
|
|
let chats = this.getters.findChats(); |
|
|
if (state.chats[idx].type == chatInfo.type && |
|
|
for (let idx in chats) { |
|
|
state.chats[idx].targetId == chatInfo.targetId) { |
|
|
if (chats[idx].type == chatInfo.type && |
|
|
state.chats[idx].unreadCount = 0; |
|
|
chats[idx].targetId == chatInfo.targetId) { |
|
|
state.chats[idx].atMe = false; |
|
|
chats[idx].unreadCount = 0; |
|
|
state.chats[idx].atAll = false; |
|
|
chats[idx].atMe = false; |
|
|
|
|
|
chats[idx].atAll = false; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
this.commit("saveToStorage"); |
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
readedMessage(state, pos) { |
|
|
readedMessage(state, pos) { |
|
|
for (let idx in state.chats) { |
|
|
let chats = this.getters.findChats(); |
|
|
if (state.chats[idx].type == 'PRIVATE' && |
|
|
for (let idx in chats) { |
|
|
state.chats[idx].targetId == pos.friendId) { |
|
|
if (chats[idx].type == 'PRIVATE' && |
|
|
state.chats[idx].messages.forEach((m) => { |
|
|
chats[idx].targetId == pos.friendId) { |
|
|
|
|
|
chats[idx].messages.forEach((m) => { |
|
|
if (m.selfSend && m.status != MESSAGE_STATUS.RECALL) { |
|
|
if (m.selfSend && m.status != MESSAGE_STATUS.RECALL) { |
|
|
// pos.maxId为空表示整个会话已读
|
|
|
// pos.maxId为空表示整个会话已读
|
|
|
if(!pos.maxId || m.id <= pos.maxId){ |
|
|
if (!pos.maxId || m.id <= pos.maxId) { |
|
|
m.status = MESSAGE_STATUS.READED |
|
|
m.status = MESSAGE_STATUS.READED |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
@ -89,28 +120,25 @@ export default { |
|
|
this.commit("saveToStorage"); |
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
removeChat(state, idx) { |
|
|
removeChat(state, idx) { |
|
|
state.chats.splice(idx, 1); |
|
|
let chats = this.getters.findChats(); |
|
|
|
|
|
chats[idx].delete = true; |
|
|
this.commit("saveToStorage"); |
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
removePrivateChat(state, userId) { |
|
|
removePrivateChat(state, userId) { |
|
|
for (let idx in state.chats) { |
|
|
let chats = this.getters.findChats(); |
|
|
if (state.chats[idx].type == 'PRIVATE' && |
|
|
for (let idx in chats) { |
|
|
state.chats[idx].targetId == userId) { |
|
|
if (chats[idx].type == 'PRIVATE' && |
|
|
|
|
|
chats[idx].targetId == userId) { |
|
|
this.commit("removeChat", idx); |
|
|
this.commit("removeChat", idx); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
moveTop(state, idx) { |
|
|
moveTop(state, idx) { |
|
|
// 加载中不移动,很耗性能
|
|
|
let chats = this.getters.findChats(); |
|
|
if(state.loadingPrivateMsg || state.loadingGroupMsg){ |
|
|
let chat = chats[idx]; |
|
|
return ; |
|
|
// 最新的时间会显示在顶部
|
|
|
} |
|
|
chat.lastSendTime = new Date().getTime(); |
|
|
if (idx > 0) { |
|
|
this.commit("saveToStorage"); |
|
|
let chat = state.chats[idx]; |
|
|
|
|
|
state.chats.splice(idx, 1); |
|
|
|
|
|
state.chats.unshift(chat); |
|
|
|
|
|
this.commit("saveToStorage"); |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
}, |
|
|
insertMessage(state, msgInfo) { |
|
|
insertMessage(state, msgInfo) { |
|
|
// 获取对方id或群id
|
|
|
// 获取对方id或群id
|
|
|
@ -125,46 +153,45 @@ export default { |
|
|
// 如果是已存在消息,则覆盖旧的消息数据
|
|
|
// 如果是已存在消息,则覆盖旧的消息数据
|
|
|
let chat = this.getters.findChat(msgInfo); |
|
|
let chat = this.getters.findChat(msgInfo); |
|
|
let message = this.getters.findMessage(chat, msgInfo); |
|
|
let message = this.getters.findMessage(chat, msgInfo); |
|
|
if(message){ |
|
|
if (message) { |
|
|
Object.assign(message, msgInfo); |
|
|
Object.assign(message, msgInfo); |
|
|
// 撤回消息需要显示
|
|
|
// 撤回消息需要显示
|
|
|
if(msgInfo.type == MESSAGE_TYPE.RECALL){ |
|
|
if (msgInfo.type == MESSAGE_TYPE.RECALL) { |
|
|
chat.lastContent = msgInfo.content; |
|
|
chat.lastContent = msgInfo.content; |
|
|
} |
|
|
} |
|
|
this.commit("saveToStorage"); |
|
|
this.commit("saveToStorage"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
// 会话列表内容
|
|
|
// 会话列表内容
|
|
|
if(!state.loadingPrivateMsg && !state.loadingGroupMsg){ |
|
|
if (msgInfo.type == MESSAGE_TYPE.IMAGE) { |
|
|
if (msgInfo.type == MESSAGE_TYPE.IMAGE) { |
|
|
chat.lastContent = "[图片]"; |
|
|
chat.lastContent = "[图片]"; |
|
|
} else if (msgInfo.type == MESSAGE_TYPE.FILE) { |
|
|
} else if (msgInfo.type == MESSAGE_TYPE.FILE) { |
|
|
chat.lastContent = "[文件]"; |
|
|
chat.lastContent = "[文件]"; |
|
|
} else if (msgInfo.type == MESSAGE_TYPE.AUDIO) { |
|
|
} else if (msgInfo.type == MESSAGE_TYPE.AUDIO) { |
|
|
chat.lastContent = "[语音]"; |
|
|
chat.lastContent = "[语音]"; |
|
|
} else if (msgInfo.type == MESSAGE_TYPE.TEXT || msgInfo.type == MESSAGE_TYPE.RECALL) { |
|
|
} else if (msgInfo.type == MESSAGE_TYPE.TEXT || msgInfo.type == MESSAGE_TYPE.RECALL) { |
|
|
chat.lastContent = msgInfo.content; |
|
|
chat.lastContent = msgInfo.content; |
|
|
} else if (msgInfo.type == MESSAGE_TYPE.RT_VOICE) { |
|
|
} else if (msgInfo.type == MESSAGE_TYPE.RT_VOICE) { |
|
|
chat.lastContent = "[语音通话]"; |
|
|
chat.lastContent = "[语音通话]"; |
|
|
} else if (msgInfo.type == MESSAGE_TYPE.RT_VIDEO) { |
|
|
} else if (msgInfo.type == MESSAGE_TYPE.RT_VIDEO) { |
|
|
chat.lastContent = "[视频通话]"; |
|
|
chat.lastContent = "[视频通话]"; |
|
|
|
|
|
} |
|
|
|
|
|
chat.lastSendTime = msgInfo.sendTime; |
|
|
|
|
|
chat.sendNickName = msgInfo.sendNickName; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
chat.lastSendTime = msgInfo.sendTime; |
|
|
|
|
|
chat.sendNickName = msgInfo.sendNickName; |
|
|
|
|
|
|
|
|
// 未读加1
|
|
|
// 未读加1
|
|
|
if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED |
|
|
if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED && |
|
|
&& msgInfo.type != MESSAGE_TYPE.TIP_TEXT) { |
|
|
msgInfo.type != MESSAGE_TYPE.TIP_TEXT) { |
|
|
chat.unreadCount++; |
|
|
chat.unreadCount++; |
|
|
} |
|
|
} |
|
|
// 是否有人@我
|
|
|
// 是否有人@我
|
|
|
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 userId = userStore.state.userInfo.id; |
|
|
if(msgInfo.atUserIds.indexOf(userId)>=0){ |
|
|
if (msgInfo.atUserIds.indexOf(userId) >= 0) { |
|
|
chat.atMe = true; |
|
|
chat.atMe = true; |
|
|
} |
|
|
} |
|
|
if(msgInfo.atUserIds.indexOf(-1)>=0){ |
|
|
if (msgInfo.atUserIds.indexOf(-1) >= 0) { |
|
|
chat.atAll = true; |
|
|
chat.atAll = true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -178,21 +205,29 @@ export default { |
|
|
} |
|
|
} |
|
|
// 根据id顺序插入,防止消息乱序
|
|
|
// 根据id顺序插入,防止消息乱序
|
|
|
let insertPos = chat.messages.length; |
|
|
let insertPos = chat.messages.length; |
|
|
for (let idx in chat.messages) { |
|
|
// 防止 图片、文件 在发送方 显示 在顶端 因为还没存库,id=0
|
|
|
if (chat.messages[idx].id && msgInfo.id < chat.messages[idx].id) { |
|
|
if (msgInfo.id && msgInfo.id > 0) { |
|
|
insertPos = idx; |
|
|
for (let idx in chat.messages) { |
|
|
console.log(`消息出现乱序,位置:${chat.messages.length},修正至:${insertPos}`); |
|
|
if (chat.messages[idx].id && msgInfo.id < chat.messages[idx].id) { |
|
|
break; |
|
|
insertPos = idx; |
|
|
|
|
|
console.log(`消息出现乱序,位置:${chat.messages.length},修正至:${insertPos}`); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
chat.messages.splice(insertPos, 0, msgInfo); |
|
|
if (insertPos == chat.messages.length) { |
|
|
|
|
|
// 这种赋值效率最高
|
|
|
|
|
|
chat.messages[insertPos] = msgInfo; |
|
|
|
|
|
} else { |
|
|
|
|
|
chat.messages.splice(insertPos, 0, msgInfo); |
|
|
|
|
|
} |
|
|
this.commit("saveToStorage"); |
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
updateMessage(state, msgInfo) { |
|
|
updateMessage(state, msgInfo) { |
|
|
// 获取对方id或群id
|
|
|
// 获取对方id或群id
|
|
|
let chat = this.getters.findChat(msgInfo); |
|
|
let chat = this.getters.findChat(msgInfo); |
|
|
let message = this.getters.findMessage(chat, msgInfo); |
|
|
let message = this.getters.findMessage(chat, msgInfo); |
|
|
if(message){ |
|
|
if (message) { |
|
|
// 属性拷贝
|
|
|
// 属性拷贝
|
|
|
Object.assign(message, msgInfo); |
|
|
Object.assign(message, msgInfo); |
|
|
this.commit("saveToStorage"); |
|
|
this.commit("saveToStorage"); |
|
|
@ -204,21 +239,22 @@ export default { |
|
|
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[idx].delete = true; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
// 正在发送中的消息可能没有id,根据发送时间删除
|
|
|
// 正在发送中的消息可能没有id,根据发送时间删除
|
|
|
if (msgInfo.selfSend && chat.messages[idx].selfSend && |
|
|
if (msgInfo.selfSend && chat.messages[idx].selfSend && |
|
|
chat.messages[idx].sendTime == msgInfo.sendTime) { |
|
|
chat.messages[idx].sendTime == msgInfo.sendTime) { |
|
|
chat.messages.splice(idx, 1); |
|
|
chat.messages[idx].delete = true; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
this.commit("saveToStorage"); |
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
updateChatFromFriend(state, friend) { |
|
|
updateChatFromFriend(state, friend) { |
|
|
for (let i in state.chats) { |
|
|
let chats = this.getters.findChats(); |
|
|
let chat = state.chats[i]; |
|
|
for (let i in chats) { |
|
|
|
|
|
let chat = chats[i]; |
|
|
if (chat.type == 'PRIVATE' && chat.targetId == friend.id) { |
|
|
if (chat.type == 'PRIVATE' && chat.targetId == friend.id) { |
|
|
chat.headImage = friend.headImageThumb; |
|
|
chat.headImage = friend.headImageThumb; |
|
|
chat.showName = friend.nickName; |
|
|
chat.showName = friend.nickName; |
|
|
@ -228,8 +264,9 @@ export default { |
|
|
this.commit("saveToStorage"); |
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
updateChatFromGroup(state, group) { |
|
|
updateChatFromGroup(state, group) { |
|
|
for (let i in state.chats) { |
|
|
let chats = this.getters.findChats(); |
|
|
let chat = state.chats[i]; |
|
|
for (let i in chats) { |
|
|
|
|
|
let chat = chats[i]; |
|
|
if (chat.type == 'GROUP' && chat.targetId == group.id) { |
|
|
if (chat.type == 'GROUP' && chat.targetId == group.id) { |
|
|
chat.headImage = group.headImageThumb; |
|
|
chat.headImage = group.headImageThumb; |
|
|
chat.showName = group.remark; |
|
|
chat.showName = group.remark; |
|
|
@ -240,40 +277,30 @@ export default { |
|
|
}, |
|
|
}, |
|
|
loadingPrivateMsg(state, loadding) { |
|
|
loadingPrivateMsg(state, loadding) { |
|
|
state.loadingPrivateMsg = loadding; |
|
|
state.loadingPrivateMsg = loadding; |
|
|
if(!state.loadingPrivateMsg && !state.loadingGroupMsg){ |
|
|
if (!this.getters.isLoading()) { |
|
|
this.commit("refreshChats") |
|
|
this.commit("refreshChats") |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
loadingGroupMsg(state, loadding) { |
|
|
loadingGroupMsg(state, loadding) { |
|
|
state.loadingGroupMsg = loadding; |
|
|
state.loadingGroupMsg = loadding; |
|
|
if(!state.loadingPrivateMsg && !state.loadingGroupMsg){ |
|
|
if (!this.getters.isLoading()) { |
|
|
this.commit("refreshChats") |
|
|
this.commit("refreshChats") |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
refreshChats(state){ |
|
|
refreshChats(state) { |
|
|
state.chats.forEach((chat)=>{ |
|
|
// 排序
|
|
|
if(chat.messages.length>0){ |
|
|
cacheChats.sort((chat1, chat2) => { |
|
|
let msgInfo = chat.messages[chat.messages.length-1]; |
|
|
return chat2.lastSendTime - chat1.lastSendTime; |
|
|
if (msgInfo.type == MESSAGE_TYPE.IMAGE) { |
|
|
|
|
|
chat.lastContent = "[图片]"; |
|
|
|
|
|
} else if (msgInfo.type == MESSAGE_TYPE.FILE) { |
|
|
|
|
|
chat.lastContent = "[文件]"; |
|
|
|
|
|
} else if (msgInfo.type == MESSAGE_TYPE.AUDIO) { |
|
|
|
|
|
chat.lastContent = "[语音]"; |
|
|
|
|
|
} else if (msgInfo.type == MESSAGE_TYPE.TEXT || msgInfo.type == MESSAGE_TYPE.RECALL) { |
|
|
|
|
|
chat.lastContent = msgInfo.content; |
|
|
|
|
|
} |
|
|
|
|
|
chat.lastSendTime = msgInfo.sendTime; |
|
|
|
|
|
}else{ |
|
|
|
|
|
chat.lastContent = ""; |
|
|
|
|
|
chat.lastSendTime = new Date().getTime() |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
state.chats.sort((chat1, chat2) => { |
|
|
|
|
|
return chat2.lastSendTime-chat1.lastSendTime; |
|
|
|
|
|
}); |
|
|
}); |
|
|
|
|
|
// 将消息一次性装载回来
|
|
|
|
|
|
state.chats = cacheChats; |
|
|
|
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
saveToStorage(state) { |
|
|
saveToStorage(state) { |
|
|
|
|
|
// 加载中不保存,防止卡顿
|
|
|
|
|
|
if (this.getters.isLoading()) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
let userId = userStore.state.userInfo.id; |
|
|
let userId = userStore.state.userInfo.id; |
|
|
let key = "chats-" + userId; |
|
|
let key = "chats-" + userId; |
|
|
let chatsData = { |
|
|
let chatsData = { |
|
|
@ -283,12 +310,12 @@ export default { |
|
|
} |
|
|
} |
|
|
uni.setStorage({ |
|
|
uni.setStorage({ |
|
|
key: key, |
|
|
key: key, |
|
|
data: chatsData |
|
|
data: chatsData , |
|
|
}) |
|
|
}) |
|
|
}, |
|
|
}, |
|
|
clear(state) { |
|
|
clear(state) { |
|
|
|
|
|
cacheChats = []; |
|
|
state.chats = []; |
|
|
state.chats = []; |
|
|
state.activeIndex = -1; |
|
|
|
|
|
state.privateMsgMaxId = 0; |
|
|
state.privateMsgMaxId = 0; |
|
|
state.groupMsgMaxId = 0; |
|
|
state.groupMsgMaxId = 0; |
|
|
state.loadingPrivateMsg = false; |
|
|
state.loadingPrivateMsg = false; |
|
|
@ -313,24 +340,32 @@ export default { |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
getters: { |
|
|
getters: { |
|
|
findChatIdx: (state) => (chat) => { |
|
|
isLoading: (state) => () => { |
|
|
for (let idx in state.chats) { |
|
|
return state.loadingPrivateMsg || state.loadingGroupMsg |
|
|
if (state.chats[idx].type == chat.type && |
|
|
}, |
|
|
state.chats[idx].targetId === chat.targetId) { |
|
|
findChats: (state, getters) => () => { |
|
|
|
|
|
return getters.isLoading() ? cacheChats : state.chats; |
|
|
|
|
|
}, |
|
|
|
|
|
findChatIdx: (state, getters) => (chat) => { |
|
|
|
|
|
let chats = getters.findChats(); |
|
|
|
|
|
for (let idx in chats) { |
|
|
|
|
|
if (chats[idx].type == chat.type && |
|
|
|
|
|
chats[idx].targetId === chat.targetId) { |
|
|
chat = state.chats[idx]; |
|
|
chat = state.chats[idx]; |
|
|
return idx; |
|
|
return idx; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
findChat: (state) => (msgInfo) => { |
|
|
findChat: (state, getters) => (msgInfo) => { |
|
|
|
|
|
let chats = getters.findChats(); |
|
|
// 获取对方id或群id
|
|
|
// 获取对方id或群id
|
|
|
let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE'; |
|
|
let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE'; |
|
|
let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId; |
|
|
let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId; |
|
|
let chat = null; |
|
|
let chat = null; |
|
|
for (let idx in state.chats) { |
|
|
for (let idx in chats) { |
|
|
if (state.chats[idx].type == type && |
|
|
if (chats[idx].type == type && |
|
|
state.chats[idx].targetId === targetId) { |
|
|
chats[idx].targetId === targetId) { |
|
|
chat = state.chats[idx]; |
|
|
chat = chats[idx]; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|