| | |
| | | router, |
| | | store, |
| | | render: (h) => h(App), |
| | | mounted() { |
| | | const token = store.state.user.token || Cookies.get('token') || ''; |
| | | if (token && !isWebSocketInitialized) { |
| | | console.log('初始化 WebSocket,Token:', token); |
| | | isWebSocketInitialized = true; |
| | | initWebSocket(token, (type, data) => { |
| | | console.log('WebSocket 收到消息:', { type, data }); // 调试:记录原始数据 |
| | | if (type === 'error') { |
| | | Vue.prototype.$showNotification.call(this, 'error', '错误', data); |
| | | return; |
| | | } |
| | | try { |
| | | if (typeof data === 'string' && data.trim().startsWith('{')) { |
| | | // 替换大整数字段,防止精度丢失 |
| | | const normalizedData = data.replace(/"(noticeId|notice_id|id)":\s*(\d+)/g, '"$1":"$2"'); |
| | | const message = JSON.parse(normalizedData); |
| | | console.log('WebSocket 解析后消息:', message); // 调试:记录解析后的消息 |
| | | if (message.noticeId || message.notice_id || message.id) { |
| | | const noticeTypeLabel = message.noticeType === '1' ? '通知' : '公告'; |
| | | const noticeTitle = message.noticeTitle ? message.noticeTitle.replace(/<[^>]+>/g, '') : '无标题'; |
| | | const contentPreview = message.noticeContent |
| | | ? message.noticeContent.replace(/<[^>]+>/g, '').substring(0, 20) + '...' |
| | | : '无内容'; |
| | | const noticeId = String(message.noticeId || message.notice_id || message.id); |
| | | console.log('准备触发通知,noticeId:', noticeId, 'type:', typeof noticeId); // 调试:记录传递的 noticeId |
| | | Vue.prototype.$showNotification.call( |
| | | this, |
| | | 'success', |
| | | `新${noticeTypeLabel}`, |
| | | `${noticeTitle} - ${contentPreview}`, |
| | | null, |
| | | noticeId |
| | | ); |
| | | } else { |
| | | console.log('未知消息类型:', message); |
| | | Vue.prototype.$showNotification.call(this, 'info', '消息', '收到未知格式的消息'); |
| | | mounted() { |
| | | const token = store.state.user.token || Cookies.get('token') || ''; |
| | | if (token) { |
| | | // 调用 getConfigKey 接口,检查 xxtz 配置 |
| | | getConfigKey('xxtz').then(res => { |
| | | console.log('getConfigKey 返回结果:', res); // 调试:记录接口响应 |
| | | if (res.msg === 'Y') { |
| | | // 消息通知功能开启 |
| | | console.log('消息通知功能已启用'); |
| | | if (!isWebSocketInitialized) { |
| | | console.log('初始化 WebSocket,Token:', token); |
| | | isWebSocketInitialized = true; |
| | | initWebSocket(token, (type, data) => { |
| | | console.log('WebSocket 收到消息:', { type, data }); // 调试:记录原始数据 |
| | | if (type === 'error') { |
| | | Vue.prototype.$showNotification.call(this, 'error', '错误', data); |
| | | return; |
| | | } |
| | | } else { |
| | | console.log('WebSocket 非 JSON 消息:', data); |
| | | |
| | | } |
| | | } catch (error) { |
| | | console.error('消息解析失败:', error, '原始数据:', data); |
| | | Vue.prototype.$showNotification.call(this, 'error', '消息解析失败', `服务器回应字符串: ${data}`); |
| | | try { |
| | | if (typeof data === 'string' && data.trim().startsWith('{')) { |
| | | // 替换大整数字段,防止精度丢失 |
| | | const normalizedData = data.replace(/"(noticeId|notice_id|id)":\s*(\d+)/g, '"$1":"$2"'); |
| | | const message = JSON.parse(normalizedData); |
| | | console.log('WebSocket 解析后消息:', message); // 调试:记录解析后的消息 |
| | | if (message.noticeId || message.notice_id || message.id) { |
| | | const noticeTypeLabel = message.noticeType === '1' ? '通知' : '公告'; |
| | | const noticeTitle = message.noticeTitle ? message.noticeTitle.replace(/<[^>]+>/g, '') : '无标题'; |
| | | const contentPreview = message.noticeContent |
| | | ? message.noticeContent.replace(/<[^>]+>/g, '').substring(0, 20) + '...' |
| | | : '无内容'; |
| | | const noticeId = String(message.noticeId || message.notice_id || message.id); |
| | | console.log('准备触发通知,noticeId:', noticeId, 'type:', typeof noticeId); // 调试:记录传递的 noticeId |
| | | Vue.prototype.$showNotification.call( |
| | | this, |
| | | 'success', |
| | | `新${noticeTypeLabel}`, |
| | | `${noticeTitle} - ${contentPreview}`, |
| | | null, |
| | | noticeId |
| | | ); |
| | | } else { |
| | | console.log('未知消息类型:', message); |
| | | Vue.prototype.$showNotification.call(this, 'info', '消息', '收到未知格式的消息'); |
| | | } |
| | | } else { |
| | | console.log('WebSocket 非 JSON 消息:', data); |
| | | } |
| | | } catch (error) { |
| | | console.error('消息解析失败:', error, '原始数据:', data); |
| | | Vue.prototype.$showNotification.call(this, 'error', '消息解析失败', `服务器回应字符串: ${data}`); |
| | | } |
| | | }); |
| | | } else { |
| | | console.log('WebSocket 已初始化,跳过重复连接'); |
| | | } |
| | | }); |
| | | } else if (!token) { |
| | | console.error('未找到 token,无法初始化 WebSocket'); |
| | | } else { |
| | | console.log('WebSocket 已初始化,跳过重复连接'); |
| | | } |
| | | }, |
| | | } else { |
| | | console.log('消息通知功能未启用 (xxtz: N)'); |
| | | } |
| | | }).catch(error => { |
| | | console.error('调用 getConfigKey 失败:', error); |
| | | // 可选:根据需求处理接口调用失败的情况 |
| | | Vue.prototype.$showNotification.call(this, 'error', '配置获取失败', '无法获取消息通知配置'); |
| | | }); |
| | | } else { |
| | | console.error('未找到 token,无法初始化 WebSocket'); |
| | | } |
| | | }, |
| | | beforeDestroy() { |
| | | if (isWebSocketInitialized) { |
| | | closeWebSocket(); |