header.vue 2.83 KB
<script setup lang="ts">
/**
 * 公共布局头部组件
 * @module Header
 */
import { getCurrentInstance, onMounted, reactive, computed } from "vue";
import * as store from "@/pinia/index";
import { ElMessageBox } from "element-plus";
import { Fold, Expand } from "@element-plus/icons-vue";
import styles from "@/style/global.module.scss";
// TIP 获取全局变量,暂时只有坏图片占位图
const {
  appContext: {
    config: { globalProperties },
  },
} = getCurrentInstance();
/**
 * 侧边菜单collapse状态切换
 */
const toggleMenu = () => {
  store.family.appStore().toggleCollapse();
};
/**
 * 退出登录
 */
const handleLogout = () => {
  ElMessageBox.confirm("确认退出登录吗?", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    store.family.loginStore().removeToken();
  }).catch(error => {
    console.log(error)
  })
};
let asyncData = reactive({
  userInfo: "",
  isCollapse: null,
});
onMounted(() => {
  asyncData.userInfo = store.family.loginStore().userName;
  asyncData.isCollapse = computed(() => {
    return store.family.appStore().isCollapse
  })
});
</script>

<template>
  <div class="header-container clearfix">
    <!-- TIP logo -->
    <div
      class="left-logo"
      :class="asyncData.isCollapse ? 'thinLogo' : 'fatLogo'"
    >
      <router-link to="/">
        <el-image
          :src="globalProperties.$cnliveLogo"
          fit="contain"
          style="height: 40px"
        ></el-image>
      </router-link>
    </div>
    <!-- TIP logo结束 -->
    <!-- TIP 控制菜单collapse的按钮 -->
    <div class="menu-collapse-button" @click="toggleMenu">
      <Expand class="font-style" v-if="asyncData.isCollapse" />
      <Fold class="font-style" v-if="!asyncData.isCollapse" />
    </div>
    <!-- TIP 控制菜单collapse的按钮结束 -->
    <!-- TIP 功能键 -->
    <div class="right-buttons">
      <el-button type="text"><router-link to="/">首页</router-link></el-button>
      <el-button type="text"
        >当前用户:<b>{{ asyncData.userInfo? asyncData.userInfo: '超级管理员' }}</b></el-button
      >
      <el-button type="text" @click="handleLogout">退出</el-button>
    </div>
    <!-- TIP 功能键结束 -->
  </div>
</template>

<style scoped lang="scss">
.header-container {
  padding: 0 20px 0 0;
  height: 60px;
  top: 0;
  position: fixed;
  z-index: 1000;
  width: 100%;
  line-height: 60px;
  background-color: white;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  .left-logo {
    float: left;
    margin-top: 10px;
    text-align: center;
  }
  .menu-collapse-button {
    float: left;
    margin-left: 20px;
    cursor: hand;
    cursor: pointer;
    .font-style {
      width: 1.2em;
      height: 1.2em;
      margin-right: 8px;
      margin-top: 20px;
    }
  }
  .right-buttons {
    float: right;
  }
}
</style>