| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- import { defineStore } from 'pinia';
- import { MESSAGE_TYPE, MESSAGE_STATUS } from '@/common/enums.js';
- import useUserStore from './userStore';
- let cacheChats = [];
- export default defineStore('chatStore', {
- state: () => {
- return {
- chats: [],
- privateMsgMaxId: 0,
- groupMsgMaxId: 0,
- loadingPrivateMsg: false,
- loadingGroupMsg: false
- }
- },
- actions: {
- initChats(chatsData) {
- cacheChats = [];
- this.chats = [];
- for (let chat of chatsData.chats) {
- // 暂存至缓冲区
- chat.stored = false;
- cacheChats.push(JSON.parse(JSON.stringify(chat)));
- // 加载期间显示只前15个会话做做样子,一切都为了加快初始化时间
- if (this.chats.length < 15) {
- this.chats.push(chat);
- }
- }
- this.privateMsgMaxId = chatsData.privateMsgMaxId || 0;
- this.groupMsgMaxId = chatsData.groupMsgMaxId || 0;
- // 防止图片一直处在加载中状态
- cacheChats.forEach((chat) => {
- chat.messages.forEach((msg) => {
- if (msg.loadStatus == "loading") {
- msg.loadStatus = "fail"
- }
- })
- })
- },
- openChat(chatInfo) {
- let chats = this.curChats;
- let chat = null;
- for (let idx in chats) {
- if (chats[idx].type == chatInfo.type &&
- chats[idx].targetId === chatInfo.targetId) {
- chat = chats[idx];
- // 放置头部
- this.moveTop(idx)
- 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: [],
- atMe: false,
- atAll: false,
- stored: false
- };
- chats.unshift(chat);
- this.saveToStorage();
- }
- },
- activeChat(idx) {
- let chats = this.curChats;
- if (idx >= 0) {
- chats[idx].unreadCount = 0;
- }
- },
- resetUnreadCount(chatInfo) {
- let chats = this.curChats;
- for (let idx in chats) {
- if (chats[idx].type == chatInfo.type &&
- chats[idx].targetId == chatInfo.targetId) {
- chats[idx].unreadCount = 0;
- chats[idx].atMe = false;
- chats[idx].atAll = false;
- chats[idx].stored = false;
- this.saveToStorage();
- }
- }
- },
- readedMessage(pos) {
- let chat = this.findChatByFriend(pos.friendId);
- chat.messages.forEach((m) => {
- if (m.id && m.selfSend && m.status < MESSAGE_STATUS.RECALL) {
- // pos.maxId为空表示整个会话已读
- if (!pos.maxId || m.id <= pos.maxId) {
- m.status = MESSAGE_STATUS.READED
- chat.stored = false;
- }
- }
- })
- if (!chat.stored) {
- this.saveToStorage();
- }
- },
- removeChat(idx) {
- let chats = this.curChats;
- chats[idx].delete = true;
- chats[idx].stored = false;
- this.saveToStorage();
- },
- removePrivateChat(userId) {
- let chats = this.curChats;
- for (let idx in chats) {
- if (chats[idx].type == 'PRIVATE' &&
- chats[idx].targetId == userId) {
- this.removeChat(idx);
- }
- }
- },
- removeGroupChat(groupId) {
- let chats = this.curChats;
- for (let idx in chats) {
- if (chats[idx].type == 'GROUP' &&
- chats[idx].targetId == groupId) {
- this.removeChat(idx);
- }
- }
- },
- moveTop(idx) {
- if (this.isLoading()) {
- return;
- }
- let chats = this.curChats;
- if (idx > 0) {
- let chat = chats[idx];
- chats.splice(idx, 1);
- chats.unshift(chat);
- chat.lastSendTime = new Date().getTime();
- chat.stored = false;
- this.saveToStorage();
- }
- },
- insertMessage(msgInfo, chatInfo) {
- // 获取对方id或群id
- let type = chatInfo.type;
- // 记录消息的最大id
- if (msgInfo.id && type == "PRIVATE" && msgInfo.id > this.privateMsgMaxId) {
- this.privateMsgMaxId = msgInfo.id;
- }
- if (msgInfo.id && type == "GROUP" && msgInfo.id > this.groupMsgMaxId) {
- this.groupMsgMaxId = msgInfo.id;
- }
- // 如果是已存在消息,则覆盖旧的消息数据
- let chat = this.findChat(chatInfo);
- let message = this.findMessage(chat, msgInfo);
- if (message) {
- Object.assign(message, msgInfo);
- chat.stored = false;
- this.saveToStorage();
- return;
- }
- // 会话列表内容
- 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.ACT_RT_VOICE) {
- chat.lastContent = "[语音通话]";
- } else if (msgInfo.type == MESSAGE_TYPE.ACT_RT_VIDEO) {
- chat.lastContent = "[视频通话]";
- } else if (msgInfo.type == MESSAGE_TYPE.TEXT ||
- msgInfo.type == MESSAGE_TYPE.RECALL ||
- msgInfo.type == MESSAGE_TYPE.TIP_TEXT) {
- chat.lastContent = msgInfo.content;
- }
- chat.lastSendTime = msgInfo.sendTime;
- chat.sendNickName = msgInfo.sendNickName;
- // 未读加1
- if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED &&
- msgInfo.status != MESSAGE_STATUS.RECALL && msgInfo.type != MESSAGE_TYPE.TIP_TEXT) {
- chat.unreadCount++;
- }
- // 是否有人@我
- if (!msgInfo.selfSend && chat.type == "GROUP" && msgInfo.atUserIds &&
- msgInfo.status != MESSAGE_STATUS.READED) {
- const userStore = useUserStore();
- let userId = userStore.userInfo.id;
- if (msgInfo.atUserIds.indexOf(userId) >= 0) {
- chat.atMe = true;
- }
- if (msgInfo.atUserIds.indexOf(-1) >= 0) {
- chat.atAll = true;
- }
- }
- // 间隔大于10分钟插入时间显示
- if (!chat.lastTimeTip || (chat.lastTimeTip < msgInfo.sendTime - 600 * 1000)) {
- chat.messages.push({
- sendTime: msgInfo.sendTime,
- type: MESSAGE_TYPE.TIP_TIME,
- });
- chat.lastTimeTip = msgInfo.sendTime;
- }
- // 根据id顺序插入,防止消息乱序
- let insertPos = chat.messages.length;
- // 防止 图片、文件 在发送方 显示 在顶端 因为还没存库,id=0
- if (msgInfo.id && msgInfo.id > 0) {
- for (let idx in chat.messages) {
- if (chat.messages[idx].id && msgInfo.id < chat.messages[idx].id) {
- insertPos = idx;
- console.log(`消息出现乱序,位置:${chat.messages.length},修正至:${insertPos}`);
- break;
- }
- }
- }
- if (insertPos == chat.messages.length) {
- // 这种赋值效率最高
- chat.messages[insertPos] = msgInfo;
- } else {
- chat.messages.splice(insertPos, 0, msgInfo);
- }
- chat.stored = false;
- this.saveToStorage();
- },
- updateMessage(msgInfo, chatInfo) {
- // 获取对方id或群id
- let chat = this.findChat(chatInfo);
- let message = this.findMessage(chat, msgInfo);
- if (message) {
- // 属性拷贝
- Object.assign(message, msgInfo);
- chat.stored = false;
- this.saveToStorage();
- }
- },
- deleteMessage(msgInfo, chatInfo) {
- // 获取对方id或群id
- let chat = this.findChat(chatInfo);
- for (let idx in chat.messages) {
- // 已经发送成功的,根据id删除
- if (chat.messages[idx].id && chat.messages[idx].id == msgInfo.id) {
- chat.messages.splice(idx, 1);
- break;
- }
- // 正在发送中的消息可能没有id,只有临时id
- if (chat.messages[idx].tmpId && chat.messages[idx].tmpId == msgInfo.tmpId) {
- chat.messages.splice(idx, 1);
- break;
- }
- }
- chat.stored = false;
- this.saveToStorage();
- },
- recallMessage(msgInfo, chatInfo) {
- let chat = this.findChat(chatInfo);
- if (!chat) return;
- // 要撤回的消息id
- let id = msgInfo.content;
- let name = msgInfo.selfSend ? '你' : chat.type == 'PRIVATE' ? '对方' : msgInfo.sendNickName;
- for (let idx in chat.messages) {
- let m = chat.messages[idx];
- if (m.id && m.id == id) {
- // 改造成一条提示消息
- m.status = MESSAGE_STATUS.RECALL;
- m.content = name + "撤回了一条消息";
- m.type = MESSAGE_TYPE.TIP_TEXT
- // 会话列表
- chat.lastContent = m.content;
- chat.lastSendTime = msgInfo.sendTime;
- chat.sendNickName = '';
- if (!msgInfo.selfSend && msgInfo.status != MESSAGE_STATUS.READED) {
- chat.unreadCount++;
- }
- }
- // 被引用的消息也要撤回
- if (m.quoteMessage && m.quoteMessage.id == msgInfo.id) {
- m.quoteMessage.content = "引用内容已撤回";
- m.quoteMessage.status = MESSAGE_STATUS.RECALL;
- m.quoteMessage.type = MESSAGE_TYPE.TIP_TEXT
- }
- }
- chat.stored = false;
- this.saveToStorage();
- },
- updateChatFromFriend(friend) {
- let chat = this.findChatByFriend(friend.id)
- if (chat && (chat.headImage != friend.headImage ||
- chat.showName != friend.nickName)) {
- // 更新会话中的群名和头像
- chat.headImage = friend.headImage;
- chat.showName = friend.nickName;
- chat.stored = false;
- this.saveToStorage();
- }
- },
- updateChatFromUser(user) {
- let chat = this.findChatByFriend(user.id);
- // 更新会话中的昵称和头像
- if (chat && (chat.headImage != user.headImageThumb ||
- chat.showName != user.nickName)) {
- chat.headImage = user.headImageThumb;
- chat.showName = user.nickName;
- chat.stored = false;
- this.saveToStorage();
- }
- },
- updateChatFromGroup(group) {
- let chat = this.findChatByGroup(group.id);
- if (chat && (chat.headImage != group.headImageThumb ||
- chat.showName != group.showGroupName)) {
- // 更新会话中的群名称和头像
- chat.headImage = group.headImageThumb;
- chat.showName = group.showGroupName;
- chat.stored = false;
- this.saveToStorage();
- }
- },
- setLoadingPrivateMsg(loading) {
- this.loadingPrivateMsg = loading;
- if (!this.isLoading()) {
- this.refreshChats()
- }
- },
- setLoadingGroupMsg(loading) {
- this.loadingGroupMsg = loading;
- if (!this.isLoading()) {
- this.refreshChats()
- }
- },
- refreshChats() {
- if (!cacheChats) {
- return;
- }
- // 排序
- cacheChats.sort((chat1, chat2) => {
- return chat2.lastSendTime - chat1.lastSendTime;
- });
- // 将消息一次性装载回来
- this.chats = cacheChats;
- // 清空缓存,不再使用
- cacheChats = null;
- this.saveToStorage();
- },
- saveToStorage(state) {
- // 加载中不保存,防止卡顿
- if (this.isLoading()) {
- return;
- }
- const userStore = useUserStore();
- let userId = userStore.userInfo.id;
- let key = "chats-app-" + userId;
- let chatKeys = [];
- // 按会话为单位存储,只存储有改动的会话
- this.chats.forEach((chat) => {
- let chatKey = `${key}-${chat.type}-${chat.targetId}`
- if (!chat.stored) {
- if (chat.delete) {
- uni.removeStorageSync(chatKey);
- } else {
- uni.setStorageSync(chatKey, chat);
- }
- chat.stored = true;
- }
- if (!chat.delete) {
- chatKeys.push(chatKey);
- }
- })
- // 会话核心信息
- let chatsData = {
- privateMsgMaxId: this.privateMsgMaxId,
- groupMsgMaxId: this.groupMsgMaxId,
- chatKeys: chatKeys
- }
- uni.setStorageSync(key, chatsData)
- // 清理已删除的会话
- this.chats = this.chats.filter(chat => !chat.delete)
- },
- clear(state) {
- cacheChats = [];
- this.chats = [];
- this.privateMsgMaxId = 0;
- this.groupMsgMaxId = 0;
- this.loadingPrivateMsg = false;
- this.loadingGroupMsg = false;
- },
- loadChat(context) {
- return new Promise((resolve, reject) => {
- let userStore = useUserStore();
- let userId = userStore.userInfo.id;
- let chatsData = uni.getStorageSync("chats-app-" + userId)
- if (chatsData) {
- if (chatsData.chatKeys) {
- let time = new Date().getTime();
- chatsData.chats = [];
- chatsData.chatKeys.forEach(key => {
- let chat = uni.getStorageSync(key);
- if (chat) {
- chatsData.chats.push(chat);
- }
- })
- }
- this.initChats(chatsData);
- }
- resolve()
- })
- }
- },
- getters: {
- isLoading: (state) => () => {
- return state.loadingPrivateMsg || state.loadingGroupMsg
- },
- curChats: (state) => {
- if (cacheChats && state.isLoading()) {
- return cacheChats;
- }
- return state.chats;
- },
- findChatIdx: (state) => (chat) => {
- let chats = state.curChats;
- for (let idx in chats) {
- if (chats[idx].type == chat.type &&
- chats[idx].targetId === chat.targetId) {
- chat = state.chats[idx];
- return idx;
- }
- }
- },
- findChat: (state) => (chat) => {
- let chats = state.curChats;
- let idx = state.findChatIdx(chat);
- return chats[idx];
- },
- findChatByFriend: (state) => (fid) => {
- return state.curChats.find(chat => chat.type == 'PRIVATE' &&
- chat.targetId == fid)
- },
- findChatByGroup: (state) => (gid) => {
- return state.curChats.find(chat => chat.type == 'GROUP' &&
- chat.targetId == gid)
- },
- findMessage: (state) => (chat, msgInfo) => {
- if (!chat) {
- return null;
- }
- for (let idx in chat.messages) {
- // 通过id判断
- if (msgInfo.id && chat.messages[idx].id == msgInfo.id) {
- return chat.messages[idx];
- }
- // 正在发送中的消息可能没有id,只有tmpId
- if (msgInfo.tmpId && chat.messages[idx].tmpId &&
- chat.messages[idx].tmpId == msgInfo.tmpId) {
- return chat.messages[idx];
- }
- }
- }
- }
- });
|