diff --git a/src/components/NotificationCenter/index.vue b/src/components/NotificationCenter/index.vue index 8bf24ad..948be76 100644 --- a/src/components/NotificationCenter/index.vue +++ b/src/components/NotificationCenter/index.vue @@ -469,18 +469,151 @@ const exportNotifications = () => { // 监听WebSocket事件 const setupWebSocketListeners = () => { + console.log('设置WebSocket监听器') + + // 监听新消息 websocketService.on('message', (message) => { - // 新消息到达时的处理逻辑 console.log('收到WebSocket消息:', message) + + // 如果消息包含通知信息,添加到通知服务 + if (message.data && message.data.notification) { + console.log('处理通知消息:', message.data.notification) + notificationService.addNotification({ + type: message.data.notification.type || 'SYSTEM', + title: message.data.notification.title || '新通知', + content: message.data.notification.content || '', + priority: message.data.notification.priority || 'NORMAL', + category: message.data.notification.category || '系统', + targetUrl: message.data.notification.targetUrl, + metadata: message.data.notification.metadata, + source: message.data.notification.source || 'WEBSOCKET' + }) + } }) + // 监听审批状态变更 websocketService.on('approvalStatusChanged', (data) => { console.log('审批状态变更:', data) + + // 添加审批状态变更通知 + if (data.type === 'SUBMITTED') { + notificationService.addNotification({ + type: 'PENDING', + title: '新的审批申请', + content: `收到来自 ${data.applicantName || '申请人'} 的${data.businessType || '设备'}申请:${data.equipmentName || '未知设备'}`, + targetUrl: '/asset-management/device-management/approval', + priority: 'HIGH', + category: '审批申请', + actionRequired: true, + source: 'APPROVAL_SYSTEM', + metadata: { + approvalId: data.approvalId, + equipmentName: data.equipmentName, + applicantName: data.applicantName, + businessType: data.businessType, + timestamp: Date.now() + } + }) + } }) + // 监听设备状态变更 websocketService.on('equipmentStatusChanged', (data) => { console.log('设备状态变更:', data) + + // 添加设备状态变更通知 + notificationService.addNotification({ + type: 'EQUIPMENT_ALERT', + title: '设备状态更新', + content: `设备"${data.equipmentName || '未知设备'}"状态已更新为:${data.newStatus}`, + targetUrl: '/asset-management/device-management/device-center', + priority: 'NORMAL', + category: '设备状态', + source: 'EQUIPMENT_SYSTEM', + metadata: { + equipmentId: data.equipmentId, + equipmentName: data.equipmentName, + oldStatus: data.oldStatus, + newStatus: data.newStatus, + timestamp: Date.now() + } + }) }) + + // 监听采购状态变更 + websocketService.on('procurementStatusChanged', (data) => { + console.log('采购状态变更:', data) + + if (data.type === 'SUBMITTED') { + notificationService.addNotification({ + type: 'PROCUREMENT', + title: '新的采购申请', + content: `收到来自 ${data.applicantName || '申请人'} 的设备采购申请:${data.equipmentName || '未知设备'}`, + targetUrl: '/asset-management/device-management/approval', + priority: 'HIGH', + category: '设备采购', + actionRequired: true, + source: 'PROCUREMENT_SYSTEM', + metadata: { + procurementId: data.procurementId, + equipmentName: data.equipmentName, + applicantName: data.applicantName, + timestamp: Date.now() + } + }) + } + }) + + // 监听新审批申请 + websocketService.on('newApprovalRequest', (data) => { + console.log('新审批申请:', data) + + notificationService.addNotification({ + type: 'PENDING', + title: '新的审批申请', + content: `收到来自 ${data.applicantName || '申请人'} 的${data.businessType || '设备'}申请:${data.equipmentName || '未知设备'}`, + targetUrl: '/asset-management/device-management/approval', + priority: 'HIGH', + category: '审批申请', + actionRequired: true, + source: 'APPROVAL_SYSTEM', + metadata: { + approvalId: data.approvalId, + equipmentName: data.equipmentName, + applicantName: data.applicantName, + businessType: data.businessType, + timestamp: Date.now() + } + }) + }) + + // 监听WebSocket连接状态 + websocketService.on('connected', () => { + console.log('WebSocket已连接') + }) + + websocketService.on('disconnected', (data) => { + console.log('WebSocket已断开:', data) + }) + + websocketService.on('error', (error) => { + console.error('WebSocket错误:', error) + }) +} + +// 清理WebSocket监听器 +const cleanupWebSocketListeners = () => { + console.log('清理WebSocket监听器') + + // 移除所有事件监听器 + websocketService.off('message', () => {}) + websocketService.off('approvalStatusChanged', () => {}) + websocketService.off('equipmentStatusChanged', () => {}) + websocketService.off('procurementStatusChanged', () => {}) + websocketService.off('newApprovalRequest', () => {}) + websocketService.off('connected', () => {}) + websocketService.off('disconnected', () => {}) + websocketService.off('error', () => {}) } // 定期检查提醒 @@ -506,6 +639,7 @@ onMounted(() => { }) onUnmounted(() => { + cleanupWebSocketListeners() if (reminderCheckInterval) { clearInterval(reminderCheckInterval) } diff --git a/src/layout/components/HeaderRightBar/index.vue b/src/layout/components/HeaderRightBar/index.vue index ab8b2f9..3a7bcd7 100644 --- a/src/layout/components/HeaderRightBar/index.vue +++ b/src/layout/components/HeaderRightBar/index.vue @@ -60,10 +60,12 @@