|
@@ -1,19 +1,15 @@
|
|
|
// src/stores/websocket.js
|
|
|
import { defineStore } from "pinia";
|
|
|
import { $root as protobuf } from "@/common/proto/proto";
|
|
|
-import * as Constant from "@/common/constant/Constant";
|
|
|
import { useWalletStore } from "@/stores/modules/walletStore";
|
|
|
import { getMessageApi, friendRequest, groupList } from "@/api/path/im.api";
|
|
|
import { showDialog } from 'vant';
|
|
|
-import { eventBus } from '@/utils/utils';
|
|
|
import { useSystemStore } from "@/stores/modules/systemStore";
|
|
|
import * as MsgType from "@/common/constant/msgType";
|
|
|
import {
|
|
|
setMessageHook,
|
|
|
handleMessageHook,
|
|
|
} from "@/views/im/hook/messagesHook";
|
|
|
-import { add, update } from "lodash";
|
|
|
-
|
|
|
|
|
|
// 配置常量
|
|
|
const WS_CONFIG = {
|
|
@@ -105,6 +101,24 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
},
|
|
|
|
|
|
actions: {
|
|
|
+ // 监听前后台切换
|
|
|
+ changeAppState(state) {
|
|
|
+ if(state){
|
|
|
+ // 前台
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // 后台
|
|
|
+ }
|
|
|
+ // 检测是否断开连接
|
|
|
+ if(!this.isConnected){
|
|
|
+ console.warn("断开重新连接中...");
|
|
|
+ this.disconnect();
|
|
|
+ this.connect(this.uuid);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ // 监听网络变化
|
|
|
+ changeNetworkState(state) {
|
|
|
+ },
|
|
|
// 初始化连接
|
|
|
connect(userUuid, callback) {
|
|
|
if (!userUuid) {
|
|
@@ -228,14 +242,18 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
this.heartbeat.retryCount++;
|
|
|
|
|
|
if (
|
|
|
- this.heartbeat.retryCount >= WS_CONFIG.HEARTBEAT_RETRY_LIMIT
|
|
|
+ this.heartbeat.retryCount > WS_CONFIG.HEARTBEAT_RETRY_LIMIT
|
|
|
) {
|
|
|
console.error("心跳响应超时,关闭连接");
|
|
|
this.disconnect();
|
|
|
} else {
|
|
|
- console.warn(
|
|
|
- `心跳无响应,第${this.heartbeat.retryCount}次重试`
|
|
|
- );
|
|
|
+ if (this.heartbeat.retryCount > 1) {
|
|
|
+ console.warn(
|
|
|
+ `心跳无响应,第${this.heartbeat.retryCount - 1}次重试`
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ console.log("%c心跳响应正常,重新发送心跳", 'color: green;');
|
|
|
+ }
|
|
|
this.startHeartbeat(); // 重新发送心跳
|
|
|
}
|
|
|
}, WS_CONFIG.HEARTBEAT_TIMEOUT);
|
|
@@ -475,13 +493,25 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
messageList[sessionId] = [];
|
|
|
}
|
|
|
messageList[sessionId] = messageList[sessionId].map(item => {
|
|
|
- if (item.id + '' === msgId + '') return {...item, ...message, content: "", url: ""};
|
|
|
+ if(typeof msgId === 'function'){
|
|
|
+ let msg = {};
|
|
|
+ if(msg = msgId(item, sessionId)){
|
|
|
+ return {...item, ...message, ...msg};
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (item.id + '' === msgId + '') return {...item, ...message, content: "", url: ""};
|
|
|
return item;
|
|
|
});
|
|
|
// 检测当前消息
|
|
|
if (this.toUserInfo.uuid === sessionId) {
|
|
|
this.messages = this.messages.map(item => {
|
|
|
- if (item.id + '' === msgId + '') return {...item, ...message, content: "", url: ""};
|
|
|
+ if(typeof msgId === 'function'){
|
|
|
+ let msg = {};
|
|
|
+ if(msg = msgId(item, sessionId)){
|
|
|
+ return {...item, ...message, ...msg};
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (item.id + '' === msgId + '') return {...item, ...message, content: "", url: ""};
|
|
|
return item;
|
|
|
});
|
|
|
}
|
|
@@ -548,12 +578,15 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
// this.messages = messageList[this.sessionId];
|
|
|
// return;
|
|
|
// }
|
|
|
- this.sessionId = this.toUserInfo.uuid;
|
|
|
- this.messages = this.getSessionMessages(this.sessionId);
|
|
|
+
|
|
|
+ // this.sessionId = this.toUserInfo.uuid;
|
|
|
+ // this.messages = this.getSessionMessages(this.sessionId);
|
|
|
+ this.loading = true // 开始加载
|
|
|
+ this.changeSessionMessage(this.toUserInfo.uuid);
|
|
|
if (this.messages.length > 0) {
|
|
|
+ this.loading = false;
|
|
|
return;
|
|
|
}
|
|
|
- this.loading = true // 开始加载
|
|
|
try {
|
|
|
const res = await getMessageApi({
|
|
|
messageType: params.messageType,
|
|
@@ -624,8 +657,9 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
lastMsg = '[阅后即焚]';
|
|
|
}
|
|
|
// 额外数据
|
|
|
- const ext = {};
|
|
|
-
|
|
|
+ const ext = {
|
|
|
+ updatedAt: time,
|
|
|
+ };
|
|
|
// 修改群公告信息
|
|
|
if (message.nickname) {
|
|
|
ext.sessionName = message.nickname
|
|
@@ -639,35 +673,23 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
if (message.stick || message.stick === 0) {
|
|
|
ext.stick = message.stick
|
|
|
}
|
|
|
+ // 补充消息完整性
|
|
|
+
|
|
|
// 更新并排序
|
|
|
if(index >= 0){
|
|
|
systemStore.ImsessionList = [...systemStore.ImsessionList].map((item)=>{
|
|
|
if (item.uuid == sessionId){
|
|
|
// console.log('111', item, lastMsg, lastMsg,item.message)
|
|
|
- return { ...item, ...ext, unReadNum: item.unReadNum + 1, message: lastMsg || item.message, updatedAt: time};
|
|
|
+ return { ...item, ...ext, unReadNum: item.unReadNum + 1, message: lastMsg || item.message};
|
|
|
}
|
|
|
return item;
|
|
|
}).sort((a, b) => b.stick - a.stick || new Date(b.updatedAt) - new Date(a.updatedAt));
|
|
|
// 软刷新会话列表
|
|
|
this.needRefreshIm += 1;
|
|
|
- } else {
|
|
|
- // 创建临时会话记录
|
|
|
- // systemStore.ImsessionList.push({
|
|
|
- // uuid: sessionId,
|
|
|
- // name: message.toUsername || message.fromUsername || message.to || message.from,
|
|
|
- // nickname: message.toUsername || message.fromUsername || message.to || message.from,
|
|
|
- // type: message.messageType == 2?'group':'user',
|
|
|
- // message: lastMsg,
|
|
|
- // updatedAt: time,
|
|
|
- // avatar: message.avatar || '',
|
|
|
- // avatars: [],
|
|
|
- // unReadNum,
|
|
|
- // slience: 0,
|
|
|
- // stick: 0,
|
|
|
- // });
|
|
|
- // 强制刷新会话列表
|
|
|
- this.chatRefresh += 1;
|
|
|
+ return;
|
|
|
}
|
|
|
+ // 强制刷新会话列表
|
|
|
+ this.chatRefresh += 1;
|
|
|
},
|
|
|
|
|
|
// 更新缓存里面的会话列表 type:0 发送者 1:接收者
|
|
@@ -784,6 +806,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
// 禁用会话功能
|
|
|
this.funDeleteDelAuth(message.from);
|
|
|
// 需要删除会话
|
|
|
+
|
|
|
// 删除本地消息记录
|
|
|
this.deleteMessage(message, true);
|
|
|
}
|
|
@@ -803,25 +826,27 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
}
|
|
|
// 创建群/加入群/邀请入群
|
|
|
if (message.messageType == MsgType.CREATE_GROUP || message.messageType == MsgType.JION_GROUP || message.messageType == MsgType.INVITATION_GROUP) {
|
|
|
- // 刷新会话列表
|
|
|
- // this.needRefreshIm += 1;
|
|
|
+ console.log('群系统消息', message, msg);
|
|
|
if (message.messageType == MsgType.JION_GROUP || message.messageType == MsgType.INVITATION_GROUP) {
|
|
|
// 恢复会话功能
|
|
|
this.funRestoreDelAuth(message.from);
|
|
|
}
|
|
|
+ // 刷新会话列表
|
|
|
+ // this.needRefreshIm += 1;
|
|
|
}
|
|
|
// 群通知
|
|
|
if (message.messageType == MsgType.MESSAGE_NOTICE_GROUP) {
|
|
|
+ // 更新群名称和群公告
|
|
|
this.funGroupNotice(msg);
|
|
|
-
|
|
|
}
|
|
|
// 处理群置顶
|
|
|
if (message.messageType === MsgType.MESSAGE_STICKY_GROUP) {
|
|
|
}
|
|
|
- if (message.messageType != MsgType.DELETE_GROUP){
|
|
|
- // 更新群成员列表
|
|
|
- this.fetchGroupMembers(true);
|
|
|
- }
|
|
|
+ // 更新群成员列表
|
|
|
+ this.fetchGroupMembers(true);
|
|
|
+ // if (message.messageType != MsgType.DELETE_GROUP){
|
|
|
+ // this.fetchGroupMembers(true);
|
|
|
+ // }
|
|
|
// 处理@群成员
|
|
|
if (message.messageType == MsgType.MESSAGE_CC_GROUP){
|
|
|
let ccArr = msg?.cc.split(',');
|
|
@@ -832,12 +857,15 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
},
|
|
|
|
|
|
// 语音视频通知
|
|
|
- // 群通知
|
|
|
+ // 群通知更新群名称和公告
|
|
|
funGroupNotice(msg){
|
|
|
this.toUserInfo = {
|
|
|
...this.toUserInfo,
|
|
|
+ // 群公告
|
|
|
notice: msg && msg.notice?msg.notice: this.toUserInfo.notice,
|
|
|
nickname: msg && msg.name?msg.name:this.toUserInfo.nickname,
|
|
|
+ // 群名称
|
|
|
+ sessionName: msg && msg.name?msg.name:this.toUserInfo.sessionName,
|
|
|
}
|
|
|
},
|
|
|
// 群消息置顶
|