告警功能增加

This commit is contained in:
weiweiw 2024-11-10 08:55:36 +08:00
parent 68f09fcb7d
commit daff010eb7
4 changed files with 230 additions and 2 deletions

View File

@ -60,7 +60,8 @@
"vue-meta": "2.4.0",
"vue-router": "3.4.9",
"vuedraggable": "2.24.3",
"vuex": "3.6.0"
"vuex": "3.6.0",
"webstomp-client": "^1.2.6"
},
"devDependencies": {
"@vue/cli-plugin-babel": "4.4.6",

View File

@ -9,6 +9,7 @@
import ThemePicker from "@/components/ThemePicker";
import { mapActions } from 'vuex'
import { get } from '@/utils/config'
// import AlertNotification from "@/views/warning/AlertNotification.vue";
export default {
name: "App",

View File

@ -46,6 +46,7 @@ import { mapState } from 'vuex'
import variables from '@/assets/styles/variables.scss'
import { validateNewPassword } from '@/utils/validate'
import { updateUserPwd, checkPasswordStatus } from '@/api/system/user'
import {MessageBox} from "element-ui";
export default {
name: 'Layout',
@ -78,7 +79,12 @@ export default {
{ required: true, message: '确认密码不能为空', trigger: 'blur' },
{ required: true, validator: equalToPassword, trigger: 'blur' }
]
}
},
socket: null,
wsUrl: 'ws://localhost:18082/ws', // WebSocket
isConnected: false, //
reconnectInterval: 5000 //
}
},
components: {
@ -113,6 +119,7 @@ export default {
},
created() {
this.checkPasswordStatus()
this.connectWebSocket();
},
methods: {
checkPasswordStatus() {
@ -140,7 +147,100 @@ export default {
this.$store.dispatch('LogOut').then(() => {
location.href = '/index';
})
},
// WebSocket
connectWebSocket() {
if (this.socket) {
console.log("WebSocket 已连接");
return;
}
this.socket = new WebSocket(this.wsUrl);
// WebSocket
this.socket.onopen = () => {
console.log("WebSocket 连接成功");
this.isConnected = true;
};
//
this.socket.onmessage = (event) => {
console.log("收到消息:", event.data);
const warning = JSON.parse(event.data);
this.handleWarning(warning);
};
//
this.socket.onclose = () => {
console.log("WebSocket 连接已关闭");
this.isConnected = false;
this.socket = null;
//
this.reconnectWebSocket();
};
//
this.socket.onerror = (error) => {
console.error("WebSocket 错误:", error);
this.isConnected = false;
this.socket = null;
//
this.reconnectWebSocket();
};
},
// WebSocket
reconnectWebSocket() {
console.log("尝试重新连接 WebSocket...");
setTimeout(() => {
this.connectWebSocket();
}, this.reconnectInterval);
},
//
handleWarning(warning) {
console.log(warning)
const formattedTime = new Date(warning.warningTime).toLocaleString();
//
MessageBox.alert(
`
<p><strong>事件</strong>${warning.warningEvent}</p>
<p><strong>内容</strong>${warning.warningContent}</p>
<p><strong>等级</strong>${warning.warningGrade}</p>
<p><strong>IP</strong>${warning.warningIp}</p>
<p><strong>时间</strong>${formattedTime}</p>
`,
'告警通知',
{
dangerouslyUseHTMLString: true,
confirmButtonText: '确认',
callback: () => {
this.notifyBackend(warning.warningId);
}
}
);
},
//
notifyBackend(warningId) {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
const message = {
warningId,
status: '1' // 1
};
this.socket.send(JSON.stringify(message));
console.log(`已通知后端处理告警: ${warningId}`);
}
}
},
beforeDestroy() {
// WebSocket
if (this.socket) {
this.socket.close();
}
}
}
</script>

View File

@ -0,0 +1,126 @@
<template>
<div id="app">
<h2>告警系统</h2>
<p v-if="isConnected">WebSocket 已连接</p>
<p v-else>WebSocket 连接中...</p>
</div>
</template>
<script>
import { MessageBox } from 'element-ui';
export default {
data() {
return {
socket: null,
wsUrl: 'ws://localhost:18082/ws', // WebSocket
isConnected: false, //
reconnectInterval: 5000 //
};
},
created() {
// WebSocket
this.connectWebSocket();
},
methods: {
// WebSocket
connectWebSocket() {
if (this.socket) {
console.log("WebSocket 已连接");
return;
}
this.socket = new WebSocket(this.wsUrl);
// WebSocket
this.socket.onopen = () => {
console.log("WebSocket 连接成功");
this.isConnected = true;
};
//
this.socket.onmessage = (event) => {
console.log("收到消息:", event.data);
const warning = JSON.parse(event.data);
this.handleWarning(warning);
};
//
this.socket.onclose = () => {
console.log("WebSocket 连接已关闭");
this.isConnected = false;
this.socket = null;
//
this.reconnectWebSocket();
};
//
this.socket.onerror = (error) => {
console.error("WebSocket 错误:", error);
this.isConnected = false;
this.socket = null;
//
this.reconnectWebSocket();
};
},
// WebSocket
reconnectWebSocket() {
console.log("尝试重新连接 WebSocket...");
setTimeout(() => {
this.connectWebSocket();
}, this.reconnectInterval);
},
//
handleWarning(warning) {
const { warningEvent, warningContent, warningGrade, warningIp, warningTime } = warning;
const formattedTime = new Date(warningTime).toLocaleString();
//
MessageBox.alert(
`
<p><strong>事件</strong>${warningEvent}</p>
<p><strong>内容</strong>${warningContent}</p>
<p><strong>等级</strong>${warningGrade}</p>
<p><strong>IP</strong>${warningIp}</p>
<p><strong>时间</strong>${formattedTime}</p>
`,
'告警通知',
{
dangerouslyUseHTMLString: true,
confirmButtonText: '确认',
callback: () => {
this.notifyBackend(warning.warningId);
}
}
);
},
//
notifyBackend(warningId) {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
const message = {
warningId,
status: '1' // 1
};
this.socket.send(JSON.stringify(message));
console.log(`已通知后端处理告警: ${warningId}`);
}
}
},
beforeDestroy() {
// WebSocket
if (this.socket) {
this.socket.close();
}
}
};
</script>
<style scoped>
#app {
text-align: center;
margin-top: 50px;
}
</style>