index.vue 5.3 KB

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