| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <!-- layout/components/TopNavbar/index.vue -->
- <template>
- <div class="top-navbar">
- <div class="logo-area">
- <span class="logo-text">若依管理系统</span>
- </div>
- <div class="menu-area">
- <!-- 首页菜单 -->
- <div
- :class="[
- 'menu-link',
- { active: activeMenu === '/' || activeMenu === '/index' },
- ]"
- @click="handleMenuClick('/index')"
- >
- 首页
- </div>
- <!-- 动态菜单 -->
- <div
- v-for="item in topMenus"
- :key="item.path"
- :class="['menu-link', { active: activeMenu === item.path }]"
- @click="handleMenuClick(item.path)"
- >
- {{ item.meta?.title }}
- </div>
- </div>
- <div class="right-menu">
- <el-dropdown
- @command="handleCommand"
- class="avatar-container"
- trigger="hover"
- >
- <div class="avatar-wrapper">
- <!-- 用户图标 -->
- <svg-icon icon-class="user" class="user-icon" />
- <!-- 用户名 - 添加溢出处理 -->
- <span
- class="user-nickname"
- :title="userStore.nickName || userStore.name"
- >
- {{ userStore.nickName || userStore.name }}
- </span>
- <!-- 下拉箭头 -->
- <el-icon class="dropdown-icon">
- <ArrowDown />
- </el-icon>
- </div>
- <template #dropdown>
- <el-dropdown-menu>
- <router-link to="/user/profile">
- <el-dropdown-item>个人中心</el-dropdown-item>
- </router-link>
- <el-dropdown-item
- command="setLayout"
- v-if="settingsStore.showSettings"
- >
- <span>布局设置</span>
- </el-dropdown-item>
- <el-dropdown-item command="lockScreen">
- <span>锁定屏幕</span>
- </el-dropdown-item>
- <el-dropdown-item divided command="logout">
- <span>退出登录</span>
- </el-dropdown-item>
- </el-dropdown-menu>
- </template>
- </el-dropdown>
- </div>
- </div>
- </template>
- <script>
- import { computed, ref } from "vue";
- import { useRoute, useRouter } from "vue-router";
- import { ElMessageBox } from "element-plus";
- import { ArrowDown } from "@element-plus/icons-vue";
- import usePermissionStore from "@/store/modules/permission";
- import useUserStore from "@/store/modules/user";
- import useSettingsStore from "@/store/modules/settings";
- import useLockStore from "@/store/modules/lock";
- export default {
- name: "TopNavbar",
- components: {
- ArrowDown,
- },
- props: {
- activeMenu: {
- type: String,
- default: "",
- },
- },
- emits: ["menu-click", "setLayout"],
- setup(props, { emit }) {
- const route = useRoute();
- const router = useRouter();
- const permissionStore = usePermissionStore();
- const userStore = useUserStore();
- const settingsStore = useSettingsStore();
- const lockStore = useLockStore();
- const isFullscreen = ref(false);
- const topMenus = computed(() => {
- const routes = permissionStore.topbarRouters || [];
- return routes.filter((item) => item.meta?.title && !item.hidden);
- });
- const handleMenuClick = (path) => {
- emit("menu-click", path);
- };
- const handleCommand = (command) => {
- switch (command) {
- case "setLayout":
- emit("setLayout");
- break;
- case "lockScreen":
- lockScreen();
- break;
- case "logout":
- logout();
- break;
- default:
- break;
- }
- };
- const lockScreen = () => {
- const currentPath = route.fullPath;
- lockStore.lockScreen(currentPath);
- router.push("/lock");
- };
- const logout = () => {
- ElMessageBox.confirm("确定注销并退出系统吗?", "提示", {
- confirmButtonText: "确定",
- cancelButtonText: "取消",
- type: "warning",
- })
- .then(() => {
- userStore.logOut().then(() => {
- location.href = "/index";
- });
- })
- .catch(() => {});
- };
- return {
- topMenus,
- userStore,
- settingsStore,
- isFullscreen,
- handleMenuClick,
- handleCommand,
- };
- },
- };
- </script>
- <style scoped>
- .top-navbar {
- height: 60px;
- background: #217ff4;
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 0 30px;
- box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
- }
- .logo-area {
- width: 260px;
- flex-shrink: 0;
- }
- .logo-text {
- color: #fff;
- font-size: 20px;
- font-weight: bold;
- }
- .menu-area {
- flex: 1;
- display: flex;
- gap: 8px;
- margin: 0 30px;
- overflow-x: auto;
- }
- .menu-link {
- padding: 0 24px;
- height: 60px;
- line-height: 60px;
- color: #fff;
- cursor: pointer;
- font-size: 18px;
- font-weight: 500;
- letter-spacing: 1px;
- white-space: nowrap;
- flex-shrink: 0;
- }
- .menu-link:hover {
- background: rgba(255, 255, 255, 0.15);
- }
- .menu-link.active {
- background: rgba(255, 255, 255, 0.25);
- }
- .right-menu {
- display: flex;
- align-items: center;
- color: #fff;
- flex-shrink: 0;
- height: 100%;
- }
- .avatar-container {
- margin-right: 0;
- padding-right: 0;
- }
- .avatar-wrapper {
- display: flex;
- align-items: center;
- gap: 8px;
- cursor: pointer;
- padding: 0 12px;
- height: 60px;
- max-width: 200px;
- }
- .avatar-wrapper:hover {
- background: rgba(255, 255, 255, 0.15);
- }
- .user-icon {
- width: 18px;
- height: 18px;
- color: #fff;
- flex-shrink: 0;
- }
- .user-nickname {
- font-size: 14px;
- color: #fff;
- max-width: 120px;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- flex-shrink: 1;
- }
- .dropdown-icon {
- font-size: 14px;
- color: #fff;
- transition: transform 0.3s;
- flex-shrink: 0;
- }
- .avatar-container:hover .dropdown-icon {
- transform: rotate(180deg);
- }
- </style>
|