• 前端实现实时消息提醒消息通知


    需求:当用户收到待审批和待处理的消息后状态栏图标闪烁并进行弹窗提醒,点击消息跳转到指定的消息。

    实现方式:web端+c端。

    说明:

    • 客户不需要非常的及时的接收消息,所以未对接websocket协议,使用20秒刷新一次的方法,首次记录下待审批数量和待处理数量,20秒刷新后与上次的数量作比较,比上次多了就查询出多的这条数据,调用c端的消息提醒服务,将参数传递过去,在弹窗里点击指定消息的时候,再又C端将参数绑定在跳转路径上,由前端查询出指定的数据并展示。
    • C端实现方法不做说明(因为不懂所以说个大致思路,C端提供一个消息提醒exe,用户登录后web端检测当前是否已安装exe,没有提醒用户去下载,正常开启后,会在本地有一个服务,前端调用服务传参,正常打开消息弹窗,在点击某条消息时C端将前端传递的参数拼接到浏览器的地址里打开浏览器窗口,前端在mounted里获取数据并处理。)
    • 使用window.webkitNotifications.createNotification方法进行提醒会有问题,只能在本地,将地址栏改为IP地址后不能正常使用。

    前端实现方法:

    1、定义全局变量

    1. var configObj = {
    2. MESSAGE_URL:'http://127.0.0.1:8087/webServer',
    3. waitMESSAGENUM: 0,//待审批事项
    4. waitDealWithMESSAGENUM: 0,//待处理事项
    5. };

    2、通过注册表启用C端提供的exe程序

    window.open("messageRemind://","_self");

    3、判断消息服务是否存在,不存在提醒用户下载安装

    1. isExe(){
    2. var that = this;
    3. this.$post(this.$url.MESSAGE_URL, {
    4. path:""
    5. })
    6. .then(res=>{
    7. //正常
    8. if(res && res.data && res.data.Message == "isexe"){
    9. //没有exe去安装
    10. }else{
    11. this.$confirm('未检测到提醒插件,是否安装?如不需要提醒功能点击取消即可。', "询问", {
    12. confirmButtonText: "确定",
    13. cancelButtonText: "取消",
    14. type: "warning"
    15. }).then(function () {
    16. window.open("messageRemind.exe","_blank");
    17. that.$message({
    18. showClose: true,
    19. message: "手动安装消息提醒插件完成后,请退出系统重新登陆!",
    20. duration:0
    21. });
    22. })
    23. }
    24. // 没有exe去安装
    25. }).catch((err)=>{
    26. this.$confirm('未检测到提醒插件,是否安装?如不需要提醒功能点击取消即可。', "询问", {
    27. confirmButtonText: "确定",
    28. cancelButtonText: "取消",
    29. type: "warning"
    30. }).then(function () {
    31. window.open("messageRemind.exe","_blank");
    32. that.$message({
    33. showClose: true,
    34. message: "手动安装消息提醒插件完成后,请退出系统重新登陆!",
    35. duration:0
    36. });
    37. })
    38. })
    39. },

    4、实际调用

    1. <template>
    2. <div class="nav-header">
    3. <!-- 右侧操作 -->
    4. <div class="rightPerson">
    5. <span style="position: relative;cursor: pointer;" @click="isOpenMenu = !isOpenMenu">
    6. <img src="../../assets/header/message.png" alt="" style="margin-right: 30px;"/>
    7. <span class="message-red" v-show="waitItemNumber != 0 || waitDealwithNumber != 0">{{waitItemNumber + waitDealwithNumber}}</span>
    8. <div class="message-red-bottom" v-show="isOpenMenu">
    9. <span @click="toWaitItemsFun">待审批事项
    10. <span class="message-red" v-show="waitItemNumber != 0">{{waitItemNumber}}</span>
    11. </span>
    12. <span @click="toWaitDealwithFun">待处理事项
    13. <span class="message-red" v-show="waitDealwithNumber != 0">{{waitDealwithNumber}}</span>
    14. </span>
    15. </div>
    16. </span>
    17. </div>
    18. </div>
    19. </template>
    20. <script>
    21. import bus from "../../plugins/utils/bus";
    22. export default {
    23. name: "NavHeader",
    24. data() {
    25. return {
    26. isOpenMenu:false,
    27. waitItemNumber: 0,
    28. waitDealwithNumber:0,
    29. timer: null,
    30. timer1:null,
    31. //跳转传递的参数
    32. jumpParam:{
    33. id:null,
    34. title:"",
    35. time:"",
    36. path:""
    37. }
    38. };
    39. },
    40. created() {
    41. //审批之后更新右上角铃铛提醒数量
    42. bus.$on("waitItemsData", (data) => {
    43. this.getWaitListNum();
    44. setTimeout(() => {
    45. this.$url.waitMESSAGENUM = JSON.parse(JSON.stringify(this.waitItemNumber));
    46. },500)
    47. });
    48. //处理之后更新右上角铃铛提醒数量
    49. bus.$on("waitDealWithData", (data) => {
    50. this.getWaitDealwithNum();
    51. setTimeout(() => {
    52. this.$url.waitDealWithMESSAGENUM = JSON.parse(JSON.stringify(this.waitDealwithNumber));
    53. },500)
    54. });
    55. },
    56. beforeDestroy() {
    57. if(this.timer) {
    58. clearInterval(this.timer);
    59. this.timer = null;
    60. }
    61. if(this.timer1) {
    62. clearInterval(this.timer1);
    63. this.timer1 = null;
    64. }
    65. bus.$off("waitItemsData");
    66. bus.$off("waitDealWithData");
    67. },
    68. mounted() {
    69. //获取最新的待审批和待处理数量,显示在右上角铃铛
    70. this.getWaitListNum();
    71. this.getWaitDealwithNum();
    72. //获取待审批和待处理的数量后刷新待审批和待处理的全局记录,用于与下次的数量作比较判断数据是否有更新
    73. setTimeout(() => {
    74. this.$url.waitMESSAGENUM = JSON.parse(JSON.stringify(this.waitItemNumber));
    75. this.$url.waitDealWithMESSAGENUM = JSON.parse(JSON.stringify(this.waitDealwithNumber));
    76. //调用待审批和待处理的弹窗,开启20秒定时事件
    77. this.openMessageWindow();
    78. this.openDealWithMessageWindow();
    79. },500);
    80. },
    81. methods: {
    82. /**
    83. * @author yangjie
    84. * @time 2022-06-07 16:45:58
    85. * @description 调用消息弹窗服务打开待审批消息的弹窗
    86. * @return
    87. */
    88. openMessageWindow(){
    89. let that = this;
    90. that.timer = setInterval(() => {
    91. //每20秒拿一次最新数量,与全局的记录作比较
    92. that.getWaitListNum();
    93. setTimeout(() => {
    94. if(that.waitItemNumber>that.$url.waitMESSAGENUM) {
    95. //如果有新数据更新一次最新数据,用于消息提醒
    96. that.getWaitListValue();
    97. that.$nextTick(()=>{
    98. that.$post(that.$url.MESSAGE_URL, {
    99. param:JSON.stringify(this.jumpParam)
    100. });
    101. that.$url.waitMESSAGENUM = JSON.parse(JSON.stringify(that.waitItemNumber));
    102. })
    103. }
    104. },500)
    105. },20 * 1000)
    106. },
    107. /**
    108. * @author yangjie
    109. * @time 2022-06-07 16:45:58
    110. * @description 调用消息弹窗服务打开待处理消息的弹窗
    111. * @return
    112. */
    113. openDealWithMessageWindow(){
    114. var that = this;
    115. that.timer1 = setInterval(() => {
    116. var requestObj = {
    117. submituid:that.$store.state.user.id,
    118. status: 5, //获取已同意和已驳回的数据
    119. remark:"updatetime"
    120. };
    121. that.$post(that.$url.MANAGE_URL + "/dyTemplateProcess/selectSubmitTask?token=" + that.$store.state.token, requestObj).then(res => {
    122. if (res.data.code == 1 && res.data.data.data && res.data.data.data.length > 0) {
    123. that.waitDealwithNumber = Number(res.data.data.count);
    124. if(that.waitDealwithNumber > that.$url.waitDealWithMESSAGENUM) {
    125. that.$url.waitDealWithMESSAGENUM = JSON.parse(JSON.stringify(that.waitDealwithNumber));
    126. that.jumpParam.id = res.data.data.data[0].id;
    127. that.jumpParam.title = res.data.data.data[0].templateName;
    128. that.jumpParam.time = res.data.data.data[0].submittime;
    129. that.jumpParam.path = window.location.href.split("#")[0] + "#/collaborative/issuedItems";
    130. that.$post(that.$url.MESSAGE_URL,{
    131. param:JSON.stringify(that.jumpParam)
    132. })
    133. }
    134. } else {
    135. that.waitDealwithNumber = 0;
    136. }
    137. }).catch(error => {
    138. that.waitDealwithNumber = 0;
    139. })
    140. },20 * 1000)
    141. },
    142. /**
    143. * @author yangjie
    144. * @time 2022-06-29 15:34:25
    145. * @description 获取待审批事项的总数量(用于右上角铃铛显示和与用于与上次比较判断出是否有新的待审批事项)
    146. * @return
    147. */
    148. getWaitListNum() {
    149. var that = this;
    150. let requestObj = {
    151. nextuid:this.$store.state.user.id
    152. };
    153. this.$post(this.$url.MANAGE_URL + "/dyTemplateProcess/selectTodoTask?token=" + this.$store.state.token, requestObj).then(res => {
    154. if (res.data.code == 1 && res.data.data.data && res.data.data.data.length > 0) {
    155. that.waitItemNumber = Number(res.data.data.count);
    156. } else {
    157. that.waitItemNumber = 0;
    158. }
    159. }).catch(error => {
    160. that.waitItemNumber = 0;
    161. })
    162. },
    163. /**
    164. * @author yangjie
    165. * @time 2022-06-29 15:32:24
    166. * @description 获取最后一条待审批事项的值(传给c用于消息提醒)
    167. * @return
    168. */
    169. getWaitListValue(dataid) {
    170. var that = this;
    171. let requestObj = {
    172. nextuid:this.$store.state.user.id
    173. };
    174. if(dataid){
    175. requestObj.idList = dataid;
    176. }
    177. this.$post(this.$url.MANAGE_URL + "/dyTemplateProcess/selectTodoTask?token=" + this.$store.state.token, requestObj).then(res => {
    178. if (res.data.code == 1 && res.data.data.data && res.data.data.data.length > 0) {
    179. that.jumpParam.id = res.data.data.data[0].id;
    180. that.jumpParam.title = res.data.data.data[0].templateName;
    181. that.jumpParam.time = res.data.data.data[0].submittime;
    182. this.jumpParam.path = window.location.href.split("#")[0] + "#/collaborative/waitItems";
    183. } else {
    184. that.waitItemNumber = 0;
    185. }
    186. }).catch(error => {
    187. that.waitItemNumber = 0;
    188. })
    189. },
    190. /**
    191. * @author yangjie
    192. * @time 2022-05-18 15:15:39
    193. * @description 获取待处理事项的总数量(用于右上角铃铛显示和与用于与上次比较判断出是否有新的待处理事项)
    194. * @return
    195. */
    196. getWaitDealwithNum(){
    197. var that = this;
    198. let requestObj = {
    199. submituid:this.$store.state.user.id,
    200. status: 5 //获取已同意和已驳回的数据
    201. };
    202. this.$post(this.$url.MANAGE_URL + "/dyTemplateProcess/selectSubmitTask?token=" + this.$store.state.token, requestObj).then(res => {
    203. if (res.data.code == 1 && res.data.data.data && res.data.data.data.length > 0) {
    204. that.waitDealwithNumber = Number(res.data.data.count);
    205. } else {
    206. that.waitDealwithNumber = 0;
    207. }
    208. }).catch(error => {
    209. that.waitDealwithNumber = 0;
    210. })
    211. },
    212. //查看待办事项
    213. toWaitItemsFun() {
    214. this.$router.push({
    215. path: "/collaborative/waitItems" // 到待办事项
    216. });
    217. },
    218. /**
    219. * @author yangjie
    220. * @time 2022-05-18 15:45:27
    221. * @description 查看待处理事项
    222. * @return
    223. */
    224. toWaitDealwithFun(){
    225. this.$router.push({
    226. path: "/collaborative/issuedItems" // 到待处理事项
    227. });
    228. }
    229. }
    230. }
    231. </script>
    232. <style scoped lang="scss">
    233. </style>

    5、效果图

    收到消息后如下图

    点击闪烁的消息弹窗后如下图,点击跳转对应页面

  • 相关阅读:
    软件测试漏测怎么办(下)
    为什么要做数据治理以及如何进行数据治理?
    【数据结构】线性表的抽象数据类型
    【Spring源码】14. 消息多播器(观察者模式)
    Win10怎么重启资源管理器?重启资源管理器快捷键是什么
    lightdb22.4-新增优化器提示cardinality 和ordered_predicates
    区块链架构下,智慧城市加速发展
    [远程Call]32位远程多参数带返回调用
    星图地球数据云,便捷加载各类在线地图服务的又一神器
    docker 安装 Jenkins
  • 原文地址:https://blog.csdn.net/qq_42129143/article/details/125525986