1. 医生端开方2.0测试
This commit is contained in:
173
store/chat/chat.js
Normal file
173
store/chat/chat.js
Normal file
@@ -0,0 +1,173 @@
|
||||
import { getChatRegisterInfoApi, getMessagesByRoomIdApi } from "@/request/api/im";
|
||||
|
||||
/**
|
||||
* 聊天消息管理器(简化版)
|
||||
*/
|
||||
const ChatManager = {
|
||||
currentRoomId: null,
|
||||
lastMessageId: 0,
|
||||
unreadCounts: {},
|
||||
messageCache: {},
|
||||
roomMessages: {},
|
||||
|
||||
enterRoom(roomId) {
|
||||
this.currentRoomId = roomId;
|
||||
uni.setStorageSync('currentRoomId', roomId);
|
||||
this.resetUnreadCount(roomId);
|
||||
},
|
||||
|
||||
leaveRoom() {
|
||||
this.currentRoomId = null;
|
||||
uni.removeStorageSync('currentRoomId');
|
||||
},
|
||||
|
||||
getCurrentRoomId() {
|
||||
return this.currentRoomId || uni.getStorageSync('currentRoomId');
|
||||
},
|
||||
|
||||
increaseUnreadCount(roomId) {
|
||||
if (this.getCurrentRoomId() !== roomId) {
|
||||
this.unreadCounts[roomId] = (this.unreadCounts[roomId] || 0) + 1;
|
||||
this.notifyUnreadChange();
|
||||
}
|
||||
},
|
||||
|
||||
resetUnreadCount(roomId) {
|
||||
if (this.unreadCounts[roomId]) {
|
||||
this.unreadCounts[roomId] = 0;
|
||||
this.notifyUnreadChange();
|
||||
}
|
||||
},
|
||||
|
||||
getUnreadCount(roomId) {
|
||||
return this.unreadCounts[roomId] || 0;
|
||||
},
|
||||
|
||||
getAllUnreadCount() {
|
||||
return Object.values(this.unreadCounts).reduce((sum, count) => sum + count, 0);
|
||||
},
|
||||
|
||||
notifyUnreadChange() {
|
||||
uni.$emit('unread-message-update', {
|
||||
total: this.getAllUnreadCount(),
|
||||
byRoom: { ...this.unreadCounts }
|
||||
});
|
||||
},
|
||||
|
||||
addMessageToRoom(roomId, message) {
|
||||
if (!this.roomMessages[roomId]) {
|
||||
this.roomMessages[roomId] = [];
|
||||
}
|
||||
this.roomMessages[roomId].push(message);
|
||||
},
|
||||
|
||||
getRoomMessages(roomId) {
|
||||
let that = this;
|
||||
this.roomMessages[roomId] = [];
|
||||
getMessagesByRoomIdApi({
|
||||
room_id: roomId,
|
||||
last_message_id: 0,
|
||||
}).then((res) => {
|
||||
if (res && res.data && res.data.result) {
|
||||
that.roomMessages[roomId] = that.checkMessage(res.data.result.list || []);
|
||||
that.lastMessageId = res.data.result.last_message_id || 0;
|
||||
uni.$emit('load-room-message', 1);
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error('获取消息失败:', err);
|
||||
});
|
||||
return this.roomMessages[roomId] || [];
|
||||
},
|
||||
|
||||
checkMessage(message, type = 0) {
|
||||
if (type === 1) {
|
||||
return {
|
||||
id: message.id,
|
||||
sender_user_id: message.sender_user_id,
|
||||
receiver_user_id: message.receiver_user_id,
|
||||
message_type: message.message_type,
|
||||
message_content: message.message_content,
|
||||
duration: message.duration,
|
||||
room_id: message.room_id,
|
||||
created_at: message.created_at || message.send_time,
|
||||
created_at_text: message.created_at_text || this.formatTime(message.send_time || message.created_at),
|
||||
timestamp: message.send_time || message.created_at
|
||||
};
|
||||
}
|
||||
return message.map((item) => {
|
||||
const result = {
|
||||
id: item.id,
|
||||
sender_user_id: item.sender_user_id,
|
||||
receiver_user_id: item.receiver_user_id,
|
||||
message_type: item.message_type,
|
||||
message_content: item.message_content,
|
||||
duration: item.duration,
|
||||
room_id: item.room_id,
|
||||
created_at: item.created_at || item.send_time,
|
||||
created_at_text: item.created_at_text || this.formatTime(item.send_time || item.created_at),
|
||||
timestamp: item.send_time || item.created_at
|
||||
};
|
||||
|
||||
if (item.message_type === 10) {
|
||||
this.getChatRegisterInfo(result, message.indexOf(item));
|
||||
}
|
||||
return result;
|
||||
});
|
||||
},
|
||||
|
||||
formatTime(timestamp) {
|
||||
if (!timestamp) return '';
|
||||
const date = new Date(timestamp * 1000);
|
||||
const hours = date.getHours().toString().padStart(2, '0');
|
||||
const minutes = date.getMinutes().toString().padStart(2, '0');
|
||||
return `${hours}:${minutes}`;
|
||||
},
|
||||
|
||||
getChatRegisterInfo(message, index) {
|
||||
const parsedContent = this.parseMessageContent(message.message_content);
|
||||
if (parsedContent._loaded || !parsedContent.id) return;
|
||||
|
||||
getChatRegisterInfoApi({ id: parsedContent.id, message_id: message.id || '' }).then((res) => {
|
||||
if (res && res.data && res.data.result) {
|
||||
const resultWithFlag = { ...res.data.result, _loaded: true };
|
||||
const roomId = message.room_id;
|
||||
if (this.roomMessages[roomId] && this.roomMessages[roomId][index]) {
|
||||
this.roomMessages[roomId][index].message_content = JSON.stringify(resultWithFlag);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
parseMessageContent(content) {
|
||||
try {
|
||||
return typeof content === 'object' ? content : JSON.parse(content);
|
||||
} catch (e) {
|
||||
return content;
|
||||
}
|
||||
},
|
||||
|
||||
loadHistory(roomId) {
|
||||
getMessagesByRoomIdApi({
|
||||
room_id: roomId,
|
||||
last_message_id: this.lastMessageId,
|
||||
}).then((res) => {
|
||||
if (res && res.data && res.data.result) {
|
||||
const newMessages = this.checkMessage(res.data.result.list || []);
|
||||
if (newMessages.length > 0) {
|
||||
this.roomMessages[roomId] = [...newMessages, ...(this.roomMessages[roomId] || [])];
|
||||
this.lastMessageId = res.data.result.last_message_id || 0;
|
||||
uni.$emit('load-room-message', 0);
|
||||
} else {
|
||||
uni.$emit('no-more-history');
|
||||
}
|
||||
} else {
|
||||
uni.$emit('no-more-history');
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error('加载历史消息失败:', err);
|
||||
uni.$emit('no-more-history');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export { ChatManager };
|
||||
@@ -1,7 +1,7 @@
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
import WebSocket from '@/common/common.js';
|
||||
import Utils from '@/common/utils.js';
|
||||
import { disconnectWebSocket } from '@/utils/ws/initWebSocket.js';
|
||||
|
||||
import {
|
||||
registerList
|
||||
@@ -9,8 +9,6 @@ import {
|
||||
Vue.use(Vuex)
|
||||
const store = new Vuex.Store({
|
||||
state: {
|
||||
// url: 'wss://wss.nbxlmy.com/ws',
|
||||
webSocket: null,
|
||||
utils: null,
|
||||
consult: [], //咨询会话列表
|
||||
docSession: [], //医生首页消息会话列表
|
||||
@@ -25,37 +23,37 @@ const store = new Vuex.Store({
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
/**
|
||||
* 登出
|
||||
* 职责:断开WebSocket连接,清理状态
|
||||
*/
|
||||
logout({
|
||||
state,
|
||||
dispatch
|
||||
}) {
|
||||
dispatch('closeWebSocket')
|
||||
// state.webSocket.logout()
|
||||
dispatch('closeWebSocket');
|
||||
},
|
||||
|
||||
/**
|
||||
* 初始化(保留用于其他初始化逻辑)
|
||||
* 注意:WebSocket初始化已在App.vue中统一处理
|
||||
*/
|
||||
init({
|
||||
state,
|
||||
dispatch
|
||||
}) {
|
||||
console.log('这里是websocket init!!!!')
|
||||
// 拿到存储
|
||||
let userInfo = uni.getStorageSync('userInfo')
|
||||
if (userInfo) {
|
||||
console.log(1);
|
||||
// 连接socket
|
||||
state.webSocket = new WebSocket({
|
||||
url: state.url
|
||||
})
|
||||
// state.utils = new Utils()
|
||||
// 获取会话列表
|
||||
// dispatch('getSessionList')
|
||||
}
|
||||
// WebSocket初始化已移至App.vue,这里保留其他初始化逻辑
|
||||
// state.utils = new Utils()
|
||||
// 获取会话列表
|
||||
// dispatch('getSessionList')
|
||||
},
|
||||
closeWebSocket({
|
||||
state
|
||||
}) {
|
||||
if (state.webSocket) {
|
||||
state.webSocket.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭WebSocket
|
||||
* 职责:断开WebSocket连接
|
||||
*/
|
||||
closeWebSocket() {
|
||||
disconnectWebSocket();
|
||||
},
|
||||
// 获取挂号信息
|
||||
getConsult({
|
||||
|
||||
Reference in New Issue
Block a user