From eaa953134588c4cffb0a8adad14a9fa229643e71 Mon Sep 17 00:00:00 2001 From: lkk <364857242@qq.com> Date: 星期四, 24 七月 2025 10:39:04 +0800 Subject: [PATCH] 1 --- src/main.js | 249 ++++++++++++++++++++++++++++++++++--------------- 1 files changed, 172 insertions(+), 77 deletions(-) diff --git a/src/main.js b/src/main.js index 0ac844c..37508ae 100644 --- a/src/main.js +++ b/src/main.js @@ -1,42 +1,23 @@ +// src/main.js import Vue from "vue"; - import Cookies from "js-cookie"; import "babel-polyfill"; import Element from "element-ui"; import "./assets/styles/element-variables.scss"; - -import "@/assets/styles/index.scss"; // global css -import "@/assets/styles/ruoyi.scss"; // ruoyi css +import "@/assets/styles/index.scss"; +import "@/assets/styles/ruoyi.scss"; import App from "./App"; import store from "./store"; import router from "./router"; -import directive from "./directive"; // directive -import plugins from "./plugins"; // plugins +import directive from "./directive"; +import plugins from "./plugins"; import { download } from "@/utils/request"; import Print from "vue-print-nb"; - -// import Updater from "./utils/AutoUpdate.js"; - -//鍓嶇閲嶆柊閮ㄧ讲閫氱煡鐢ㄦ埛鍒锋柊缃戦〉 -// const AutoUpdate = new Updater() -// AutoUpdate.on('update',()=>{ -// setTimeout(async()=>{ -// const result = confirm('褰撳墠鐗堟湰宸叉洿鏂帮紝璇风偣鍑荤‘瀹氬埛鏂伴〉闈綋楠�'); -// if(result){ -// location.reload(); -// } -// },500) -// }) - - import JsonExcel from "vue-json-excel"; - -Vue.component("downloadExcel", JsonExcel); - -import "./assets/icons"; // icon -import "./permission"; // permission control +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, @@ -45,38 +26,31 @@ selectDictLabels, handleTree, } from "@/utils/ruoyi"; -// 鍒嗛〉缁勪欢 import Pagination from "@/components/Pagination"; -// 鍒嗛〉缁勪欢 - -// import { monitorZoom } from "@/utils/devicePixelRatio.js"; -// const m = monitorZoom(); -// if (window.screen.width * window.devicePixelRatio >= 3840) { -// document.body.style.zoom = 100 / (Number(m) / 2); // 灞忓箷涓� 4k 鏃� -// } else { -// document.body.style.zoom = 100 / Number(m); -// } - -// 鑷畾涔夎〃鏍煎伐鍏风粍浠� -import RightToolbar from "@/components/RightToolbar"; -// 瀵屾枃鏈粍浠� import Editor from "@/components/Editor"; -// 鏂囦欢涓婁紶缁勪欢 import FileUpload from "@/components/FileUpload"; -// 鍥剧墖涓婁紶缁勪欢 import ImageUpload from "@/components/ImageUpload"; -// 鍥剧墖棰勮缁勪欢 import ImagePreview from "@/components/ImagePreview"; -// 瀛楀吀鏍囩缁勪欢 import DictTag from "@/components/DictTag"; -// 澶撮儴鏍囩缁勪欢 import VueMeta from "vue-meta"; -// 瀛楀吀鏁版嵁缁勪欢 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); +Vue.component("Pagination", Pagination); +Vue.component("Editor", Editor); +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; @@ -87,42 +61,163 @@ Vue.prototype.download = download; Vue.prototype.handleTree = handleTree; Vue.prototype.$echarts = echarts; -import VueBarcode from "vue-barcode"; -Vue.component("barcode", VueBarcode); -// 鍏ㄥ眬缁勪欢鎸傝浇 -Vue.component("DictTag", DictTag); -Vue.component("Pagination", Pagination); -Vue.component("RightToolbar", RightToolbar); -Vue.component("Editor", Editor); -Vue.component("FileUpload", FileUpload); -Vue.component("ImageUpload", ImageUpload); -Vue.component("ImagePreview", ImagePreview); +// 閫氱煡绠$悊锛氳窡韪綋鍓嶉�氱煡鍜屽亸绉婚噺 +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.use(directive); -Vue.use(plugins); -Vue.use(VueMeta); -Vue.use(Print); -DictData.install(); +// 鍏ㄥ眬閫氱煡鏂规硶锛屾坊鍔犫�滃凡璇烩�濇寜閽� +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: notificationManager.baseOffset, // 鍒濆 offset + onClick: null, // 涓嶈烦杞� + customClass: 'global-notification', + dangerouslyUseHTMLString: false, + appendTo: document.body + }); + notificationManager.addNotification(notification); // 娣诲姞鍒伴�氱煡绠$悊 +}; -/** - * If you don't want to use mock-server - * you want to use MockJs for mock api - * you can execute: mockXHR() - * - * Currently MockJs will be used in the production environment, - * please remove it before going online! ! ! - */ - -Vue.use(Element, { - size: Cookies.get("size") || "medium", // set element-ui default size +// 鐩戝惉璺敱鍙樺寲 +router.afterEach(() => { + console.log('璺敱鍒囨崲瀹屾垚锛屽綋鍓嶈矾寰�:', router.currentRoute.path); }); -Vue.config.productionTip = false; +// 瀹氫箟 WebSocket 鍒濆鍖栨爣蹇楋紝闃叉閲嶅杩炴帴 +let isWebSocketInitialized = false; -new Vue({ +const app = new Vue({ el: "#app", router, store, 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); +Vue.use(Print); +Vue.use(Element, { + size: Cookies.get("size") || "medium", +}); +DictData.install(); + +Vue.config.productionTip = false; \ No newline at end of file -- Gitblit v1.8.0