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 {
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();

2
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

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 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--;
}

4
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 = {};
},

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 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);

4
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({

5
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 = "/";

Loading…
Cancel
Save