Blame view

src/layout/components/Navbar.vue 3.21 KB
532def1c   王彪总   新版本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  <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>