UiHeader.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <div id="UiHeader">
  3. <el-menu router :default-active="activeIndex" menu-trigger="click" background-color="#001529" text-color="#fff"
  4. active-text-color="#1890ff" mode="horizontal">
  5. <el-menu-item index="/console">控制台</el-menu-item>
  6. <el-menu-item index="/live">分屏监控</el-menu-item>
  7. <el-menu-item index="/deviceList">国标设备</el-menu-item>
  8. <el-menu-item index="/map">电子地图</el-menu-item>
  9. <el-menu-item index="/pushVideoList">推流列表</el-menu-item>
  10. <el-menu-item index="/streamProxyList">拉流代理</el-menu-item>
  11. <el-menu-item index="/cloudRecord">云端录像</el-menu-item>
  12. <el-menu-item index="/mediaServerManger">节点管理</el-menu-item>
  13. <el-menu-item index="/parentPlatformList/15/1">国标级联</el-menu-item>
  14. <el-menu-item v-if="editUser" index="/userManager">用户管理</el-menu-item>
  15. <!-- <el-submenu index="/setting">-->
  16. <!-- <template slot="title">系统设置</template>-->
  17. <!-- <el-menu-item index="/setting/web">WEB服务</el-menu-item>-->
  18. <!-- <el-menu-item index="/setting/sip">国标服务</el-menu-item>-->
  19. <!-- <el-menu-item index="/setting/media">媒体服务</el-menu-item>-->
  20. <!-- </el-submenu>-->
  21. <!-- <el-menu-item style="float: right;" @click="loginout">退出</el-menu-item>-->
  22. <el-submenu index="" style="float: right;">
  23. <template slot="title">欢迎,{{ username }}</template>
  24. <el-menu-item @click="openDoc">在线文档</el-menu-item>
  25. <el-menu-item>
  26. <el-switch v-model="alarmNotify" inactive-text="报警信息推送" @change="alarmNotifyChannge"></el-switch>
  27. </el-menu-item>
  28. <el-menu-item @click="changePassword">修改密码</el-menu-item>
  29. <el-menu-item @click="loginout">注销</el-menu-item>
  30. </el-submenu>
  31. </el-menu>
  32. <changePasswordDialog ref="changePasswordDialog"></changePasswordDialog>
  33. </div>
  34. </template>
  35. <script>
  36. import changePasswordDialog from '../components/dialog/changePassword.vue'
  37. import userService from '../components/service/UserService'
  38. import {Notification} from 'element-ui';
  39. export default {
  40. name: "UiHeader",
  41. components: {Notification, changePasswordDialog},
  42. data() {
  43. return {
  44. alarmNotify: false,
  45. sseSource: null,
  46. username: userService.getUser().username,
  47. activeIndex: this.$route.path,
  48. editUser: userService.getUser() ? userService.getUser().role.id === 1 : false
  49. };
  50. },
  51. created() {
  52. console.log(JSON.stringify(userService.getUser()))
  53. if (this.$route.path.startsWith("/channelList")) {
  54. this.activeIndex = "/deviceList"
  55. }
  56. },
  57. mounted() {
  58. window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
  59. this.alarmNotify = this.getAlarmSwitchStatus() === "true";
  60. // TODO: 此处延迟连接 sse, 避免 sse 连接时 browserId 还未生成, 后续待优化
  61. setTimeout(() => {
  62. this.sseControl()
  63. }, 3000);
  64. },
  65. methods: {
  66. loginout() {
  67. this.$axios({
  68. method: 'get',
  69. url: "/api/user/logout"
  70. }).then((res) => {
  71. // 删除用户信息,回到登录页面
  72. userService.clearUserInfo()
  73. this.$router.push('/login');
  74. if (this.sseSource != null) {
  75. this.sseSource.close();
  76. }
  77. }).catch((error) => {
  78. console.error("登出失败")
  79. console.error(error)
  80. });
  81. },
  82. changePassword() {
  83. this.$refs.changePasswordDialog.openDialog()
  84. },
  85. openDoc() {
  86. console.log(process.env.BASE_API)
  87. window.open(!!process.env.BASE_API ? process.env.BASE_API + "/doc.html" : "/doc.html")
  88. },
  89. beforeunloadHandler() {
  90. this.sseSource.close();
  91. },
  92. alarmNotifyChannge() {
  93. this.setAlarmSwitchStatus()
  94. this.sseControl()
  95. },
  96. sseControl() {
  97. let that = this;
  98. if (this.alarmNotify) {
  99. console.log("申请SSE推送API调用,浏览器ID: " + this.$browserId);
  100. this.sseSource = new EventSource('/api/emit?browserId=' + this.$browserId);
  101. this.sseSource.addEventListener('message', function (evt) {
  102. that.$notify({
  103. title: '报警信息',
  104. dangerouslyUseHTMLString: true,
  105. message: evt.data,
  106. type: 'warning',
  107. position: 'bottom-right',
  108. duration: 3000
  109. });
  110. console.log("收到信息:" + evt.data);
  111. });
  112. this.sseSource.addEventListener('open', function (e) {
  113. console.log("SSE连接打开.");
  114. }, false);
  115. this.sseSource.addEventListener('error', function (e) {
  116. if (e.target.readyState == EventSource.CLOSED) {
  117. console.log("SSE连接关闭");
  118. } else {
  119. console.log(e.target.readyState);
  120. }
  121. }, false);
  122. } else {
  123. if (this.sseSource != null) {
  124. this.sseSource.removeEventListener('open', null);
  125. this.sseSource.removeEventListener('message', null);
  126. this.sseSource.removeEventListener('error', null);
  127. this.sseSource.close();
  128. }
  129. }
  130. },
  131. getAlarmSwitchStatus() {
  132. if (localStorage.getItem("alarmSwitchStatus") == null) {
  133. localStorage.setItem("alarmSwitchStatus", false);
  134. }
  135. return localStorage.getItem("alarmSwitchStatus");
  136. },
  137. setAlarmSwitchStatus() {
  138. localStorage.setItem("alarmSwitchStatus", this.alarmNotify);
  139. }
  140. },
  141. destroyed() {
  142. window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
  143. if (this.sseSource != null) {
  144. this.sseSource.removeEventListener('open', null);
  145. this.sseSource.removeEventListener('message', null);
  146. this.sseSource.removeEventListener('error', null);
  147. this.sseSource.close();
  148. }
  149. },
  150. }
  151. </script>
  152. <style>
  153. #UiHeader .el-switch__label {
  154. color: white;
  155. }
  156. .el-menu--popup .el-menu-item .el-switch .el-switch__label {
  157. color: white !important;
  158. }
  159. #UiHeader .el-switch__label.is-active {
  160. color: #409EFF;
  161. }
  162. #UiHeader .el-menu-item.is-active {
  163. color: #fff !important;
  164. background-color: #1890ff !important;
  165. }
  166. </style>