From e4bdf650bb174a92d8726a0f247e39bd0680aa0d Mon Sep 17 00:00:00 2001 From: xd Date: Tue, 23 Apr 2024 23:10:20 +0800 Subject: [PATCH] 123 --- ruoyi-ui/src/api/system/businessNotice.js | 10 +++ ruoyi-ui/src/layout/components/Navbar.vue | 90 +++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 ruoyi-ui/src/api/system/businessNotice.js diff --git a/ruoyi-ui/src/api/system/businessNotice.js b/ruoyi-ui/src/api/system/businessNotice.js new file mode 100644 index 0000000..8e4aca6 --- /dev/null +++ b/ruoyi-ui/src/api/system/businessNotice.js @@ -0,0 +1,10 @@ +import request from '@/utils/request' + +// 查询业务消息 +export function getNoticeListTop3(query) { + return request({ + url: '/system/notice/list', + method: 'get', + params: query + }) +} diff --git a/ruoyi-ui/src/layout/components/Navbar.vue b/ruoyi-ui/src/layout/components/Navbar.vue index 5039595..5f151a2 100644 --- a/ruoyi-ui/src/layout/components/Navbar.vue +++ b/ruoyi-ui/src/layout/components/Navbar.vue @@ -58,7 +58,17 @@ import RuoYiGit from '@/components/RuoYi/Git' import RuoYiDoc from '@/components/RuoYi/Doc' import settings from '@/settings' +import { getNoticeListTop3 } from "@/api/system/businessNotice"; + export default { + data() { + return { + url: "ws://localhost:3334/websocket/message", + message: "", + text_content: "", + ws: null, + }; + }, components: { Breadcrumb, TopNav, @@ -92,7 +102,87 @@ export default { } } }, + mounted() { + /** 消息推送 */ + this.notice(); + const wsuri = this.url; + this.ws = new WebSocket(wsuri); + const self = this; + this.ws.onopen = function (event) { + //self.text_content = self.text_content + "已经打开连接!" + "\n"; + }; + this.ws.onmessage = function (event) { + //self.text_content = event.data + "\n"; + var messageBody = JSON.parse(event.data); + Notification.info({ + title: "通知", + dangerouslyUseHTMLString: true, + message: messageBody.noticeContent, + duration: 0, + offset: 40, + onClick: function () { + //self.warnDetailByWarnid(messageBody.warnId); //自定义回调,message为传的参数 + // 点击跳转的页面 + }, + }); + }; + this.ws.onclose = function (event) { + //self.text_content = self.text_content + "已经关闭连接!" + "\n"; + }; + /** 消息推送 */ + }, methods: { + /** 消息推送 */ + notice() { + getNoticeListTop3().then(response => { + for (let i = 0; i < response.length; i++) { + let messageBody = response[i]; + setTimeout(() => { + this.notificationInfo(messageBody); + }, 100); + } + }) + }, + notificationInfo(messageBody) { + Notification.info({ + title: "通知", + dangerouslyUseHTMLString: true, + message: messageBody.noticeContent, + duration: 0, + offset: 40, + onClick: function () { + //self.warnDetailByWarnid(messageBody.warnId); //自定义回调,message为传的参数 + // 点击跳转的页面 + }, + }); + }, + // join() { + // + // }, + exit() { + if (this.ws) { + this.ws.close(); + this.ws = null; + } + }, + send() { + if (this.ws) { + this.ws.send(this.message); + } else { + alert("未连接到服务器"); + } + }, + warnDetailByWarnid(warnid) { + // 跳转预警详情页面 + this.$router.push({ + path: "/XXX/XXX", + query: { + warnid: warnid, + }, + }); + }, + /** 消息推送 */ + toggleSideBar() { this.$store.dispatch('app/toggleSideBar') },