Newer
Older
smartwell_front / src / views / layout / components / AppHeader.vue
<template>
  <div :style="{backgroundImage:'url('+headerUrl+')'}" class="app-header">
    <h3>{{ title }}</h3>
    <el-dropdown class="avatar-container" trigger="click">
      <div class="avatar-wrapper">
        <span style="line-height:50px;">欢迎您,{{ name }}</span>
        <!--<img :src="avatar+'?imageView2/1/w/80/h/80'" class="user-avatar">-->
        <i class="el-icon-caret-bottom"/>
      </div>
      <el-dropdown-menu slot="dropdown" class="user-dropdown">
        <!--<router-link class="inlineBlock" to="/">-->
        <!--<el-dropdown-item>-->
        <!--主页-->
        <!--</el-dropdown-item>-->
        <!--</router-link>-->
        <el-dropdown-item>
          <span style="display:block;" @click="resetPwd">修改密码</span>
        </el-dropdown-item>
        <el-dropdown-item divided>
          <span style="display:block;" @click="logout">注销</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
    <reset-pwd v-show="showSetPwd" ref="retPwd"/>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import ResetPwd from '@/views/system/user/resetPwd'
import { getProject } from '@/utils/baseConfig'
import { eventBus } from '@/main'
export default {
  name: 'AppHeader',
  components: { ResetPwd },
  data() {
    return {
      title: getProject().title,
      name: this.$store.getters.name,
      showSetPwd: false,
      socket:null, //websoket对象
      headerUrl: require('@/assets/global_images/header.png')// 头部图片
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar'
    ])
  },
  mounted(){
    this.webSocket()
  },
  beforeDestroy(){
    //断开websocket连接
    if(this.socket){
      this.socket.close()
    }
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('ToggleSideBar')
    },
    logout() {
      this.$store.dispatch('LogOut').then(() => {
        location.reload() // 为了重新实例化vue-router对象 避免bug
      })
    },
    resetPwd() {
      this.showSetPwd = true
      this.$refs.retPwd.initDialog(true)
    },
    // 初始化websoket
    webSocket() {
      const that = this
      if (typeof (WebSocket) === 'undefined') {
        this.$notify({
          title: '提示',
          message: '当前浏览器无法接收实时报警信息,请使用谷歌浏览器或360浏览器极速模式!',
          type: 'warning',
          duration: 0
        })
      } else {
        // 获取token保存到vuex中的用户信息,此处仅适用于本项目,注意删除或修改
        // 实例化socket,这里我把用户名传给了后台,使后台能判断要把消息发给哪个用户,其实也可以后台直接获取用户IP来判断并推送
        const socketUrl = 'ws://10.9.39.8:8080/army/webSocket/' + this.$store.getters.username
        // const socketUrl = 'ws://111.198.10.15:21403/army/webSocket/' + this.$store.getters.username
        // const socketUrl = 'ws://192.168.203.201:8181/' + this.$store.getters.username
        console.log(socketUrl)
        this.socket = new WebSocket(socketUrl)
        // 监听socket打开
        this.socket.onopen = ()=> {
          console.log('浏览器WebSocket已打开')
        }
        // 监听socket消息接收
        this.socket.onmessage = msg=> {
          // 转换为json对象
          const data = JSON.parse(msg.data)
          console.log('收到websocket消息:'+data)
          // that.refreshAlarm(true)
          eventBus.$emit('refresh','refresh')
          that.playAudio()
          that.$notify({
            title: '新报警来了',
            // 这里也可以把返回信息加入到message中显示
            message: data.message,
            type: 'warning',
            onClick: () => {
              that.$router.push({
                path: '/overview',
                params:{refresh: true}
              })
            }
          })
        }
        // 监听socket错误
        this.socket.onerror = function() {
          that.$notify({
            title: '服务器错误',
            message: '无法接收实时报警信息,请检查服务器后重新刷新页面',
            type: 'error',
            duration: 0
          })
        }
        // 监听socket关闭
        this.socket.onclose = function() {
          console.log('WebSocket已关闭')
        }
      }
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-header{
    height:60px;
    background-color:#000000;
    background-repeat: no-repeat;
    -webkit-background-size: 100% 100%;
    background-size: 100% 100%;
    color:white;
  }
  h3{
    margin-left:40px;
    display: inline-block;
    float:left;
  }
  .avatar-container {
    height: 50px;
    display: inline-block;
    float: right;
    z-index:500;
    position: fixed;
    top:5px;
    right: 35px;
    .avatar-wrapper {
      color:#d3dce6;
      cursor: pointer;
      margin-top: 5px;
      position: relative;
      line-height: initial;
      .user-avatar {
        width: 40px;
        height: 40px;
        border-radius: 10px;
      }
      .el-icon-caret-bottom {
        font-size: 12px;
      }
    }
  }
</style>