|
|
@@ -2,15 +2,125 @@
|
|
|
<div id="app">
|
|
|
<watercom ref="watercom"></watercom>
|
|
|
<router-view />
|
|
|
+ <system-notify ref="notify" :msgData="msgData" @showDailog="showDailog" />
|
|
|
+ <alarm-deal ref="alarmDeal" />
|
|
|
</div>
|
|
|
</template>
|
|
|
<script>
|
|
|
// import watercom from '@/components/waterCom.vue'
|
|
|
+import SystemNotify from '@/components/SystemNotify/index.vue';
|
|
|
+import AlarmDeal from '@/components/AlarmDeal/index.vue';
|
|
|
+import config from "@/config";
|
|
|
+import {mapGetters} from "vuex";
|
|
|
+import {getToken} from '@/utils/auth'
|
|
|
|
|
|
export default {
|
|
|
+ name: 'App',
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ messages:[],
|
|
|
+ socket: null,
|
|
|
+ msgData:{},
|
|
|
+ currentIndex: 0,
|
|
|
+ timer:null,
|
|
|
+ }
|
|
|
+ },
|
|
|
+ components: {
|
|
|
+ SystemNotify,AlarmDeal
|
|
|
+ },
|
|
|
+ mounted() {
|
|
|
+ this.initWebSocket();
|
|
|
+ },
|
|
|
+ computed: {
|
|
|
+ ...mapGetters(['userName', 'messageShow']),
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ showDailog(){
|
|
|
+ this.$refs.alarmDeal.show = true;
|
|
|
+ },
|
|
|
+ startLoop() {
|
|
|
+ this.$refs.notify.show = false;
|
|
|
+ this.currentItem = this.messages[this.currentIndex];
|
|
|
+ this.timer = setInterval(() => {
|
|
|
+ if(this.messageShow){
|
|
|
+ this.$refs.notify.show = true;
|
|
|
+ }
|
|
|
+ this.nextItem();
|
|
|
+ }, 10000); // 每 10 秒切换一次
|
|
|
+ },
|
|
|
+ nextItem() {
|
|
|
+ this.currentIndex = (this.currentIndex + 1) % this.messages.length;
|
|
|
+ this.msgData = this.messages[this.currentIndex];
|
|
|
+ },
|
|
|
+ getWeSocketUrl(){
|
|
|
+ let protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
|
|
|
+ let url = protocol + window.location.host + config.baseApi + '/system/websocket';
|
|
|
+ url = url + '?Authorization=Bearer ' + getToken() + '&clientId=' + config.VITE_APP_CLIENT_KEY;
|
|
|
+ return url;
|
|
|
+ },
|
|
|
+ initWebSocket() {
|
|
|
+ if(!config.VITE_APP_WEBSOCKET){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!getToken()){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用浏览器提供的 WebSocket 对象
|
|
|
+ this.socket = new WebSocket(this.getWeSocketUrl());
|
|
|
+
|
|
|
+ // 监听 WebSocket 连接打开事件
|
|
|
+ this.socket.addEventListener('open', (event) => {
|
|
|
+ console.log('WebSocket 连接已建立:', event);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 接收消息
|
|
|
+ this.socket.addEventListener('message', (event) => {
|
|
|
+ console.log('收到消息:', event.data);
|
|
|
+ this.dealMessage(event.data);
|
|
|
+ if(this.messages.length > 0){
|
|
|
+ this.startLoop();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 连接关闭事件
|
|
|
+ this.socket.addEventListener('close', (event) => {
|
|
|
+ console.log('WebSocket 连接已关闭:', event);
|
|
|
+ this.reconnect();
|
|
|
+ });
|
|
|
+
|
|
|
+ // 错误处理
|
|
|
+ this.socket.addEventListener('error', (error) => {
|
|
|
+ console.error('WebSocket 错误:', error);
|
|
|
+ this.reconnect();
|
|
|
+ });
|
|
|
+ },
|
|
|
+ dealMessage(msg) {
|
|
|
+ let alarm = JSON.parse(msg);
|
|
|
+ this.messages = alarm;
|
|
|
+ },
|
|
|
+ clearMessage(){
|
|
|
+ //websocket 清空消息
|
|
|
+ },
|
|
|
+ reconnect(){
|
|
|
+ //定义重新连接的时间间隔
|
|
|
+ const reconnectInterval = 5;
|
|
|
+ /* setTimeout(() => {
|
|
|
+ this.initWebSocket();
|
|
|
+ }, reconnectInterval * 1000);*/
|
|
|
+ }
|
|
|
+ },
|
|
|
+ beforeDestroy() {
|
|
|
+ // 页面卸载前关闭 WebSocket 连接
|
|
|
+ if (this.socket && this.socket.readyState === WebSocket.OPEN) {
|
|
|
+ this.socket.close();
|
|
|
+ }
|
|
|
+ clearInterval(this.timer);
|
|
|
+ },
|
|
|
|
|
|
- name: 'App'
|
|
|
}
|
|
|
+
|
|
|
</script>
|
|
|
<style lang="scss">
|
|
|
html,body,div,h1,h2,h3,h4,h5,p{
|