index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <!-- layout/components/TopNavbar/index.vue -->
  2. <template>
  3. <div class="top-navbar">
  4. <div class="logo-area">
  5. <span class="logo-text">{{ title }}</span>
  6. </div>
  7. <div class="menu-area">
  8. <!-- 首页菜单 -->
  9. <div
  10. :class="[
  11. 'menu-link',
  12. { active: activeMenu === '/' || activeMenu === '/index' },
  13. ]"
  14. @click="handleMenuClick('/index')"
  15. >
  16. 首页
  17. </div>
  18. <!-- 动态菜单 -->
  19. <div
  20. v-for="item in topMenus"
  21. :key="item.path"
  22. :class="['menu-link', { active: activeMenu === item.path }]"
  23. @click="handleMenuClick(item.path)"
  24. >
  25. {{ item.meta?.title }}
  26. </div>
  27. </div>
  28. <div class="right-menu">
  29. <el-dropdown
  30. @command="handleCommand"
  31. class="avatar-container"
  32. trigger="hover"
  33. >
  34. <div class="avatar-wrapper">
  35. <!-- 用户图标 -->
  36. <svg-icon icon-class="user" class="user-icon" />
  37. <!-- 用户名 - 添加溢出处理 -->
  38. <span
  39. class="user-nickname"
  40. :title="userStore.nickName || userStore.name"
  41. >
  42. {{ userStore.nickName || userStore.name }}
  43. </span>
  44. <!-- 下拉箭头 -->
  45. <el-icon class="dropdown-icon">
  46. <ArrowDown />
  47. </el-icon>
  48. </div>
  49. <template #dropdown>
  50. <el-dropdown-menu>
  51. <router-link to="/user/profile">
  52. <el-dropdown-item>个人中心</el-dropdown-item>
  53. </router-link>
  54. <el-dropdown-item command="lockScreen">
  55. <span>锁定屏幕</span>
  56. </el-dropdown-item>
  57. <el-dropdown-item divided command="logout">
  58. <span>退出登录</span>
  59. </el-dropdown-item>
  60. </el-dropdown-menu>
  61. </template>
  62. </el-dropdown>
  63. </div>
  64. </div>
  65. </template>
  66. <script>
  67. import { computed, ref } from "vue";
  68. import { useRoute, useRouter } from "vue-router";
  69. import { ElMessageBox } from "element-plus";
  70. import { ArrowDown } from "@element-plus/icons-vue";
  71. import usePermissionStore from "@/store/modules/permission";
  72. import useUserStore from "@/store/modules/user";
  73. import useSettingsStore from "@/store/modules/settings";
  74. import useLockStore from "@/store/modules/lock";
  75. export default {
  76. name: "TopNavbar",
  77. components: {
  78. ArrowDown,
  79. },
  80. props: {
  81. activeMenu: {
  82. type: String,
  83. default: "",
  84. },
  85. },
  86. emits: ["menu-click", "setLayout"],
  87. setup(props, { emit }) {
  88. const title = import.meta.env.VITE_APP_TITLE
  89. const route = useRoute();
  90. const router = useRouter();
  91. const permissionStore = usePermissionStore();
  92. const userStore = useUserStore();
  93. const settingsStore = useSettingsStore();
  94. const lockStore = useLockStore();
  95. const isFullscreen = ref(false);
  96. const topMenus = computed(() => {
  97. const routes = permissionStore.topbarRouters || [];
  98. return routes.filter((item) => item.meta?.title && !item.hidden);
  99. });
  100. const handleMenuClick = (path) => {
  101. emit("menu-click", path);
  102. };
  103. const handleCommand = (command) => {
  104. switch (command) {
  105. case "setLayout":
  106. emit("setLayout");
  107. break;
  108. case "lockScreen":
  109. lockScreen();
  110. break;
  111. case "logout":
  112. logout();
  113. break;
  114. default:
  115. break;
  116. }
  117. };
  118. const lockScreen = () => {
  119. const currentPath = route.fullPath;
  120. lockStore.lockScreen(currentPath);
  121. router.push("/lock");
  122. };
  123. const logout = () => {
  124. ElMessageBox.confirm("确定注销并退出系统吗?", "提示", {
  125. confirmButtonText: "确定",
  126. cancelButtonText: "取消",
  127. type: "warning",
  128. })
  129. .then(() => {
  130. userStore.logOut().then(() => {
  131. location.href = "/index";
  132. });
  133. })
  134. .catch(() => {});
  135. };
  136. return {
  137. title,
  138. topMenus,
  139. userStore,
  140. settingsStore,
  141. isFullscreen,
  142. handleMenuClick,
  143. handleCommand,
  144. };
  145. },
  146. };
  147. </script>
  148. <style scoped>
  149. .top-navbar {
  150. height: 60px;
  151. background: #217ff4;
  152. display: flex;
  153. align-items: center;
  154. justify-content: space-between;
  155. padding: 0 30px;
  156. box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  157. }
  158. .logo-area {
  159. width: 260px;
  160. flex-shrink: 0;
  161. }
  162. .logo-text {
  163. color: #fff;
  164. font-size: 20px;
  165. font-weight: bold;
  166. }
  167. .menu-area {
  168. flex: 1;
  169. display: flex;
  170. gap: 8px;
  171. margin: 0 30px;
  172. overflow-x: auto;
  173. }
  174. .menu-link {
  175. padding: 0 24px;
  176. height: 60px;
  177. line-height: 60px;
  178. color: #fff;
  179. cursor: pointer;
  180. font-size: 18px;
  181. font-weight: 500;
  182. letter-spacing: 1px;
  183. white-space: nowrap;
  184. flex-shrink: 0;
  185. }
  186. .menu-link:hover {
  187. background: rgba(255, 255, 255, 0.15);
  188. }
  189. .menu-link.active {
  190. background: rgba(255, 255, 255, 0.25);
  191. }
  192. .right-menu {
  193. display: flex;
  194. align-items: center;
  195. color: #fff;
  196. flex-shrink: 0;
  197. height: 100%;
  198. }
  199. .avatar-container {
  200. margin-right: 0;
  201. padding-right: 0;
  202. }
  203. .avatar-wrapper {
  204. display: flex;
  205. align-items: center;
  206. gap: 8px;
  207. cursor: pointer;
  208. padding: 0 12px;
  209. height: 60px;
  210. max-width: 200px;
  211. }
  212. .avatar-wrapper:hover {
  213. background: rgba(255, 255, 255, 0.15);
  214. }
  215. .user-icon {
  216. width: 18px;
  217. height: 18px;
  218. color: #fff;
  219. flex-shrink: 0;
  220. }
  221. .user-nickname {
  222. font-size: 14px;
  223. color: #fff;
  224. max-width: 120px;
  225. overflow: hidden;
  226. text-overflow: ellipsis;
  227. white-space: nowrap;
  228. flex-shrink: 1;
  229. }
  230. .dropdown-icon {
  231. font-size: 14px;
  232. color: #fff;
  233. transition: transform 0.3s;
  234. flex-shrink: 0;
  235. }
  236. .avatar-container:hover .dropdown-icon {
  237. transform: rotate(180deg);
  238. }
  239. </style>