UiHeader.vue 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <div id="UiHeader">
  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>
  5. <el-menu-item index="/videoList">设备列表</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>
  8. <el-menu-item style="float: right;" @click="loginout">退出</el-menu-item>
  9. </el-menu>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. name: "UiHeader",
  15. components: { Notification },
  16. data() {
  17. return {
  18. alarmNotify: true,
  19. sseSource: null,
  20. };
  21. },
  22. methods:{
  23. loginout(){
  24. // 删除cookie,回到登录页面
  25. this.$cookies.remove("session");
  26. this.$router.push('/login');
  27. this.sseSource.close();
  28. },
  29. beforeunloadHandler() {
  30. this.sseSource.close();
  31. },
  32. sseControl() {
  33. let that = this;
  34. if (this.alarmNotify) {
  35. console.log("申请SSE推送API调用,浏览器ID: " + this.$browserId);
  36. this.sseSource = new EventSource('/api/emit?browserId=' + this.$browserId);
  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. }
  74. </script>