From 9681fd1a48a4f79ee37e4e7352b94bf4d330b11a Mon Sep 17 00:00:00 2001 From: xd <844539747@qq.com> Date: Wed, 24 Apr 2024 13:16:34 +0800 Subject: [PATCH] '123' --- .../system/SysNoticeController.java | 37 +++- .../ruoyi/common/constant/WebsocketConst.java | 17 ++ .../ruoyi/system/mapper/SysNoticeMapper.java | 20 +- .../system/service/ISysNoticeService.java | 22 ++- .../service/impl/SysNoticeServiceImpl.java | 23 ++- .../mapper/system/SysNoticeMapper.xml | 8 + ruoyi-ui/src/api/system/businessNotice.js | 10 - ruoyi-ui/src/api/system/notice.js | 11 +- ruoyi-ui/src/layout/components/Navbar.vue | 96 +--------- .../src/layout/components/NavbarNotice.vue | 175 ++++++++++++++++++ 10 files changed, 294 insertions(+), 125 deletions(-) create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/constant/WebsocketConst.java delete mode 100644 ruoyi-ui/src/api/system/businessNotice.js create mode 100644 ruoyi-ui/src/layout/components/NavbarNotice.vue diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysNoticeController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysNoticeController.java index 8622828..602dd47 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysNoticeController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysNoticeController.java @@ -1,6 +1,14 @@ package com.ruoyi.web.controller.system; +import java.util.Comparator; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import com.alibaba.fastjson2.JSONObject; +import com.ruoyi.common.constant.WebsocketConst; +import com.ruoyi.framework.websocket.WebSocketServer; +import com.ruoyi.framework.websocket.WebSocketUsers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; @@ -22,7 +30,7 @@ import com.ruoyi.system.service.ISysNoticeService; /** * 公告 信息操作处理 - * + * * @author ruoyi */ @RestController @@ -32,6 +40,9 @@ public class SysNoticeController extends BaseController @Autowired private ISysNoticeService noticeService; + @Autowired + private WebSocketServer webSocketServer; + /** * 获取通知公告列表 */ @@ -63,6 +74,13 @@ public class SysNoticeController extends BaseController public AjaxResult add(@Validated @RequestBody SysNotice notice) { notice.setCreateBy(getUsername()); + + JSONObject obj = new JSONObject(); + obj.put(WebsocketConst.MSG_ID, notice.getNoticeId()); + obj.put(WebsocketConst.MSG_TITLE, notice.getNoticeTitle()); + obj.put(WebsocketConst.MSG_CONTENT, notice.getNoticeContent()); + WebSocketUsers.sendMessageToUsersByText(obj.toString()); + return toAjax(noticeService.insertNotice(notice)); } @@ -88,4 +106,21 @@ public class SysNoticeController extends BaseController { return toAjax(noticeService.deleteNoticeByIds(noticeIds)); } + + /** + * 导航面板 消息通知 + * @return + */ + @GetMapping("/navbarNoticelist") + public Map> navbarNoticelist() + { + List list = noticeService.navbarNoticelist(); + Map> groupedByNoticeType = list.stream() + .collect(Collectors.groupingBy(SysNotice::getNoticeType)); + for(String key:groupedByNoticeType.keySet()){ + List lt = groupedByNoticeType.get(key); + lt.sort((t1, t2) -> t2.getCreateTime().compareTo(t1.getCreateTime())); + } + return groupedByNoticeType; + } } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/constant/WebsocketConst.java b/ruoyi-common/src/main/java/com/ruoyi/common/constant/WebsocketConst.java new file mode 100644 index 0000000..7649d2c --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/constant/WebsocketConst.java @@ -0,0 +1,17 @@ +package com.ruoyi.common.constant; + +/** + * 消息推送通用常量 + * + * @author ruoyi + */ +public class WebsocketConst { + /** 消息ID */ + public static final String MSG_ID = "noticeId"; + + /** 消息TITLE */ + public static final String MSG_TITLE = "noticeTitle"; + + /** 消息CONTENT */ + public static final String MSG_CONTENT = "noticeContent"; +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysNoticeMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysNoticeMapper.java index c34f0a2..c762e38 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysNoticeMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysNoticeMapper.java @@ -5,14 +5,14 @@ import com.ruoyi.system.domain.SysNotice; /** * 通知公告表 数据层 - * + * * @author ruoyi */ public interface SysNoticeMapper { /** * 查询公告信息 - * + * * @param noticeId 公告ID * @return 公告信息 */ @@ -20,7 +20,7 @@ public interface SysNoticeMapper /** * 查询公告列表 - * + * * @param notice 公告信息 * @return 公告集合 */ @@ -28,7 +28,7 @@ public interface SysNoticeMapper /** * 新增公告 - * + * * @param notice 公告信息 * @return 结果 */ @@ -36,7 +36,7 @@ public interface SysNoticeMapper /** * 修改公告 - * + * * @param notice 公告信息 * @return 结果 */ @@ -44,7 +44,7 @@ public interface SysNoticeMapper /** * 批量删除公告 - * + * * @param noticeId 公告ID * @return 结果 */ @@ -52,9 +52,15 @@ public interface SysNoticeMapper /** * 批量删除公告信息 - * + * * @param noticeIds 需要删除的公告ID * @return 结果 */ public int deleteNoticeByIds(Long[] noticeIds); + + /** + * 消息推送信息获取 + * @return + */ + List navbarNoticelist(); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysNoticeService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysNoticeService.java index 47ce1b7..bed1e32 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysNoticeService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysNoticeService.java @@ -5,14 +5,14 @@ import com.ruoyi.system.domain.SysNotice; /** * 公告 服务层 - * + * * @author ruoyi */ public interface ISysNoticeService { /** * 查询公告信息 - * + * * @param noticeId 公告ID * @return 公告信息 */ @@ -20,7 +20,7 @@ public interface ISysNoticeService /** * 查询公告列表 - * + * * @param notice 公告信息 * @return 公告集合 */ @@ -28,7 +28,7 @@ public interface ISysNoticeService /** * 新增公告 - * + * * @param notice 公告信息 * @return 结果 */ @@ -36,7 +36,7 @@ public interface ISysNoticeService /** * 修改公告 - * + * * @param notice 公告信息 * @return 结果 */ @@ -44,17 +44,23 @@ public interface ISysNoticeService /** * 删除公告信息 - * + * * @param noticeId 公告ID * @return 结果 */ public int deleteNoticeById(Long noticeId); - + /** * 批量删除公告信息 - * + * * @param noticeIds 需要删除的公告ID * @return 结果 */ public int deleteNoticeByIds(Long[] noticeIds); + + /** + * 导航面板 消息通知 + * @return + */ + List navbarNoticelist(); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysNoticeServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysNoticeServiceImpl.java index a1b557c..726d768 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysNoticeServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysNoticeServiceImpl.java @@ -12,7 +12,7 @@ import com.ruoyi.system.service.ISysNoticeService; /** * 公告 服务层实现 - * + * * @author ruoyi */ @Service @@ -23,7 +23,7 @@ public class SysNoticeServiceImpl implements ISysNoticeService /** * 查询公告信息 - * + * * @param noticeId 公告ID * @return 公告信息 */ @@ -40,7 +40,7 @@ public class SysNoticeServiceImpl implements ISysNoticeService /** * 查询公告列表 - * + * * @param notice 公告信息 * @return 公告集合 */ @@ -61,7 +61,7 @@ public class SysNoticeServiceImpl implements ISysNoticeService /** * 新增公告 - * + * * @param notice 公告信息 * @return 结果 */ @@ -76,7 +76,7 @@ public class SysNoticeServiceImpl implements ISysNoticeService /** * 修改公告 - * + * * @param notice 公告信息 * @return 结果 */ @@ -91,7 +91,7 @@ public class SysNoticeServiceImpl implements ISysNoticeService /** * 删除公告对象 - * + * * @param noticeId 公告ID * @return 结果 */ @@ -103,7 +103,7 @@ public class SysNoticeServiceImpl implements ISysNoticeService /** * 批量删除公告信息 - * + * * @param noticeIds 需要删除的公告ID * @return 结果 */ @@ -112,4 +112,13 @@ public class SysNoticeServiceImpl implements ISysNoticeService { return noticeMapper.deleteNoticeByIds(noticeIds); } + + /** + * 导航面板 消息通知 + * @return + */ + @Override + public List navbarNoticelist() { + return noticeMapper.navbarNoticelist(); + } } diff --git a/ruoyi-system/src/main/resources/mapper/system/SysNoticeMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysNoticeMapper.xml index 3e4a7c0..2b3fc06 100644 --- a/ruoyi-system/src/main/resources/mapper/system/SysNoticeMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/SysNoticeMapper.xml @@ -86,4 +86,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + diff --git a/ruoyi-ui/src/api/system/businessNotice.js b/ruoyi-ui/src/api/system/businessNotice.js deleted file mode 100644 index 8e4aca6..0000000 --- a/ruoyi-ui/src/api/system/businessNotice.js +++ /dev/null @@ -1,10 +0,0 @@ -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/api/system/notice.js b/ruoyi-ui/src/api/system/notice.js index c274ea5..2600b07 100644 --- a/ruoyi-ui/src/api/system/notice.js +++ b/ruoyi-ui/src/api/system/notice.js @@ -41,4 +41,13 @@ export function delNotice(noticeId) { url: '/system/notice/' + noticeId, method: 'delete' }) -} \ No newline at end of file +} + +// 导航面板 消息通知 +export function navbarNoticelist(query) { + return request({ + url: '/system/notice/navbarNoticelist', + method: 'get', + params: query + }) +} diff --git a/ruoyi-ui/src/layout/components/Navbar.vue b/ruoyi-ui/src/layout/components/Navbar.vue index 5f151a2..5de08b6 100644 --- a/ruoyi-ui/src/layout/components/Navbar.vue +++ b/ruoyi-ui/src/layout/components/Navbar.vue @@ -17,6 +17,8 @@ --> + + @@ -57,18 +59,9 @@ import Search from '@/components/HeaderSearch' import RuoYiGit from '@/components/RuoYi/Git' import RuoYiDoc from '@/components/RuoYi/Doc' import settings from '@/settings' - -import { getNoticeListTop3 } from "@/api/system/businessNotice"; +import NavbarNotice from "@/layout/components/NavbarNotice.vue"; export default { - data() { - return { - url: "ws://localhost:3334/websocket/message", - message: "", - text_content: "", - ws: null, - }; - }, components: { Breadcrumb, TopNav, @@ -77,7 +70,8 @@ export default { SizeSelect, Search, RuoYiGit, - RuoYiDoc + RuoYiDoc, + NavbarNotice }, computed: { ...mapGetters([ @@ -102,87 +96,7 @@ 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') }, diff --git a/ruoyi-ui/src/layout/components/NavbarNotice.vue b/ruoyi-ui/src/layout/components/NavbarNotice.vue new file mode 100644 index 0000000..3531387 --- /dev/null +++ b/ruoyi-ui/src/layout/components/NavbarNotice.vue @@ -0,0 +1,175 @@ + + + +