You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.5 KiB

3 years ago
export default {
3 years ago
state: {
activeIndex: -1,
chats: []
},
3 years ago
mutations: {
initChatStore(state) {
state.activeIndex = -1;
},
openChat(state, chatInfo) {
3 years ago
let chat = null;
let activeChat = state.activeIndex>=0?state.chats[state.activeIndex]:null;
for (let i in state.chats) {
if (state.chats[i].type == chatInfo.type &&
state.chats[i].targetId === chatInfo.targetId) {
3 years ago
chat = state.chats[i];
// 放置头部
state.chats.splice(i, 1);
3 years ago
state.chats.unshift(chat);
break;
}
}
// 创建会话
if (chat == null) {
chat = {
targetId: chatInfo.targetId,
type: chatInfo.type,
showName: chatInfo.showName,
headImage: chatInfo.headImage,
lastContent: "",
lastSendTime: new Date().getTime(),
unreadCount: 0,
messages: [],
};
state.chats.unshift(chat);
}
// 选中会话保持不变
if(activeChat){
state.chats.forEach((chat,idx)=>{
if(activeChat.type == chat.type
&& activeChat.targetId == chat.targetId){
state.activeIndex = idx;
}
})
}
},
activeChat(state, idx) {
3 years ago
state.activeIndex = idx;
state.chats[idx].unreadCount = 0;
3 years ago
},
removeChat(state, idx) {
3 years ago
state.chats.splice(idx, 1);
if (state.activeIndex >= state.chats.length) {
state.activeIndex = state.chats.length - 1;
}
},
removeGroupChat(state, groupId) {
for (let idx in state.chats) {
if (state.chats[idx].type == 'GROUP' &&
state.chats[idx].targetId == groupId) {
this.commit("removeChat", idx);
}
}
},
removePrivateChat(state, userId) {
for (let idx in state.chats) {
if (state.chats[idx].type == 'PRIVATE' &&
state.chats[idx].targetId == userId) {
this.commit("removeChat", idx);
}
3 years ago
}
},
insertMessage(state, msgInfo) {
// 获取对方id或群id
let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE';
let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId;
let chat = null;
for (let idx in state.chats) {
if (state.chats[idx].type == type &&
state.chats[idx].targetId === targetId) {
chat = state.chats[idx];
break;
}
}
chat.lastContent = msgInfo.type == 1 ? "[图片]" : msgInfo.type == 2 ? "[文件]" : msgInfo.content;
3 years ago
chat.lastSendTime = msgInfo.sendTime;
chat.messages.push(msgInfo);
// 如果不是当前会话,未读加1
chat.unreadCount++;
if(msgInfo.selfSend){
chat.unreadCount=0;
3 years ago
}
},
handleFileUpload(state, info) {
// 文件上传后数据更新
let chat = state.chats.find((c) => c.type==info.type && c.targetId === info.targetId);
let msg = chat.messages.find((m) => info.fileId == m.fileId);
msg.loadStatus = info.loadStatus;
if (info.content) {
msg.content = info.content;
}
},
updateChatFromFriend(state, friend) {
for (let i in state.chats) {
let chat = state.chats[i];
if (chat.type=='PRIVATE' && chat.targetId == friend.id) {
chat.headImage = friend.headImageThumb;
chat.showName = friend.nickName;
break;
}
}
},
updateChatFromGroup(state, group) {
for (let i in state.chats) {
let chat = state.chats[i];
if (chat.type=='GROUP' && chat.targetId == group.id) {
chat.headImage = group.headImageThumb;
chat.showName = group.remark;
3 years ago
break;
}
}
},
resetChatStore(state) {
3 years ago
state.activeIndex = -1;
state.chats = [];
}
},
}