Navbar.vue 3.21 KB
<script setup lang="ts">
import { useFullscreen } from '@vueuse/core'
import { ElMessage, ElMessageBox } from 'element-plus'
import Breadcrumb from './Breadcrumb.vue'
import { useAppStore, useUserStore } from '@/store'
import { useRouter } from 'vue-router'
import { setLocale } from '@/locales'
import { LOGIN_PATH } from '@/config'

const appStore = useAppStore()
const userStore = useUserStore()
const router = useRouter()

const { isFullscreen, toggle: toggleFullscreen } = useFullscreen()

const nickname = computed(() => userStore.userInfo?.nickname || userStore.userInfo?.username || '未登录')

function handleLocaleChange(locale: 'zh-CN' | 'en') {
  appStore.setLocale(locale)
  setLocale(locale)
}

async function handleLogout() {
  await ElMessageBox.confirm('确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await userStore.logout()
  ElMessage.success('已退出登录')
  router.push(LOGIN_PATH)
}

function handleCommand(command: string) {
  if (command === 'logout') handleLogout()
  else if (command === 'profile') ElMessage.info('个人中心将在 P1 阶段提供')
}
</script>

<template>
  <div class="navbar">
    <div class="navbar-left">
      <Icon
        :icon="appStore.sidebarCollapsed ? 'ep:expand' : 'ep:fold'"
        :size="20"
        class="navbar-icon"
        @click="appStore.toggleSidebar()"
      />
      <Breadcrumb />
    </div>

    <div class="navbar-right">
      <el-tooltip :content="isFullscreen ? '退出全屏' : '全屏'">
        <Icon
          :icon="isFullscreen ? 'ep:close' : 'ep:full-screen'"
          :size="18"
          class="navbar-icon"
          @click="toggleFullscreen()"
        />
      </el-tooltip>

      <el-dropdown class="navbar-icon" @command="handleLocaleChange">
        <span class="nav-text">
          {{ appStore.locale === 'zh-CN' ? '中文' : 'EN' }}
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="zh-CN">简体中文</el-dropdown-item>
            <el-dropdown-item command="en">English</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>

      <el-dropdown @command="handleCommand">
        <span class="nav-user">
          <el-avatar :size="26" class="mr-2">{{ nickname.charAt(0) }}</el-avatar>
          <span class="nav-text">{{ nickname }}</span>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="profile">个人中心</el-dropdown-item>
            <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<style scoped lang="scss">
.navbar {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
}

.navbar-left,
.navbar-right {
  display: flex;
  align-items: center;
  gap: 14px;
}

.navbar-icon {
  cursor: pointer;
  color: $text-color-regular;

  &:hover {
    color: $primary-color;
  }
}

.nav-text {
  font-size: 13px;
  color: $text-color-regular;
  cursor: pointer;
}

.nav-user {
  display: flex;
  align-items: center;
  cursor: pointer;
}
</style>