index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <!-- layout/components/TopNavbar/index.vue -->
  2. <template>
  3. <div class="top-navbar">
  4. <div class="logo-area">
  5. <img src="@/assets/images/tba-slgc-logo.png" alt="logo"/>
  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 = "/slgc/index";
  137. });
  138. })
  139. .catch(() => {
  140. });
  141. };
  142. return {
  143. topMenus,
  144. userStore,
  145. settingsStore,
  146. isFullscreen,
  147. handleMenuClick,
  148. handleCommand,
  149. };
  150. },
  151. };
  152. </script>
  153. <style scoped>
  154. .top-navbar {
  155. height: 56px;
  156. background: linear-gradient(135deg, #1a6be0, #217ff4);
  157. display: flex;
  158. align-items: center;
  159. justify-content: space-between;
  160. padding: 0 24px;
  161. box-shadow: 0 2px 8px rgba(33, 127, 244, 0.15);
  162. user-select: none;
  163. }
  164. .logo-area {
  165. flex-shrink: 0;
  166. display: flex;
  167. align-items: center;
  168. }
  169. .logo-area img {
  170. height: 45px;
  171. width: auto;
  172. }
  173. .logo-text {
  174. color: #fff;
  175. font-size: 18px;
  176. font-weight: 600;
  177. letter-spacing: 2px;
  178. white-space: nowrap;
  179. }
  180. .menu-area {
  181. flex: 1;
  182. display: flex;
  183. gap: 2px;
  184. margin: 0 16px;
  185. overflow-x: auto;
  186. justify-content: flex-start;
  187. }
  188. .menu-link {
  189. padding: 0 20px;
  190. height: 56px;
  191. line-height: 56px;
  192. color: rgba(255, 255, 255, 0.85);
  193. cursor: pointer;
  194. font-size: 15px;
  195. font-weight: 500;
  196. white-space: nowrap;
  197. flex-shrink: 0;
  198. transition: all 0.2s;
  199. position: relative;
  200. }
  201. .menu-link:hover {
  202. color: #fff;
  203. background: rgba(255, 255, 255, 0.12);
  204. }
  205. .menu-link.active {
  206. color: #fff;
  207. background: rgba(255, 255, 255, 0.18);
  208. }
  209. .menu-link.active::after {
  210. content: '';
  211. position: absolute;
  212. bottom: 0;
  213. left: 50%;
  214. transform: translateX(-50%);
  215. width: 60%;
  216. height: 3px;
  217. background: #fff;
  218. border-radius: 3px 3px 0 0;
  219. }
  220. .right-menu {
  221. display: flex;
  222. align-items: center;
  223. color: #fff;
  224. flex-shrink: 0;
  225. height: 100%;
  226. }
  227. .avatar-container {
  228. margin-right: 0;
  229. padding-right: 0;
  230. }
  231. .avatar-wrapper {
  232. display: flex;
  233. align-items: center;
  234. gap: 6px;
  235. cursor: pointer;
  236. padding: 0 10px;
  237. height: 56px;
  238. max-width: 200px;
  239. transition: background 0.2s;
  240. border-radius: 4px;
  241. }
  242. .avatar-wrapper:hover {
  243. background: rgba(255, 255, 255, 0.12);
  244. }
  245. .user-icon {
  246. width: 16px;
  247. height: 16px;
  248. color: #fff;
  249. flex-shrink: 0;
  250. }
  251. .user-nickname {
  252. font-size: 14px;
  253. color: #fff;
  254. max-width: 120px;
  255. overflow: hidden;
  256. text-overflow: ellipsis;
  257. white-space: nowrap;
  258. flex-shrink: 1;
  259. }
  260. .dropdown-icon {
  261. font-size: 12px;
  262. color: rgba(255, 255, 255, 0.75);
  263. transition: transform 0.3s;
  264. flex-shrink: 0;
  265. }
  266. .avatar-container:hover .dropdown-icon {
  267. transform: rotate(180deg);
  268. }
  269. </style>