Commit 5d031b3df29e3c4a1ce2ecab20dcbc2a6ffc5e35
1 parent
6811b8dc
增加报警信息推送请求和页面通知发布
Showing
1 changed file
with
54 additions
and
4 deletions
web_src/src/components/UiHeader.vue
| @@ -3,7 +3,8 @@ | @@ -3,7 +3,8 @@ | ||
| 3 | <el-menu router :default-active="this.$route.path" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" mode="horizontal"> | 3 | <el-menu router :default-active="this.$route.path" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" mode="horizontal"> |
| 4 | <el-menu-item index="/">控制台</el-menu-item> | 4 | <el-menu-item index="/">控制台</el-menu-item> |
| 5 | <el-menu-item index="/videoList">设备列表</el-menu-item> | 5 | <el-menu-item index="/videoList">设备列表</el-menu-item> |
| 6 | - <el-menu-item index="/parentPlatformList/15/1">国标级联</el-menu-item> | 6 | + <el-menu-item index="/parentPlatformList/15/1">国标级联</el-menu-item> |
| 7 | + <el-switch v-model="alarmNotify" active-text="报警信息推送" style="display: block float: right" @change="sseControl"></el-switch> | ||
| 7 | <el-menu-item style="float: right;" @click="loginout">退出</el-menu-item> | 8 | <el-menu-item style="float: right;" @click="loginout">退出</el-menu-item> |
| 8 | </el-menu> | 9 | </el-menu> |
| 9 | </div> | 10 | </div> |
| @@ -12,14 +13,63 @@ | @@ -12,14 +13,63 @@ | ||
| 12 | <script> | 13 | <script> |
| 13 | export default { | 14 | export default { |
| 14 | name: "UiHeader", | 15 | name: "UiHeader", |
| 16 | + components: { Notification }, | ||
| 17 | + data() { | ||
| 18 | + return { | ||
| 19 | + alarmNotify: true, | ||
| 20 | + sseSource: null, | ||
| 21 | + }; | ||
| 22 | + }, | ||
| 15 | methods:{ | 23 | methods:{ |
| 16 | - | ||
| 17 | loginout(){ | 24 | loginout(){ |
| 18 | // 删除cookie,回到登录页面 | 25 | // 删除cookie,回到登录页面 |
| 19 | this.$cookies.remove("session"); | 26 | this.$cookies.remove("session"); |
| 20 | this.$router.push('/login'); | 27 | this.$router.push('/login'); |
| 28 | + this.sseSource.close(); | ||
| 29 | + }, | ||
| 30 | + beforeunloadHandler() { | ||
| 31 | + this.sseSource.close(); | ||
| 21 | }, | 32 | }, |
| 22 | - } | ||
| 23 | -} | 33 | + sseControl() { |
| 34 | + let that = this; | ||
| 35 | + if (this.alarmNotify) { | ||
| 36 | + this.sseSource = new EventSource('/api/emit'); | ||
| 37 | + this.sseSource.addEventListener('message', function(evt) { | ||
| 38 | + that.$notify({ | ||
| 39 | + title: '收到报警信息', | ||
| 40 | + dangerouslyUseHTMLString: true, | ||
| 41 | + message: evt.data, | ||
| 42 | + type: 'warning' | ||
| 43 | + }); | ||
| 44 | + console.log("收到信息:" + evt.data); | ||
| 45 | + }); | ||
| 46 | + this.sseSource.addEventListener('open', function(e) { | ||
| 47 | + console.log("SSE连接打开."); | ||
| 48 | + }, false); | ||
| 49 | + this.sseSource.addEventListener('error', function(e) { | ||
| 50 | + if (e.target.readyState == EventSource.CLOSED) { | ||
| 51 | + console.log("SSE连接关闭"); | ||
| 52 | + } else { | ||
| 53 | + console.log(e.target.readyState); | ||
| 54 | + } | ||
| 55 | + }, false); | ||
| 56 | + } else { | ||
| 57 | + this.sseSource.removeEventListener('open', null); | ||
| 58 | + this.sseSource.removeEventListener('message', null); | ||
| 59 | + this.sseSource.removeEventListener('error', null); | ||
| 60 | + this.sseSource.close(); | ||
| 61 | + } | ||
| 62 | + } | ||
| 63 | + }, | ||
| 64 | + mounted() { | ||
| 65 | + window.addEventListener('beforeunload', e => this.beforeunloadHandler(e)) | ||
| 66 | + // window.addEventListener('unload', e => this.unloadHandler(e)) | ||
| 67 | + this.sseControl(); | ||
| 68 | + }, | ||
| 69 | + destroyed() { | ||
| 70 | + window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e)) | ||
| 71 | + // window.removeEventListener('unload', e => this.unloadHandler(e)) | ||
| 72 | + }, | ||
| 73 | + } | ||
| 24 | 74 | ||
| 25 | </script> | 75 | </script> |