App.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <div id="app">
  3. <watercom ref="watercom"></watercom>
  4. <router-view />
  5. <system-notify ref="notify" :msgData="msgData" @showDialog="showDialog" />
  6. <alarm-deal ref="alarmDeal" @removeMessage="removeMessage"/>
  7. </div>
  8. </template>
  9. <script>
  10. // import watercom from '@/components/waterCom.vue'
  11. import SystemNotify from '@/components/SystemNotify/index.vue';
  12. import AlarmDeal from '@/components/AlarmDeal/index.vue';
  13. import config from "@/config";
  14. import {mapGetters} from "vuex";
  15. import {getToken} from '@/utils/auth'
  16. export default {
  17. name: 'App',
  18. data() {
  19. return {
  20. messages:[],
  21. socket: null,
  22. msgData:{},
  23. currentIndex: 0,
  24. timer:null,
  25. heartBeatTimer:null,
  26. }
  27. },
  28. components: {
  29. SystemNotify,AlarmDeal
  30. },
  31. mounted() {
  32. //this.initWebSocket();
  33. },
  34. provide() {
  35. return {
  36. initWebSocket: this.initWebSocket,
  37. }
  38. },
  39. computed: {
  40. ...mapGetters(['userName', 'messageShow']),
  41. },
  42. methods: {
  43. showDialog(obj){
  44. this.$refs.alarmDeal.show = true;
  45. this.$refs.alarmDeal.data = obj;
  46. },
  47. removeMessage(iotAlarmDataId){
  48. console.log("处理报警id:"+iotAlarmDataId)
  49. let index = this.messages.findIndex(item => item.iotAlarmDataId+'' === iotAlarmDataId+'');
  50. if (index !== -1){
  51. this.messages.splice(index, 1)
  52. }
  53. this.currentIndex = 0;
  54. if (this.messages.length === 0){
  55. //无消息就不显通知
  56. clearInterval(this.timer);
  57. this.$refs.notify.show = false;
  58. }
  59. },
  60. startLoop() {
  61. this.$refs.notify.show = false;
  62. this.timer = setInterval(() => {
  63. if(this.messageShow){
  64. this.$refs.notify.show = true;
  65. this.nextItem();
  66. }
  67. }, 15 * 1000); // 每 15 秒切换一次
  68. },
  69. nextItem() {
  70. this.currentIndex = (this.currentIndex + 1) % this.messages.length;
  71. this.msgData = this.messages[this.currentIndex];
  72. },
  73. getWeSocketUrl(){
  74. let protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
  75. let url = "";
  76. if (config.baseApi === "/"){
  77. url = url + protocol + window.location.host + '/system/websocket';
  78. }else {
  79. url = url + protocol + window.location.host + config.baseApi + '/system/websocket';
  80. }
  81. url = url + '?Authorization=Bearer ' + getToken() + '&clientId=' + config.VITE_APP_CLIENT_KEY;
  82. return url;
  83. },
  84. initWebSocket() {
  85. if(!config.VITE_APP_WEBSOCKET || !getToken()){
  86. return;
  87. }
  88. // 使用浏览器提供的 WebSocket 对象
  89. this.socket = new WebSocket(this.getWeSocketUrl());
  90. // 监听 WebSocket 连接打开事件
  91. this.socket.addEventListener('open', (event) => {
  92. console.log('WebSocket 连接已建立:', event);
  93. });
  94. // 接收消息
  95. this.socket.addEventListener('message', (event) => {
  96. console.log('收到消息:', event.data);
  97. this.dealMessage(event.data);
  98. if(this.messages.length > 0){
  99. this.startLoop();
  100. }
  101. });
  102. // 连接关闭事件
  103. this.socket.addEventListener('close', (event) => {
  104. console.log('WebSocket 连接已关闭:', event);
  105. this.reconnect();
  106. });
  107. // 错误处理
  108. this.socket.addEventListener('error', (error) => {
  109. console.error('WebSocket 错误:', error);
  110. this.reconnect();
  111. });
  112. this.startSocketHeartBeat();
  113. },
  114. dealMessage(msg) {
  115. //收到消息首先清除显示
  116. clearInterval(this.timer);
  117. this.$refs.notify.show = false;
  118. let alarm = JSON.parse(msg);
  119. //如果消息已经被处理了,就从队列中移除
  120. if(alarm.isDo && alarm.isDo === 1){
  121. this.removeMessage(alarm.iotAlarmDataId);
  122. }else {
  123. this.messages.push(alarm);
  124. }
  125. },
  126. clearMessage(){
  127. //websocket 清空消息
  128. },
  129. reconnect(){
  130. //定义重新连接的时间间隔
  131. const reconnectInterval = 30;
  132. setTimeout(() => {
  133. this.initWebSocket();
  134. }, reconnectInterval * 1000);
  135. },
  136. startSocketHeartBeat(){
  137. this.heartBeatTimer = setInterval(() => {
  138. if (this.socket.readyState === WebSocket.OPEN) {
  139. this.socket.send('ping');
  140. console.log('发送websocket心跳');
  141. }
  142. },30 * 1000);
  143. },
  144. stopHeartBeat(){
  145. console.log('停止websocket心跳');
  146. clearInterval(this.heartBeatTimer);
  147. }
  148. },
  149. beforeDestroy() {
  150. // 页面卸载前关闭 WebSocket 连接
  151. if (this.socket && this.socket.readyState === WebSocket.OPEN) {
  152. this.socket.close();
  153. }
  154. clearInterval(this.timer);
  155. this.stopHeartBeat();
  156. },
  157. }
  158. </script>
  159. <style lang="scss">
  160. html,body,div,h1,h2,h3,h4,h5,p{
  161. margin: 0;
  162. padding: 0;
  163. box-sizing: border-box;
  164. user-select: none;
  165. -moz-user-select:none; /* Firefox私有属性 */
  166. -webkit-user-select:none; /* WebKit内核私有属性 */
  167. -ms-user-select:none; /* IE私有属性(IE10及以后) */
  168. }
  169. #app {
  170. font-family: Avenir, Helvetica, Arial, sans-serif;
  171. -webkit-font-smoothing: antialiased;
  172. -moz-osx-font-smoothing: grayscale;
  173. font-size: 25px;
  174. user-select: none;
  175. -moz-user-select:none; /* Firefox私有属性 */
  176. -webkit-user-select:none; /* WebKit内核私有属性 */
  177. -ms-user-select:none; /* IE私有属性(IE10及以后) */
  178. }
  179. .fieldset{
  180. border: none;
  181. padding: 0;
  182. margin: 0;
  183. }
  184. pre{
  185. white-space: pre-wrap;
  186. word-wrap: break-word;
  187. }
  188. </style>