|
|
@ -1,19 +1,36 @@ |
|
|
import {MESSAGE_TYPE} from "../api/enums.js" |
|
|
import { |
|
|
|
|
|
MESSAGE_TYPE, |
|
|
|
|
|
MESSAGE_STATUS |
|
|
|
|
|
} from "../api/enums.js" |
|
|
|
|
|
import userStore from './userStore'; |
|
|
export default { |
|
|
export default { |
|
|
|
|
|
|
|
|
state: { |
|
|
state: { |
|
|
activeIndex: -1, |
|
|
activeIndex: -1, |
|
|
|
|
|
privateMsgMaxId: 0, |
|
|
|
|
|
groupMsgMaxId: 0, |
|
|
|
|
|
loadingPrivateMsg: false, |
|
|
|
|
|
loadingGroupMsg: false, |
|
|
chats: [] |
|
|
chats: [] |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
mutations: { |
|
|
mutations: { |
|
|
initChatStore(state) { |
|
|
initChats(state, chats) { |
|
|
// 防止图片一直处在加载中状态
|
|
|
state.chats = chats||[]; |
|
|
state.chats.forEach((chat) => { |
|
|
state.chats.forEach((chat) => { |
|
|
chat.messages.forEach((msg) => { |
|
|
chat.messages.forEach((msg) => { |
|
|
|
|
|
// 防止图片一直处在加载中状态
|
|
|
if (msg.loadStatus == "loading") { |
|
|
if (msg.loadStatus == "loading") { |
|
|
msg.loadStatus = "fail" |
|
|
msg.loadStatus = "fail" |
|
|
} |
|
|
} |
|
|
|
|
|
// 记录最大私聊消息id
|
|
|
|
|
|
if(chat.type == "PRIVATE" && msg.id && msg.id>state.privateMsgMaxId){ |
|
|
|
|
|
state.privateMsgMaxId = msg.id; |
|
|
|
|
|
} |
|
|
|
|
|
// 记录最大群聊消息id
|
|
|
|
|
|
if(chat.type == "GROUP" && msg.id && msg.id>state.groupMsgMaxId){ |
|
|
|
|
|
state.groupMsgMaxId = msg.id; |
|
|
|
|
|
} |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}, |
|
|
}, |
|
|
@ -47,8 +64,8 @@ export default { |
|
|
// 选中会话保持不变
|
|
|
// 选中会话保持不变
|
|
|
if (activeChat) { |
|
|
if (activeChat) { |
|
|
state.chats.forEach((chat, idx) => { |
|
|
state.chats.forEach((chat, idx) => { |
|
|
if(activeChat.type == chat.type |
|
|
if (activeChat.type == chat.type && |
|
|
&& activeChat.targetId == chat.targetId){ |
|
|
activeChat.targetId == chat.targetId) { |
|
|
state.activeIndex = idx; |
|
|
state.activeIndex = idx; |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
@ -56,20 +73,42 @@ export default { |
|
|
}, |
|
|
}, |
|
|
activeChat(state, idx) { |
|
|
activeChat(state, idx) { |
|
|
state.activeIndex = idx; |
|
|
state.activeIndex = idx; |
|
|
|
|
|
}, |
|
|
|
|
|
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; |
|
|
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) => { |
|
|
|
|
|
if (m.selfSend && m.status != MESSAGE_STATUS.RECALL) { |
|
|
|
|
|
m.status = MESSAGE_STATUS.READED |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
removeChat(state, idx) { |
|
|
removeChat(state, idx) { |
|
|
state.chats.splice(idx, 1); |
|
|
state.chats.splice(idx, 1); |
|
|
if (state.activeIndex >= state.chats.length) { |
|
|
if (state.activeIndex >= state.chats.length) { |
|
|
state.activeIndex = state.chats.length - 1; |
|
|
state.activeIndex = state.chats.length - 1; |
|
|
} |
|
|
} |
|
|
|
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
moveTop(state, idx) { |
|
|
moveTop(state, idx) { |
|
|
let chat = state.chats[idx]; |
|
|
let chat = state.chats[idx]; |
|
|
// 放置头部
|
|
|
// 放置头部
|
|
|
state.chats.splice(idx, 1); |
|
|
state.chats.splice(idx, 1); |
|
|
state.chats.unshift(chat); |
|
|
state.chats.unshift(chat); |
|
|
|
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
removeGroupChat(state, groupId) { |
|
|
removeGroupChat(state, groupId) { |
|
|
for (let idx in state.chats) { |
|
|
for (let idx in state.chats) { |
|
|
@ -79,15 +118,16 @@ export default { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
removePrivateChat(state, userId) { |
|
|
removePrivateChat(state, friendId) { |
|
|
for (let idx in state.chats) { |
|
|
for (let idx in state.chats) { |
|
|
if (state.chats[idx].type == 'PRIVATE' && |
|
|
if (state.chats[idx].type == 'PRIVATE' && |
|
|
state.chats[idx].targetId == userId) { |
|
|
state.chats[idx].targetId == friendId) { |
|
|
this.commit("removeChat", idx); |
|
|
this.commit("removeChat", idx); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
insertMessage(state, msgInfo) { |
|
|
insertMessage(state, msgInfo) { |
|
|
|
|
|
|
|
|
// 获取对方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; |
|
|
@ -110,21 +150,30 @@ export default { |
|
|
chat.lastContent = msgInfo.content; |
|
|
chat.lastContent = msgInfo.content; |
|
|
} |
|
|
} |
|
|
chat.lastSendTime = msgInfo.sendTime; |
|
|
chat.lastSendTime = msgInfo.sendTime; |
|
|
// 如果不是当前会话,未读加1
|
|
|
// 未读加1
|
|
|
|
|
|
if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED) { |
|
|
chat.unreadCount++; |
|
|
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) { |
|
|
for (let idx in chat.messages) { |
|
|
if (msgInfo.id && chat.messages[idx].id == msgInfo.id) { |
|
|
if (msgInfo.id && chat.messages[idx].id == msgInfo.id) { |
|
|
Object.assign(chat.messages[idx], msgInfo); |
|
|
Object.assign(chat.messages[idx], msgInfo); |
|
|
|
|
|
this.commit("saveToStorage"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
// 正在发送中的消息可能没有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) { |
|
|
Object.assign(chat.messages[idx], msgInfo); |
|
|
Object.assign(chat.messages[idx], msgInfo); |
|
|
|
|
|
this.commit("saveToStorage"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -138,7 +187,7 @@ export default { |
|
|
} |
|
|
} |
|
|
// 新的消息
|
|
|
// 新的消息
|
|
|
chat.messages.push(msgInfo); |
|
|
chat.messages.push(msgInfo); |
|
|
|
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
deleteMessage(state, msgInfo) { |
|
|
deleteMessage(state, msgInfo) { |
|
|
// 获取对方id或群id
|
|
|
// 获取对方id或群id
|
|
|
@ -160,12 +209,13 @@ export default { |
|
|
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.splice(idx, 1); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
updateChatFromFriend(state, friend) { |
|
|
updateChatFromFriend(state, friend) { |
|
|
for (let i in state.chats) { |
|
|
for (let i in state.chats) { |
|
|
@ -176,6 +226,7 @@ export default { |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
updateChatFromGroup(state, group) { |
|
|
updateChatFromGroup(state, group) { |
|
|
for (let i in state.chats) { |
|
|
for (let i in state.chats) { |
|
|
@ -186,11 +237,35 @@ export default { |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
this.commit("saveToStorage"); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
loadingPrivateMsg(state,loadding){ |
|
|
|
|
|
state.loadingPrivateMsg = loadding; |
|
|
|
|
|
}, |
|
|
|
|
|
loadingGroupMsg(state,loadding){ |
|
|
|
|
|
state.loadingGroupMsg = loadding; |
|
|
}, |
|
|
}, |
|
|
resetChatStore(state) { |
|
|
saveToStorage(state) { |
|
|
|
|
|
let userId = userStore.state.userInfo.id; |
|
|
|
|
|
let key = "chats-" + userId; |
|
|
|
|
|
localStorage.setItem(key, JSON.stringify(state.chats)); |
|
|
|
|
|
}, |
|
|
|
|
|
clear(state) { |
|
|
state.activeIndex = -1; |
|
|
state.activeIndex = -1; |
|
|
state.chats = []; |
|
|
state.chats = []; |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
actions: { |
|
|
|
|
|
loadChat(context) { |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
let userId = userStore.state.userInfo.id; |
|
|
|
|
|
let key = "chats-" + userId; |
|
|
|
|
|
let item = localStorage.getItem(key) |
|
|
|
|
|
let chats = JSON.parse(localStorage.getItem(key)); |
|
|
|
|
|
context.commit("initChats", chats); |
|
|
|
|
|
resolve(); |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |