|
|
@ -1,23 +1,33 @@ |
|
|
import {MESSAGE_TYPE} from '@/common/enums.js'; |
|
|
import { |
|
|
|
|
|
MESSAGE_TYPE, |
|
|
|
|
|
MESSAGE_STATUS |
|
|
|
|
|
} from '@/common/enums.js'; |
|
|
import userStore from './userStore'; |
|
|
import userStore from './userStore'; |
|
|
|
|
|
|
|
|
export default { |
|
|
export default { |
|
|
|
|
|
|
|
|
state: { |
|
|
state: { |
|
|
chats: [] |
|
|
activeIndex: -1, |
|
|
|
|
|
chats: [], |
|
|
|
|
|
privateMsgMaxId: 0, |
|
|
|
|
|
groupMsgMaxId: 0, |
|
|
|
|
|
loadingPrivateMsg: false, |
|
|
|
|
|
loadingGroupMsg: false, |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
mutations: { |
|
|
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)=>{ |
|
|
chat.messages.forEach((msg) => { |
|
|
if(msg.loadStatus == "loading"){ |
|
|
if (msg.loadStatus == "loading") { |
|
|
msg.loadStatus = "fail" |
|
|
msg.loadStatus = "fail" |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
state.chats = chats; |
|
|
|
|
|
}, |
|
|
}, |
|
|
openChat(state, chatInfo) { |
|
|
openChat(state, chatInfo) { |
|
|
let chat = null; |
|
|
let chat = null; |
|
|
@ -49,10 +59,33 @@ export default { |
|
|
}, |
|
|
}, |
|
|
activeChat(state, idx) { |
|
|
activeChat(state, idx) { |
|
|
state.activeIndex = idx; |
|
|
state.activeIndex = idx; |
|
|
if(idx>=0){ |
|
|
if (idx >= 0) { |
|
|
state.chats[idx].unreadCount = 0; |
|
|
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) { |
|
|
removeChat(state, idx) { |
|
|
state.chats.splice(idx, 1); |
|
|
state.chats.splice(idx, 1); |
|
|
this.commit("saveToStorage"); |
|
|
this.commit("saveToStorage"); |
|
|
@ -73,7 +106,7 @@ export default { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
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); |
|
|
@ -94,41 +127,44 @@ export default { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
// 插入新的数据
|
|
|
// 插入新的数据
|
|
|
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{ |
|
|
} else { |
|
|
chat.lastContent = msgInfo.content; |
|
|
chat.lastContent = msgInfo.content; |
|
|
} |
|
|
} |
|
|
chat.lastSendTime = msgInfo.sendTime; |
|
|
chat.lastSendTime = msgInfo.sendTime; |
|
|
// 如果不是当前会话,未读加1
|
|
|
// 未读加1
|
|
|
if(chatIdx != state.activeIndex){ |
|
|
if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED) { |
|
|
chat.unreadCount++; |
|
|
chat.unreadCount++; |
|
|
} |
|
|
} |
|
|
// 自己回复了消息,说明消息已读
|
|
|
// 记录消息的最大id
|
|
|
if(msgInfo.selfSend){ |
|
|
if (msgInfo.id && type == "PRIVATE" && msgInfo.id > state.privateMsgMaxId) { |
|
|
chat.unreadCount=0; |
|
|
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"); |
|
|
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"); |
|
|
this.commit("saveToStorage"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
// 间隔大于10分钟插入时间显示
|
|
|
// 间隔大于10分钟插入时间显示
|
|
|
if(!chat.lastTimeTip || (chat.lastTimeTip < msgInfo.sendTime - 600*1000)){ |
|
|
if (!chat.lastTimeTip || (chat.lastTimeTip < msgInfo.sendTime - 600 * 1000)) { |
|
|
chat.messages.push({ |
|
|
chat.messages.push({ |
|
|
sendTime: msgInfo.sendTime, |
|
|
sendTime: msgInfo.sendTime, |
|
|
type: MESSAGE_TYPE.TIP_TIME, |
|
|
type: MESSAGE_TYPE.TIP_TIME, |
|
|
@ -138,9 +174,9 @@ export default { |
|
|
// 新的消息
|
|
|
// 新的消息
|
|
|
chat.messages.push(msgInfo); |
|
|
chat.messages.push(msgInfo); |
|
|
this.commit("saveToStorage"); |
|
|
this.commit("saveToStorage"); |
|
|
|
|
|
|
|
|
}, |
|
|
}, |
|
|
deleteMessage(state, msgInfo){ |
|
|
deleteMessage(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; |
|
|
@ -152,16 +188,16 @@ export default { |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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); |
|
|
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; |
|
|
} |
|
|
} |
|
|
@ -171,7 +207,7 @@ export default { |
|
|
updateChatFromFriend(state, friend) { |
|
|
updateChatFromFriend(state, friend) { |
|
|
for (let i in state.chats) { |
|
|
for (let i in state.chats) { |
|
|
let chat = state.chats[i]; |
|
|
let chat = state.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; |
|
|
break; |
|
|
break; |
|
|
@ -182,7 +218,7 @@ export default { |
|
|
updateChatFromGroup(state, group) { |
|
|
updateChatFromGroup(state, group) { |
|
|
for (let i in state.chats) { |
|
|
for (let i in state.chats) { |
|
|
let chat = state.chats[i]; |
|
|
let chat = state.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; |
|
|
break; |
|
|
break; |
|
|
@ -190,35 +226,50 @@ export default { |
|
|
} |
|
|
} |
|
|
this.commit("saveToStorage"); |
|
|
this.commit("saveToStorage"); |
|
|
}, |
|
|
}, |
|
|
saveToStorage(state){ |
|
|
loadingPrivateMsg(state, loadding) { |
|
|
|
|
|
state.loadingPrivateMsg = loadding; |
|
|
|
|
|
}, |
|
|
|
|
|
loadingGroupMsg(state, loadding) { |
|
|
|
|
|
state.loadingGroupMsg = loadding; |
|
|
|
|
|
}, |
|
|
|
|
|
saveToStorage(state) { |
|
|
let userId = userStore.state.userInfo.id; |
|
|
let userId = userStore.state.userInfo.id; |
|
|
|
|
|
let key = "chats-" + userId; |
|
|
|
|
|
let chatsData = { |
|
|
|
|
|
privateMsgMaxId: state.privateMsgMaxId, |
|
|
|
|
|
groupMsgMaxId: state.groupMsgMaxId, |
|
|
|
|
|
chats: state.chats |
|
|
|
|
|
} |
|
|
uni.setStorage({ |
|
|
uni.setStorage({ |
|
|
key:"chats-"+userId, |
|
|
key: key, |
|
|
data: state.chats |
|
|
data: chatsData |
|
|
}) |
|
|
}) |
|
|
}, |
|
|
}, |
|
|
clear(state){ |
|
|
clear(state) { |
|
|
state.chats = []; |
|
|
state.chats = []; |
|
|
|
|
|
state.activeIndex = -1; |
|
|
|
|
|
state.privateMsgMaxId = 0; |
|
|
|
|
|
state.groupMsgMaxId = 0; |
|
|
|
|
|
state.loadingPrivateMsg = false; |
|
|
|
|
|
state.loadingGroupMsg = false; |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
actions:{ |
|
|
actions: { |
|
|
loadChat(context) { |
|
|
loadChat(context) { |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
let userId = userStore.state.userInfo.id; |
|
|
let userId = userStore.state.userInfo.id; |
|
|
uni.getStorage({ |
|
|
uni.getStorage({ |
|
|
key:"chats-"+userId, |
|
|
key: "chats-" + userId, |
|
|
success(res) { |
|
|
success(res) { |
|
|
context.commit("initChats",res.data); |
|
|
context.commit("initChats", res.data); |
|
|
resolve() |
|
|
resolve() |
|
|
}, |
|
|
}, |
|
|
fail(e) { |
|
|
fail(e) { |
|
|
// 不存在聊天记录,清空聊天列表
|
|
|
|
|
|
context.commit("initChats",[]); |
|
|
|
|
|
resolve() |
|
|
resolve() |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |