• Java+Vue 实现消息通知示例


     

    前端代码部分:

    右上方小铃铛组件

     

     

    1. <template>
    2. <div>
    3. <el-popover placement="bottom" :width="280" trigger="click">
    4. <template #reference>
    5. <el-badge :is-dot="isDot" class="item" style="line-height: 40px">
    6. <!-- <svg-icon :icon-class="Bell" @click="toggle" />-->
    7. <el-icon class="el-icon-bell" style="font-size: 20px;cursor:pointer;transform: translateY(0.6px);" @click="bellClick"></el-icon>
    8. <!-- <svg-icon class-name="search-icon" icon-class="search" @click.stop="click" />-->
    9. </el-badge>
    10. </template>
    11. <div v-if="noticeList.length>0">
    12. <el-tabs>
    13. <el-tab-pane label="通知">
    14. <div v-for="(item,index) in noticeList.slice(0,5)">
    15. <table>
    16. <tr>
    17. <td rowspan="2">
    18. <el-avatar icon="message" style="width:20px;height:20px;margin-right: 20px;background-color: #4d88ff"/>
    19. </td>
    20. <td>
    21. <el-tooltip
    22. class="box-item"
    23. effect="dark"
    24. :content="item.messageTitle"
    25. placement="bottom"
    26. >
    27. <el-link type="primary" @click="handleRead(item)" style="font-size: 15px">
    28. {{item.messageTitle}}
    29. </el-link>
    30. </el-tooltip>
    31. </td>
    32. </tr>
    33. <tr>
    34. <td>
    35. <span>{{item.createTime}}</span>
    36. </td>
    37. </tr>
    38. </table>
    39. <hr v-if="index color="#e6e6e6"/>
    40. </div>
    41. <div style="text-align: center;margin-top: 10px">
    42. <el-button v-show="showAllBtn" align="center" type="primary" @click="showAll()">查看更多</el-button>
    43. </div>
    44. </el-tab-pane>
    45. </el-tabs>
    46. </div>
    47. <div v-else>
    48. <div style="text-align: center">暂无未读消息</div>
    49. </div>
    50. </el-popover>
    51. <!-- 添加或修改我的消息对话框 -->
    52. <el-dialog title="我的消息" :visible.sync="open" width="500px" append-to-body>
    53. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    54. <el-form-item label="消息标题" prop="messageTitle">
    55. <el-input disabled v-model="form.messageTitle" placeholder="请输入消息标题" />
    56. </el-form-item>
    57. <!-- <el-form-item label="用户ID" prop="userId">-->
    58. <!-- <el-input disabled v-model="form.userId" placeholder="请输入用户ID" />-->
    59. <!-- </el-form-item>-->
    60. <el-form-item label="消息内容">
    61. <editor :readOnly="true" v-model="form.messageContent" :min-height="192"/>
    62. </el-form-item>
    63. <!-- <el-form-item label="备注" prop="remark">-->
    64. <!-- <el-input disabled v-model="form.remark" placeholder="请输入备注" />-->
    65. <!-- </el-form-item>-->
    66. </el-form>
    67. <div slot="footer" class="dialog-footer">
    68. <!-- <el-button type="primary" @click="submitForm">确 定</el-button>-->
    69. <el-button @click="cancel">关闭</el-button>
    70. </div>
    71. </el-dialog>
    72. </div>
    73. </template>
    74. <script>
    75. import {mapGetters} from "vuex";
    76. import router from 'vue-router'
    77. import {listMyUnreadNotice,getSysmessage,listSysmessage} from "@/api/system/sysmessage";
    78. import events from '@/utils/events'
    79. export default {
    80. name: "index",
    81. components: {},
    82. data() {
    83. return {
    84. noticeList: [],
    85. showAllBtn: false,
    86. form:{},
    87. rules:{},
    88. open:false,
    89. queryParams:{
    90. id:null,
    91. status:'0',
    92. }
    93. }
    94. },
    95. computed: {
    96. ...mapGetters(["isDot",'id']),
    97. },
    98. created() {
    99. this.getUnRead(); //查询是否有未读消息通知
    100. events.$on('noticePush', this.getUnRead)
    101. events.$on('noticePushByMsg', this.getUnRead)
    102. this.$nextTick(()=>{
    103. events.$on('noticeCheckByMsg', this.getUnRead)
    104. })
    105. },
    106. mounted() {
    107. },
    108. methods: {
    109. /** 查询是否存在未读消息 根据当前用户匹配消息表中得ID*/
    110. getUnRead() {
    111. this.queryParams.id = this.id
    112. listSysmessage(this.queryParams).then(res => {
    113. if(res.code == 200){
    114. if (res.rows.length>0) {
    115. let object = res.rows
    116. this.noticeList = object
    117. if(object.length >5){
    118. this.showAllBtn=true
    119. }else{
    120. this.showAllBtn=false
    121. }
    122. this.$store.dispatch('app/setIsDot', true)
    123. } else {
    124. this.noticeList = []
    125. this.$store.dispatch('app/setIsDot', false)
    126. }
    127. }
    128. });
    129. },
    130. bellClick() {
    131. this.getUnRead()
    132. },
    133. cancel(){
    134. this.open = false
    135. this.getUnRead();
    136. },
    137. //查看更多
    138. showAll() {
    139. this.$router.push({path: "/monitor/myMessage"});
    140. },
    141. //查看消息 弹窗显示
    142. handleRead(data){
    143. const messageId = data.messageId
    144. getSysmessage(messageId).then(response => {
    145. this.form = response.data;
    146. // this.form.messageTitle = data.messageTitle
    147. // this.form.messageContent = data.messageContent
    148. this.open = true
    149. });
    150. }
    151. }
    152. }
    153. </script>
    154. <style scoped>
    155. </style>

    把是否有消息的状态通过vuex存到公共状态里来管理

     

     

    this.$store.dispatch('app/setIsDot', true) //设置红点
    this.$store.dispatch('app/setIsDot', false) //去掉红点

     首先新建通知公告 发布通知

     我的消息列表

    JAVA部分

    两个表 通知公告表和我的消息表 做关联

    发布消息根据指定的用户类型去插入数据 通过userID分别插入到我的消息表中

    全部设置已读 首先去查未读的消息根据userID,在遍历集合更新状态

     

     

  • 相关阅读:
    移动应用数据安全管理要求(一)
    百度飞浆环境安装
    跨域的解决方案
    Spring的bean装配和bean的自动装配
    MyBatisPlus 基础Mapperr接口:增删改查
    Nodejs+vue+elementui汽车销售网站管理系统
    【苍穹外卖 | 项目日记】第四天
    Union类型和集合的union()方法-set.union()
    Android进阶:5、发送post请求、json数据格式以及okhttp框架的使用
    JS中的BOM
  • 原文地址:https://blog.csdn.net/huichao199175/article/details/133361858