告警弹窗设置,子页面生效

This commit is contained in:
龚皓 2024-10-25 16:52:42 +08:00
parent 47dc751cca
commit 474d7b533d
1 changed files with 152 additions and 0 deletions

152
src/components/Settings.vue Normal file
View File

@ -0,0 +1,152 @@
<template>
<div class="settings-container">
<el-row class="popup-row">
<el-checkbox v-model="isPopupEnabled" @change="handleCheckboxChange">开启弹窗</el-checkbox>
</el-row>
</div>
</template>
<script lang="ts" setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
//
const isPopupEnabled = ref(false);
let websocket: WebSocket | null = null;
let heartbeatInterval: number | null = null; //
// localStorage
onMounted(() => {
const storedState = localStorage.getItem('isPopupEnabled');
isPopupEnabled.value = storedState === 'true';
if (isPopupEnabled.value) {
connectWebSocket();
}
});
// WebSocket
onBeforeUnmount(() => {
closeWebSocket();
});
//
const handleCheckboxChange = () => {
if (isPopupEnabled.value) {
//
ElMessageBox.confirm('是否开启弹窗提示?', '提示', {
confirmButtonText: '是',
cancelButtonText: '否',
type: 'warning',
})
.then(() => {
// WebSocket
localStorage.setItem('isPopupEnabled', 'true'); // localStorage
connectWebSocket();
})
.catch(() => {
//
isPopupEnabled.value = false;
localStorage.setItem('isPopupEnabled', 'false'); // localStorage
});
} else {
// WebSocket
closeWebSocket();
}
};
// WebSocket
const connectWebSocket = () => {
websocket = new WebSocket('ws://192.168.28.11:8080/event/ws');
websocket.onopen = () => {
ElMessage.success('弹窗告警开启成功');
startHeartbeat(); //
};
websocket.onmessage = (event) => {
//
showNotification(event.data);
};
websocket.onclose = () => {
ElMessage.warning('WebSocket 已关闭');
isPopupEnabled.value = false; //
localStorage.setItem('isPopupEnabled', 'false'); // localStorage
stopHeartbeat(); //
};
websocket.onerror = () => {
ElMessage.error('WebSocket 连接出错');
isPopupEnabled.value = false;
localStorage.setItem('isPopupEnabled', 'false');
stopHeartbeat(); //
};
};
// WebSocket
const closeWebSocket = () => {
if (websocket) {
websocket.close();
ElMessage.info('WebSocket 连接已关闭');
localStorage.setItem('isPopupEnabled', 'false'); // localStorage
stopHeartbeat(); //
}
};
//
const showNotification = (message: string) => {
if (Notification.permission === 'granted') {
new Notification('新消息', {
body: message,
icon: '/path/to/icon.png', //
});
} else if (Notification.permission !== 'denied') {
//
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
new Notification('新消息', {
body: message,
icon: '/path/to/icon.png',
});
}
});
}
};
// ping WebSocket
const startHeartbeat = () => {
if (heartbeatInterval) return;
heartbeatInterval = window.setInterval(() => {
if (websocket && websocket.readyState === WebSocket.OPEN) {
websocket.send('ping'); //
}
}, 5000); // 5
};
//
const stopHeartbeat = () => {
if (heartbeatInterval) {
clearInterval(heartbeatInterval);
heartbeatInterval = null;
}
};
//
onMounted(() => {
if (Notification.permission !== 'granted') {
Notification.requestPermission();
}
});
</script>
<style scoped>
.settings-container {
padding: 20px;
}
.popup-row {
margin-bottom: 20px;
}
</style>