|
|
|
@ -1,23 +1,33 @@ |
|
|
|
import {MESSAGE_TYPE} from '@/common/enums.js'; |
|
|
|
import { |
|
|
|
MESSAGE_TYPE, |
|
|
|
MESSAGE_STATUS |
|
|
|
} from '@/common/enums.js'; |
|
|
|
import userStore from './userStore'; |
|
|
|
|
|
|
|
export default { |
|
|
|
|
|
|
|
state: { |
|
|
|
chats: [] |
|
|
|
activeIndex: -1, |
|
|
|
chats: [], |
|
|
|
privateMsgMaxId: 0, |
|
|
|
groupMsgMaxId: 0, |
|
|
|
loadingPrivateMsg: false, |
|
|
|
loadingGroupMsg: false, |
|
|
|
}, |
|
|
|
|
|
|
|
mutations: { |
|
|
|
initChats(state,chats){ |
|
|
|
initChats(state, chatsData) { |
|
|
|
state.chats = chatsData.chats ||[]; |
|
|
|
state.privateMsgMaxId = chatsData.privateMsgMaxId||0; |
|
|
|
state.groupMsgMaxId = chatsData.groupMsgMaxId||0; |
|
|
|
// 防止图片一直处在加载中状态
|
|
|
|
chats.forEach((chat)=>{ |
|
|
|
state.chats.forEach((chat) => { |
|
|
|
chat.messages.forEach((msg) => { |
|
|
|
if (msg.loadStatus == "loading") { |
|
|
|
msg.loadStatus = "fail" |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
state.chats = chats; |
|
|
|
}, |
|
|
|
openChat(state, chatInfo) { |
|
|
|
let chat = null; |
|
|
|
@ -53,6 +63,29 @@ export default { |
|
|
|
state.chats[idx].unreadCount = 0; |
|
|
|
} |
|
|
|
}, |
|
|
|
resetUnreadCount(state, chatInfo) { |
|
|
|
for (let idx in state.chats) { |
|
|
|
if (state.chats[idx].type == chatInfo.type && |
|
|
|
state.chats[idx].targetId == chatInfo.targetId) { |
|
|
|
state.chats[idx].unreadCount = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
this.commit("saveToStorage"); |
|
|
|
}, |
|
|
|
readedMessage(state, friendId) { |
|
|
|
for (let idx in state.chats) { |
|
|
|
if (state.chats[idx].type == 'PRIVATE' && |
|
|
|
state.chats[idx].targetId == friendId) { |
|
|
|
state.chats[idx].messages.forEach((m) => { |
|
|
|
console.log("readedMessage") |
|
|
|
if (m.selfSend && m.status != MESSAGE_STATUS.RECALL) { |
|
|
|
m.status = MESSAGE_STATUS.READED |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
this.commit("saveToStorage"); |
|
|
|
}, |
|
|
|
removeChat(state, idx) { |
|
|
|
state.chats.splice(idx, 1); |
|
|
|
this.commit("saveToStorage"); |
|
|
|
@ -104,13 +137,16 @@ export default { |
|
|
|
chat.lastContent = msgInfo.content; |
|
|
|
} |
|
|
|
chat.lastSendTime = msgInfo.sendTime; |
|
|
|
// 如果不是当前会话,未读加1
|
|
|
|
if(chatIdx != state.activeIndex){ |
|
|
|
// 未读加1
|
|
|
|
if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED) { |
|
|
|
chat.unreadCount++; |
|
|
|
} |
|
|
|
// 自己回复了消息,说明消息已读
|
|
|
|
if(msgInfo.selfSend){ |
|
|
|
chat.unreadCount=0; |
|
|
|
// 记录消息的最大id
|
|
|
|
if (msgInfo.id && type == "PRIVATE" && msgInfo.id > state.privateMsgMaxId) { |
|
|
|
state.privateMsgMaxId = msgInfo.id; |
|
|
|
} |
|
|
|
if (msgInfo.id && type == "GROUP" && msgInfo.id > state.groupMsgMaxId) { |
|
|
|
state.groupMsgMaxId = msgInfo.id; |
|
|
|
} |
|
|
|
// 如果是已存在消息,则覆盖旧的消息数据
|
|
|
|
for (let idx in chat.messages) { |
|
|
|
@ -120,8 +156,8 @@ export default { |
|
|
|
return; |
|
|
|
} |
|
|
|
// 正在发送中的消息可能没有id,通过发送时间判断
|
|
|
|
if(msgInfo.selfSend && chat.messages[idx].selfSend |
|
|
|
&& chat.messages[idx].sendTime == msgInfo.sendTime){ |
|
|
|
if (msgInfo.selfSend && chat.messages[idx].selfSend && |
|
|
|
chat.messages[idx].sendTime == msgInfo.sendTime) { |
|
|
|
Object.assign(chat.messages[idx], msgInfo); |
|
|
|
this.commit("saveToStorage"); |
|
|
|
return; |
|
|
|
@ -160,8 +196,8 @@ export default { |
|
|
|
break; |
|
|
|
} |
|
|
|
// 正在发送中的消息可能没有id,根据发送时间删除
|
|
|
|
if(msgInfo.selfSend && chat.messages[idx].selfSend |
|
|
|
&&chat.messages[idx].sendTime == msgInfo.sendTime){ |
|
|
|
if (msgInfo.selfSend && chat.messages[idx].selfSend && |
|
|
|
chat.messages[idx].sendTime == msgInfo.sendTime) { |
|
|
|
chat.messages.splice(idx, 1); |
|
|
|
break; |
|
|
|
} |
|
|
|
@ -190,15 +226,32 @@ export default { |
|
|
|
} |
|
|
|
this.commit("saveToStorage"); |
|
|
|
}, |
|
|
|
loadingPrivateMsg(state, loadding) { |
|
|
|
state.loadingPrivateMsg = loadding; |
|
|
|
}, |
|
|
|
loadingGroupMsg(state, loadding) { |
|
|
|
state.loadingGroupMsg = loadding; |
|
|
|
}, |
|
|
|
saveToStorage(state) { |
|
|
|
let userId = userStore.state.userInfo.id; |
|
|
|
let key = "chats-" + userId; |
|
|
|
let chatsData = { |
|
|
|
privateMsgMaxId: state.privateMsgMaxId, |
|
|
|
groupMsgMaxId: state.groupMsgMaxId, |
|
|
|
chats: state.chats |
|
|
|
} |
|
|
|
uni.setStorage({ |
|
|
|
key:"chats-"+userId, |
|
|
|
data: state.chats |
|
|
|
key: key, |
|
|
|
data: chatsData |
|
|
|
}) |
|
|
|
}, |
|
|
|
clear(state) { |
|
|
|
state.chats = []; |
|
|
|
state.activeIndex = -1; |
|
|
|
state.privateMsgMaxId = 0; |
|
|
|
state.groupMsgMaxId = 0; |
|
|
|
state.loadingPrivateMsg = false; |
|
|
|
state.loadingGroupMsg = false; |
|
|
|
} |
|
|
|
}, |
|
|
|
actions: { |
|
|
|
@ -212,8 +265,6 @@ export default { |
|
|
|
resolve() |
|
|
|
}, |
|
|
|
fail(e) { |
|
|
|
// 不存在聊天记录,清空聊天列表
|
|
|
|
context.commit("initChats",[]); |
|
|
|
resolve() |
|
|
|
} |
|
|
|
}); |
|
|
|
|