Navbar.vue 4.92 KB
<template>
  <div class="navbar">
    <!-- <hamburger :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" /> -->

    <!-- <breadcrumb class="breadcrumb-container" /> -->
    <Sidebar />
    <div class="right-menu">
      <el-dropdown class="avatar-container" trigger="click">
        <span class="el-dropdown-link">{{userName}}<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item divided @click.native="logout">
            <span style="display:block;">退出登录</span>
          </el-dropdown-item>
          
        </el-dropdown-menu>
      </el-dropdown>
      <span style="color: white; padding-right: 10px"> {{ dateTime }}</span>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import Sidebar from './Sidebar/index.vue'
import { logout } from '@/api/user'
import { getUserName } from "@/utils/auth";
import EventBus from '@/utils/bus'
export default {
  data() {
    return {
      dateTime: '',
   
    }
  },
  components: {
    Breadcrumb,
    Hamburger,
    Sidebar
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar'
    ]),
    userName() {
      return  getUserName()
    },
    
  },
  mounted() {
    this.nowTimes()
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    logout() {
      logout().then(() => {
        EventBus.$emit('cancelWS')
        this.$store.dispatch('user/logout')
        this.$router.push(`/login`)
      })
     
    },
    
    // 获取当前时间函数
    timeFormate (timeStamp) {
      let year = new Date(timeStamp).getFullYear()
      let month = new Date(timeStamp).getMonth() + 1 < 10 ? '0' + (new Date(timeStamp).getMonth() + 1) : new Date(timeStamp).getMonth() + 1
      let date = new Date(timeStamp).getDate() < 10 ? '0' + new Date(timeStamp).getDate() : new Date(timeStamp).getDate()
      let hh = new Date(timeStamp).getHours() < 10 ? '0' + new Date(timeStamp).getHours() : new Date(timeStamp).getHours()
      let mm = new Date(timeStamp).getMinutes() < 10 ? '0' + new Date(timeStamp).getMinutes() : new Date(timeStamp).getMinutes()
      let ss = new Date(timeStamp).getSeconds() < 10? "0" + new Date(timeStamp).getSeconds(): new Date(timeStamp).getSeconds();
      // return year + "年" + month + "月" + date +"日"+" "+hh+":"+mm ;
      this.dateTime = year + '/' + month + '/' + date + ' ' + hh + ':' + mm + ':' + ss
    },
    // 定时器函数
    nowTimes() {
      this.timeFormate(new Date())
      clearInterval(this.timer)
      this.timer = setInterval(this.nowTimes, 1000)
    },
     // 清除定时器,不然会导致页面崩溃
    beforeDestroy () {
      if (this.timer) {
        clearInterval(this.timer)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.navbar {
  overflow: hidden;
  // position: fixed;
    top: 0;
    left: 0;
    z-index: 5;
    width: 100%;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  .log {
    font-size: 24px;
    color: #fff;
    float: left;
  }
  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color:transparent;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .right-menu {
    // float: right;
    height: 100%;
    line-height: 50px;
    position: absolute;
    top: 5px;
    right: 30px;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #fff;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
.el-dropdown-menu__item{
  color:#B2DDFF;
}
.el-popper[x-placement^=bottom] {
    margin-top: 5px;
}
.el-dropdown-menu__item--divided{
  border:none;
  margin-top: 0;
}
.el-dropdown-menu__item--divided:before{
  content: '';
    height: 6px;
    display: none;
    margin: 0;
    background-color: transparent;
}
</style>
<style>
  .el-popper .popper__arrow{
    display: none !important;
  }
  .el-dropdown-menu{
    padding:5px 0;
    background: #2066f9;
    border:none;
  }
  .el-dropdown-menu__item:not(.is-disabled):hover{
    background-color: #1a52c7;
    color: #fff;
  }
</style>