Browse Source

修复极端情况下无法拉取离线消息的bug

master
xsx 8 months ago
parent
commit
24338c6859
  1. 23
      im-uniapp/App.vue
  2. 2
      im-uniapp/pages/chat/chat.vue
  3. 18
      im-uniapp/store/chatStore.js
  4. 4
      im-uniapp/store/configStore.js
  5. 16
      im-web/src/store/chatStore.js
  6. 4
      im-web/src/store/configStore.js
  7. 5
      im-web/src/view/Home.vue

23
im-uniapp/App.vue

@ -9,7 +9,6 @@ import UNI_APP from '@/.env.js'
export default { export default {
data() { data() {
return { return {
isInit: false, //
isExit: false, // 退 isExit: false, // 退
audioTip: null, audioTip: null,
reconnecting: false // reconnecting: false //
@ -18,13 +17,12 @@ export default {
methods: { methods: {
init() { init() {
this.reconnecting = false; this.reconnecting = false;
this.isExit = false; this.configStore.setAppInit(false);
// //
this.loadStore().then(() => { this.loadStore().then(() => {
// websocket // websocket
this.initWebSocket(); this.initWebSocket();
this.isInit = true; }).catch(e => {
}).catch((e) => {
console.log(e); console.log(e);
this.exit(); this.exit();
}) })
@ -40,7 +38,7 @@ export default {
// 线 // 线
this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId); this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId);
this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId); this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId);
this.configStore.setAppInit(true);
} }
}); });
wsApi.onMessage((cmd, msgInfo) => { wsApi.onMessage((cmd, msgInfo) => {
@ -66,7 +64,7 @@ export default {
console.log("ws断开", res); console.log("ws断开", res);
// //
this.reconnectWs(); this.reconnectWs();
this.configStore.setAppInit(false);
}) })
}, },
loadStore() { loadStore() {
@ -92,7 +90,11 @@ export default {
url: "/message/private/pullOfflineMessage?minId=" + minId, url: "/message/private/pullOfflineMessage?minId=" + minId,
method: 'GET' method: 'GET'
}).catch(() => { }).catch(() => {
this.chatStore.setLoadingPrivateMsg(false) uni.showToast({
title: "消息拉取失败,请重新登陆",
icon: 'none'
})
this.exit()
}) })
}, },
pullGroupOfflineMessage(minId) { pullGroupOfflineMessage(minId) {
@ -101,7 +103,11 @@ export default {
url: "/message/group/pullOfflineMessage?minId=" + minId, url: "/message/group/pullOfflineMessage?minId=" + minId,
method: 'GET' method: 'GET'
}).catch(() => { }).catch(() => {
this.chatStore.setLoadingGroupMsg(false) uni.showToast({
title: "消息拉取失败,请重新登陆",
icon: 'none'
})
this.exit()
}) })
}, },
handlePrivateMessage(msg) { handlePrivateMessage(msg) {
@ -421,6 +427,7 @@ export default {
// 线 // 线
this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId); this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId);
this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId); this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId);
this.configStore.setAppInit(true);
}).catch((e) => { }).catch((e) => {
console.log(e); console.log(e);
this.exit(); this.exit();

2
im-uniapp/pages/chat/chat.vue

@ -114,7 +114,7 @@ export default {
return this.chatStore.isLoading(); return this.chatStore.isLoading();
}, },
initializing() { initializing() {
return !getApp().$vm.isInit; return !this.configStore.appInit;
}, },
showChats() { showChats() {
this.chatStore.chats.filter((chat) => !chat.delete && chat.showName && chat.showName.includes(this this.chatStore.chats.filter((chat) => !chat.delete && chat.showName && chat.showName.includes(this

18
im-uniapp/store/chatStore.js

@ -3,6 +3,7 @@ import { MESSAGE_TYPE, MESSAGE_STATUS } from '@/common/enums.js';
import useFriendStore from './friendStore.js'; import useFriendStore from './friendStore.js';
import useGroupStore from './groupStore.js'; import useGroupStore from './groupStore.js';
import useUserStore from './userStore'; import useUserStore from './userStore';
import useConfigStore from './configStore.js';
let cacheChats = []; let cacheChats = [];
export default defineStore('chatStore', { export default defineStore('chatStore', {
@ -155,12 +156,15 @@ export default defineStore('chatStore', {
insertMessage(msgInfo, chatInfo) { insertMessage(msgInfo, chatInfo) {
// 获取对方id或群id // 获取对方id或群id
let type = chatInfo.type; let type = chatInfo.type;
// 记录消息的最大id // 完成初始化之前不能修改消息最大id,否则可能导致拉不到离线消息
if (msgInfo.id && type == "PRIVATE" && msgInfo.id > this.privateMsgMaxId) { if (useConfigStore().appInit) {
this.privateMsgMaxId = msgInfo.id; // 记录消息的最大id
} if (msgInfo.id && type == "PRIVATE" && msgInfo.id > this.privateMsgMaxId) {
if (msgInfo.id && type == "GROUP" && msgInfo.id > this.groupMsgMaxId) { this.privateMsgMaxId = msgInfo.id;
this.groupMsgMaxId = msgInfo.id; }
if (msgInfo.id && type == "GROUP" && msgInfo.id > this.groupMsgMaxId) {
this.groupMsgMaxId = msgInfo.id;
}
} }
// 如果是已存在消息,则覆盖旧的消息数据 // 如果是已存在消息,则覆盖旧的消息数据
let chat = this.findChat(chatInfo); let chat = this.findChat(chatInfo);
@ -264,7 +268,7 @@ export default defineStore('chatStore', {
} }
if (delIdx >= 0) { if (delIdx >= 0) {
chat.messages.splice(delIdx, 1); chat.messages.splice(delIdx, 1);
if( delIdx < chat.hotMinIdx){ if (delIdx < chat.hotMinIdx) {
isColdMessage = true; isColdMessage = true;
chat.hotMinIdx--; chat.hotMinIdx--;
} }

4
im-uniapp/store/configStore.js

@ -4,6 +4,7 @@ import http from '../common/request'
export default defineStore('configStore', { export default defineStore('configStore', {
state: () => { state: () => {
return { return {
appInit: false,
webrtc: {} webrtc: {}
} }
}, },
@ -11,6 +12,9 @@ export default defineStore('configStore', {
setConfig(config) { setConfig(config) {
this.webrtc = config.webrtc; this.webrtc = config.webrtc;
}, },
setAppInit(appInit) {
this.appInit = appInit;
},
clear() { clear() {
this.webrtc = {}; this.webrtc = {};
}, },

16
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 useFriendStore from './friendStore.js';
import useGroupStore from './groupStore.js'; import useGroupStore from './groupStore.js';
import useUserStore from './userStore.js'; import useUserStore from './userStore.js';
import useConfigStore from './configStore.js';
import localForage from 'localforage'; import localForage from 'localforage';
/** /**
@ -155,12 +156,15 @@ export default defineStore('chatStore', {
}, },
insertMessage(msgInfo, chatInfo) { insertMessage(msgInfo, chatInfo) {
let type = chatInfo.type; let type = chatInfo.type;
// 记录消息的最大id // 完成初始化之前不能修改消息最大id,否则可能导致拉不到离线消息
if (msgInfo.id && type == "PRIVATE" && msgInfo.id > this.privateMsgMaxId) { if (useConfigStore().appInit) {
this.privateMsgMaxId = msgInfo.id; // 记录消息的最大id
} if (msgInfo.id && type == "PRIVATE" && msgInfo.id > this.privateMsgMaxId) {
if (msgInfo.id && type == "GROUP" && msgInfo.id > this.groupMsgMaxId) { this.privateMsgMaxId = msgInfo.id;
this.groupMsgMaxId = msgInfo.id; }
if (msgInfo.id && type == "GROUP" && msgInfo.id > this.groupMsgMaxId) {
this.groupMsgMaxId = msgInfo.id;
}
} }
// 如果是已存在消息,则覆盖旧的消息数据 // 如果是已存在消息,则覆盖旧的消息数据
let chat = this.findChat(chatInfo); let chat = this.findChat(chatInfo);

4
im-web/src/store/configStore.js

@ -4,6 +4,7 @@ import http from '../api/httpRequest.js'
export default defineStore('configStore', { export default defineStore('configStore', {
state: () => { state: () => {
return { return {
appInit: false, // 应用是否完成初始化
webrtc: {} webrtc: {}
} }
}, },
@ -11,6 +12,9 @@ export default defineStore('configStore', {
setConfig(config) { setConfig(config) {
this.webrtc = config.webrtc; this.webrtc = config.webrtc;
}, },
setAppInit(appInit) {
this.appInit = appInit;
},
loadConfig() { loadConfig() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
http({ http({

5
im-web/src/view/Home.vue

@ -99,6 +99,7 @@ export default {
// //
this.$refs.fullImage.open(url); this.$refs.fullImage.open(url);
}); });
this.configStore.setAppInit(false)
this.loadStore().then(() => { this.loadStore().then(() => {
// ws // ws
this.$wsApi.connect(process.env.VUE_APP_WS_URL, sessionStorage.getItem("accessToken")); this.$wsApi.connect(process.env.VUE_APP_WS_URL, sessionStorage.getItem("accessToken"));
@ -109,6 +110,7 @@ export default {
// 线 // 线
this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId); this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId);
this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId); this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId);
this.configStore.setAppInit(true);
} }
}); });
this.$wsApi.onMessage((cmd, msgInfo) => { this.$wsApi.onMessage((cmd, msgInfo) => {
@ -138,6 +140,7 @@ export default {
if (e.code != 3000) { if (e.code != 3000) {
// 线 // 线
this.reconnectWs(); this.reconnectWs();
this.configStore.setAppInit(false)
} }
}); });
}).catch((e) => { }).catch((e) => {
@ -169,6 +172,7 @@ export default {
// 线 // 线
this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId); this.pullPrivateOfflineMessage(this.chatStore.privateMsgMaxId);
this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId); this.pullGroupOfflineMessage(this.chatStore.groupMsgMaxId);
this.configStore.setAppInit(true)
this.$message.success("重新连接成功"); this.$message.success("重新连接成功");
}).catch(() => { }).catch(() => {
this.$message.error("初始化失败"); this.$message.error("初始化失败");
@ -387,6 +391,7 @@ export default {
}, },
onExit() { onExit() {
this.unloadStore(); this.unloadStore();
this.configStore.setAppInit(false);
this.$wsApi.close(3000); this.$wsApi.close(3000);
sessionStorage.removeItem("accessToken"); sessionStorage.removeItem("accessToken");
location.href = "/"; location.href = "/";

Loading…
Cancel
Save