index.vue 5.7 KB

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