diff --git a/App.vue b/App.vue index 19fd7d9..db0586e 100644 --- a/App.vue +++ b/App.vue @@ -3,6 +3,7 @@ getVersion } from './request/api/api' import { webSocketManager } from '@/utils/ws/websocket.js'; + import { ChatManager } from '@/store/chat/chat.js'; import {checkDev} from "@/utils/utils"; import {bindUserApi} from "@/request/api/salesperson"; export default { @@ -151,7 +152,76 @@ methods: { initWebSocket() { webSocketManager.connect(); + + // 注册全局消息处理器 + webSocketManager.addMessageHandler((data) => { + // 只处理聊天消息(包含 room_id 的消息) + if (data.room_id) { + // 获取当前页面路径 + const pages = getCurrentPages(); + const currentPage = pages[pages.length - 1]; + const currentPath = currentPage ? currentPage.route : ''; + + // 如果用户在聊天页面,不调用 ChatManager(chat.vue 会自己处理) + // 避免消息重复推送 + if (currentPath && currentPath.includes('chat/chat')) { + return; + } + + ChatManager.handleIncomingMessage(data); + } + }); + + // 监听全局通知事件 + uni.$on('show-global-notification', this.handleShowNotification); + }, + + /** + * 处理显示通知 + * @param {Object} message 消息对象 + */ + handleShowNotification(message) { + // 获取当前页面路径 + const pages = getCurrentPages(); + const currentPage = pages[pages.length - 1]; + const currentPath = currentPage ? currentPage.route : ''; + + // 如果在聊天页面,不显示顶部通知 + if (currentPath && currentPath.includes('chat/chat')) { + return; + } + + // 获取消息预览文本 + const messagePreview = ChatManager.getMessagePreview(message); + + // 从消息中提取发送者信息 + let senderName = '新消息'; + let senderAvatar = '/static/xx/ysxx.png'; + + // 尝试解析发送者ID获取更多信息 + const senderId = message.sender_user_id || ''; + if (senderId.startsWith('doctor-')) { + senderName = '医生消息'; + } + + // 触发通知组件显示 + uni.$emit('show-message-notification', { + avatar: senderAvatar, + title: senderName, + message: messagePreview, + roomId: message.room_id, + roomData: { + nick_name: senderName, + user_id: senderId, + avatar: senderAvatar + } + }); } + }, + + onUnload() { + // 移除全局通知事件监听 + uni.$off('show-global-notification', this.handleShowNotification); } } diff --git a/components/MessageNotification/MessageNotification.vue b/components/MessageNotification/MessageNotification.vue new file mode 100644 index 0000000..6850026 --- /dev/null +++ b/components/MessageNotification/MessageNotification.vue @@ -0,0 +1,236 @@ + + + + + + diff --git a/pages.json b/pages.json index a211a95..52fa7b5 100644 --- a/pages.json +++ b/pages.json @@ -3,7 +3,9 @@ // 下载安装方式 // "^u-(.*)": "@/uview-ui/components/u-$1/u-$1.vue" // npm安装方式 - "^u-(.*)": "uview-ui/components/u-$1/u-$1.vue" + "^u-(.*)": "uview-ui/components/u-$1/u-$1.vue", + // 消息通知组件 + "^MessageNotification": "@/components/MessageNotification/MessageNotification.vue" }, "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages diff --git a/pages/Inquiries/Inquiries.vue b/pages/Inquiries/Inquiries.vue index 1ff888b..2846338 100644 --- a/pages/Inquiries/Inquiries.vue +++ b/pages/Inquiries/Inquiries.vue @@ -1,5 +1,6 @@