From 59dd5c96e8bca3341d1166a9a350f0731436817b Mon Sep 17 00:00:00 2001 From: qx <1084500556@qq.com> Date: 星期四, 24 七月 2025 10:27:41 +0800 Subject: [PATCH] Merge branch 'master' of http://101.42.27.146:5001/r/ltkj_peisweb --- src/main.js | 146 +++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 136 insertions(+), 10 deletions(-) diff --git a/src/main.js b/src/main.js index 9ebec6f..37508ae 100644 --- a/src/main.js +++ b/src/main.js @@ -17,7 +17,7 @@ import "./assets/icons"; import "./permission"; import { getDicts } from "@/api/system/dict/data"; -import { getConfigKey } from "@/api/system/config"; +import { getConfigKey, yidu } from "@/api/system/config"; import { parseTime, resetForm, @@ -36,7 +36,10 @@ import DictData from "@/components/DictData"; import * as echarts from "echarts"; import VueBarcode from "vue-barcode"; +import { initWebSocket, closeWebSocket } from "@/utils/websocket"; +import RightToolbar from "@/components/RightToolbar" +// 娉ㄥ唽鍏ㄥ眬缁勪欢 Vue.component("downloadExcel", JsonExcel); Vue.component("barcode", VueBarcode); Vue.component("DictTag", DictTag); @@ -45,7 +48,9 @@ Vue.component("FileUpload", FileUpload); Vue.component("ImageUpload", ImageUpload); Vue.component("ImagePreview", ImagePreview); +Vue.component("RightToolbar", RightToolbar); +// 娉ㄥ唽鍏ㄥ眬鏂规硶 Vue.prototype.getDicts = getDicts; Vue.prototype.getConfigKey = getConfigKey; Vue.prototype.parseTime = parseTime; @@ -57,20 +62,78 @@ Vue.prototype.handleTree = handleTree; Vue.prototype.$echarts = echarts; -// 淇濈暀 $showNotification锛屾敮鎸佹墜鍔ㄨЕ鍙戦�氱煡 -Vue.prototype.$showNotification = function (type, title, message, onClick) { - console.log('瑙﹀彂閫氱煡:', { type, title, message }, new Date().toLocaleString()); - Vue.prototype.$notify({ - title, - message, +// 閫氱煡绠$悊锛氳窡韪綋鍓嶉�氱煡鍜屽亸绉婚噺 +const notificationManager = { + notifications: [], // 瀛樺偍褰撳墠鏄剧ず鐨勯�氱煡瀹炰緥 + baseOffset: 50, // 鍩虹鍋忕Щ閲� + notificationHeight: 80, // 姣忎釜閫氱煡鐨勪及璁¢珮搴︼紙鍖呮嫭闂磋窛锛� + maxNotifications: 5, // 鏈�澶у悓鏃舵樉绀虹殑閫氱煡鏁伴噺 + addNotification(notification) { + if (this.notifications.length >= this.maxNotifications) { + // 鍏抽棴鏈�鏃╃殑閫氱煡 + const oldest = this.notifications.shift(); + oldest.close(); + } + this.notifications.push(notification); + // 璁剧疆鍔ㄦ�� offset 鍜� z-index + notification.offset = this.baseOffset + this.notifications.length * this.notificationHeight; + notification.customClass += ` notification-${this.notifications.length}`; // 涓� z-index 娣诲姞鍞竴绫� + // 鐩戝惉閫氱煡鍏抽棴 + notification.onClose = () => { + const index = this.notifications.indexOf(notification); + if (index > -1) { + this.notifications.splice(index, 1); + // 鏇存柊鍚庣画閫氱煡鐨� offset + this.updateOffsets(); + } + }; + }, + updateOffsets() { + this.notifications.forEach((notification, index) => { + notification.offset = this.baseOffset + (index + 1) * this.notificationHeight; + notification.customClass = notification.customClass.replace(/notification-\d+/, `notification-${index + 1}`); + }); + } +}; + +// 鍏ㄥ眬閫氱煡鏂规硶锛屾坊鍔犫�滃凡璇烩�濇寜閽� +Vue.prototype.$showNotification = function (type, title, message, onClick, noticeId) { + console.log('瑙﹀彂閫氱煡:', { type, title, message, noticeId, noticeIdType: typeof noticeId }); // 璋冭瘯锛氳褰� noticeId 鍜岀被鍨� + const h = this.$createElement; + const notification = this.$notify({ + title: title, + message: h('div', { class: 'notification-content' }, [ + h('div', { class: 'notification-message' }, message), + noticeId ? h('el-button', { + class: 'read-button', + style: { marginLeft: '10px', float: 'right', cursor: 'pointer' }, + props: { type: 'primary', size: 'mini' }, + on: { + click: async () => { + console.log('鐐瑰嚮鈥滃凡璇烩�濇寜閽紝noticeId:', noticeId, 'type:', typeof noticeId); // 璋冭瘯锛氳褰曠偣鍑绘椂鐨� noticeId + try { + await yidu({ noticeId: String(noticeId) }); + console.log(`閫氱煡 ${noticeId} 宸叉爣璁颁负宸茶`); + this.$message.success('鏍囪涓哄凡璇绘垚鍔�'); + notification.close(); + } catch (error) { + console.error('鏍囪宸茶澶辫触:', error, 'noticeId:', noticeId); + this.$message.error('鏍囪宸茶澶辫触'); + } + } + } + }, '宸茶') : null + ]), type, duration: 5000, position: 'top-right', - offset: 50, - onClick, + offset: notificationManager.baseOffset, // 鍒濆 offset + onClick: null, // 涓嶈烦杞� customClass: 'global-notification', + dangerouslyUseHTMLString: false, appendTo: document.body }); + notificationManager.addNotification(notification); // 娣诲姞鍒伴�氱煡绠$悊 }; // 鐩戝惉璺敱鍙樺寲 @@ -78,13 +141,76 @@ console.log('璺敱鍒囨崲瀹屾垚锛屽綋鍓嶈矾寰�:', router.currentRoute.path); }); +// 瀹氫箟 WebSocket 鍒濆鍖栨爣蹇楋紝闃叉閲嶅杩炴帴 +let isWebSocketInitialized = false; + const app = new Vue({ el: "#app", router, store, - render: (h) => h(App) + render: (h) => h(App), + mounted() { + const token = store.state.user.token || Cookies.get('token') || ''; + if (token && !isWebSocketInitialized) { + console.log('鍒濆鍖� WebSocket锛孴oken:', 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 瑙f瀽鍚庢秷鎭�:', 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('鍑嗗瑙﹀彂閫氱煡锛宯oticeId:', 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('娑堟伅瑙f瀽澶辫触:', error, '鍘熷鏁版嵁:', data); + Vue.prototype.$showNotification.call(this, 'error', '娑堟伅瑙f瀽澶辫触', `鏈嶅姟鍣ㄥ洖搴斿瓧绗︿覆: ${data}`); + } + }); + } else if (!token) { + console.error('鏈壘鍒� token锛屾棤娉曞垵濮嬪寲 WebSocket'); + } else { + console.log('WebSocket 宸插垵濮嬪寲锛岃烦杩囬噸澶嶈繛鎺�'); + } + }, + beforeDestroy() { + if (isWebSocketInitialized) { + closeWebSocket(); + isWebSocketInitialized = false; + console.log('Vue 瀹炰緥閿�姣侊紝WebSocket 宸叉竻鐞�'); + } + } }); +// 娉ㄥ唽鎻掍欢 Vue.use(directive); Vue.use(plugins); Vue.use(VueMeta); -- Gitblit v1.8.0