diff --git a/im-uniapp/App.vue b/im-uniapp/App.vue index e9264ca..eebe499 100644 --- a/im-uniapp/App.vue +++ b/im-uniapp/App.vue @@ -9,7 +9,6 @@ import UNI_APP from '@/.env.js' export default { data() { return { - isInit: false, // 是否已经初始化 isExit: false, // 是否已退出 audioTip: null, reconnecting: false // 正在重连标志 @@ -18,13 +17,12 @@ export default { methods: { init() { this.reconnecting = false; - this.isExit = false; + this.configStore.setAppInit(false); // 加载数据 this.loadStore().then(() => { // 初始化websocket this.initWebSocket(); - this.isInit = true; - }).catch((e) => { + }).catch(e => { console.log(e); this.exit(); }) @@ -40,7 +38,7 @@ export default { // 加载离线消息 this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId); this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId); - + this.configStore.setAppInit(true); } }); wsApi.onMessage((cmd, msgInfo) => { @@ -66,7 +64,7 @@ export default { console.log("ws断开", res); // 重新连接 this.reconnectWs(); - + this.configStore.setAppInit(false); }) }, loadStore() { @@ -92,7 +90,11 @@ export default { url: "/message/private/pullOfflineMessage?minId=" + minId, method: 'GET' }).catch(() => { - this.chatStore.setLoadingPrivateMsg(false) + uni.showToast({ + title: "消息拉取失败,请重新登陆", + icon: 'none' + }) + this.exit() }) }, pullGroupOfflineMessage(minId) { @@ -101,7 +103,11 @@ export default { url: "/message/group/pullOfflineMessage?minId=" + minId, method: 'GET' }).catch(() => { - this.chatStore.setLoadingGroupMsg(false) + uni.showToast({ + title: "消息拉取失败,请重新登陆", + icon: 'none' + }) + this.exit() }) }, handlePrivateMessage(msg) { @@ -421,6 +427,7 @@ export default { // 加载离线消息 this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId); this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId); + this.configStore.setAppInit(true); }).catch((e) => { console.log(e); this.exit(); diff --git a/im-uniapp/pages/chat/chat.vue b/im-uniapp/pages/chat/chat.vue index 4bc52ad..f0081ec 100644 --- a/im-uniapp/pages/chat/chat.vue +++ b/im-uniapp/pages/chat/chat.vue @@ -114,7 +114,7 @@ export default { return this.chatStore.isLoading(); }, initializing() { - return !getApp().$vm.isInit; + return !this.configStore.appInit; }, showChats() { this.chatStore.chats.filter((chat) => !chat.delete && chat.showName && chat.showName.includes(this diff --git a/im-uniapp/store/chatStore.js b/im-uniapp/store/chatStore.js index 78c0c60..0d67b83 100644 --- a/im-uniapp/store/chatStore.js +++ b/im-uniapp/store/chatStore.js @@ -3,6 +3,7 @@ import { MESSAGE_TYPE, MESSAGE_STATUS } from '@/common/enums.js'; import useFriendStore from './friendStore.js'; import useGroupStore from './groupStore.js'; import useUserStore from './userStore'; +import useConfigStore from './configStore.js'; let cacheChats = []; export default defineStore('chatStore', { @@ -155,12 +156,15 @@ export default defineStore('chatStore', { 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; + // 完成初始化之前不能修改消息最大id,否则可能导致拉不到离线消息 + if (useConfigStore().appInit) { + // 记录消息的最大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); @@ -264,7 +268,7 @@ export default defineStore('chatStore', { } if (delIdx >= 0) { chat.messages.splice(delIdx, 1); - if( delIdx < chat.hotMinIdx){ + if (delIdx < chat.hotMinIdx) { isColdMessage = true; chat.hotMinIdx--; } diff --git a/im-uniapp/store/configStore.js b/im-uniapp/store/configStore.js index fde339e..0048818 100644 --- a/im-uniapp/store/configStore.js +++ b/im-uniapp/store/configStore.js @@ -4,6 +4,7 @@ import http from '../common/request' export default defineStore('configStore', { state: () => { return { + appInit: false, webrtc: {} } }, @@ -11,6 +12,9 @@ export default defineStore('configStore', { setConfig(config) { this.webrtc = config.webrtc; }, + setAppInit(appInit) { + this.appInit = appInit; + }, clear() { this.webrtc = {}; }, diff --git a/im-web/src/store/chatStore.js b/im-web/src/store/chatStore.js index e672517..c0be3f6 100644 --- a/im-web/src/store/chatStore.js +++ b/im-web/src/store/chatStore.js @@ -3,6 +3,7 @@ import { MESSAGE_TYPE, MESSAGE_STATUS } from "../api/enums.js" import useFriendStore from './friendStore.js'; import useGroupStore from './groupStore.js'; import useUserStore from './userStore.js'; +import useConfigStore from './configStore.js'; import localForage from 'localforage'; /** @@ -155,12 +156,15 @@ export default defineStore('chatStore', { }, insertMessage(msgInfo, chatInfo) { 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; + // 完成初始化之前不能修改消息最大id,否则可能导致拉不到离线消息 + if (useConfigStore().appInit) { + // 记录消息的最大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); diff --git a/im-web/src/store/configStore.js b/im-web/src/store/configStore.js index cc18be2..86321c2 100644 --- a/im-web/src/store/configStore.js +++ b/im-web/src/store/configStore.js @@ -4,6 +4,7 @@ import http from '../api/httpRequest.js' export default defineStore('configStore', { state: () => { return { + appInit: false, // 应用是否完成初始化 webrtc: {} } }, @@ -11,6 +12,9 @@ export default defineStore('configStore', { setConfig(config) { this.webrtc = config.webrtc; }, + setAppInit(appInit) { + this.appInit = appInit; + }, loadConfig() { return new Promise((resolve, reject) => { http({ diff --git a/im-web/src/view/Home.vue b/im-web/src/view/Home.vue index 67b7d5d..4fb89c7 100644 --- a/im-web/src/view/Home.vue +++ b/im-web/src/view/Home.vue @@ -99,6 +99,7 @@ export default { // 图片全屏 this.$refs.fullImage.open(url); }); + this.configStore.setAppInit(false) this.loadStore().then(() => { // ws初始化 this.$wsApi.connect(process.env.VUE_APP_WS_URL, sessionStorage.getItem("accessToken")); @@ -109,6 +110,7 @@ export default { // 加载离线消息 this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId); this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId); + this.configStore.setAppInit(true); } }); this.$wsApi.onMessage((cmd, msgInfo) => { @@ -138,6 +140,7 @@ export default { if (e.code != 3000) { // 断线重连 this.reconnectWs(); + this.configStore.setAppInit(false) } }); }).catch((e) => { @@ -169,6 +172,7 @@ export default { // 加载离线消息 this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId); this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId); + this.configStore.setAppInit(true) this.$message.success("重新连接成功"); }).catch(() => { this.$message.error("初始化失败"); @@ -387,6 +391,7 @@ export default { }, onExit() { this.unloadStore(); + this.configStore.setAppInit(false); this.$wsApi.close(3000); sessionStorage.removeItem("accessToken"); location.href = "/";