|
|
@@ -3,6 +3,11 @@ import store from './store'
|
|
|
import NProgress from 'nprogress' // progress bar
|
|
|
import 'nprogress/nprogress.css' // progress bar style
|
|
|
import { cancelRequest } from './utils/request'
|
|
|
+import {
|
|
|
+ subscribe,
|
|
|
+ unsubscribe
|
|
|
+} from './utils/mqtt'
|
|
|
+import { Notification } from 'element-ui'
|
|
|
|
|
|
NProgress.configure({ showSpinner: false }) // NProgress Configuration
|
|
|
|
|
|
@@ -15,16 +20,20 @@ router.beforeEach((to, from, next) => {
|
|
|
|
|
|
if (store.getters.isValid) {
|
|
|
if (to.path === '/error') {
|
|
|
+ startMonitor()
|
|
|
// redirect to the home page
|
|
|
next({ path: '/' })
|
|
|
NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
|
|
|
} else {
|
|
|
+ to?.meta?.dashboard ? stopMonitor() : startMonitor()
|
|
|
next()
|
|
|
}
|
|
|
} else if (whiteList.includes(to.path)) {
|
|
|
+ to?.meta?.dashboard ? stopMonitor() : startMonitor()
|
|
|
// in the free login whitelist, go directly
|
|
|
next()
|
|
|
} else {
|
|
|
+ stopMonitor()
|
|
|
// other pages that do not have permission to access are redirected to the login page.
|
|
|
next('/error')
|
|
|
NProgress.done()
|
|
|
@@ -35,3 +44,36 @@ router.afterEach(() => {
|
|
|
// finish progress bar
|
|
|
NProgress.done()
|
|
|
})
|
|
|
+
|
|
|
+let notify = false
|
|
|
+
|
|
|
+function startMonitor () {
|
|
|
+ if (notify) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ notify = true
|
|
|
+ subscribe(['dashboard/event'], onMessage)
|
|
|
+}
|
|
|
+
|
|
|
+function stopMonitor () {
|
|
|
+ if (!notify) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ notify = false
|
|
|
+ unsubscribe(['dashboard/event'], onMessage)
|
|
|
+}
|
|
|
+
|
|
|
+function onMessage (topic, message) {
|
|
|
+ if (topic === 'dashboard/event') {
|
|
|
+ message = message && JSON.parse(message)
|
|
|
+ if (message.level > 1) {
|
|
|
+ Notification({
|
|
|
+ type: 'warning',
|
|
|
+ position: 'bottom-right',
|
|
|
+ duration: 0,
|
|
|
+ title: '紧急告警',
|
|
|
+ message: `${message.deviceName} 于 ${message.happenTime} 发生 ${message.bugName}`
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|