index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <el-menu :default-active="activeMenu" mode="horizontal" :ellipsis="false" @select="handleSelect">
  3. <template v-for="(item, index) in topMenus">
  4. <el-menu-item v-if="index < visibleNumber" :key="index" :style="{ '--theme': theme }" :index="item.path"
  5. ><svg-icon v-if="item.meta && item.meta.icon && item.meta.icon !== '#'" :icon-class="item.meta ? item.meta.icon : ''" />
  6. {{ item.meta?.title }}</el-menu-item
  7. >
  8. </template>
  9. <!-- 顶部菜单超出数量折叠 -->
  10. <el-sub-menu v-if="topMenus.length > visibleNumber" :style="{ '--theme': theme }" index="more">
  11. <template #title>更多菜单</template>
  12. <template v-for="(item, index) in topMenus">
  13. <el-menu-item v-if="index >= visibleNumber" :key="index" :index="item.path"
  14. ><svg-icon :icon-class="item.meta ? item.meta.icon : ''" /> {{ item.meta?.title }}</el-menu-item
  15. >
  16. </template>
  17. </el-sub-menu>
  18. </el-menu>
  19. </template>
  20. <script setup lang="ts">
  21. import { constantRoutes } from '@/router';
  22. import { isHttp } from '@/utils/validate';
  23. import { useAppStore } from '@/store/modules/app';
  24. import { useSettingsStore } from '@/store/modules/settings';
  25. import { usePermissionStore } from '@/store/modules/permission';
  26. import { RouteRecordRaw } from 'vue-router';
  27. // 顶部栏初始数
  28. const visibleNumber = ref<number>(-1);
  29. // 当前激活菜单的 index
  30. const currentIndex = ref<string>();
  31. // 隐藏侧边栏路由
  32. const hideList = ['/index', '/user/profile'];
  33. const appStore = useAppStore();
  34. const settingsStore = useSettingsStore();
  35. const permissionStore = usePermissionStore();
  36. const route = useRoute();
  37. const router = useRouter();
  38. // 主题颜色
  39. const theme = computed(() => settingsStore.theme);
  40. // 所有的路由信息
  41. const routers = computed(() => permissionStore.getTopbarRoutes());
  42. // 顶部显示菜单
  43. const topMenus = computed(() => {
  44. const topMenus: RouteRecordRaw[] = [];
  45. routers.value.map((menu) => {
  46. if (menu.hidden !== true) {
  47. // 兼容顶部栏一级菜单内部跳转
  48. if (menu.path === '/' && menu.children) {
  49. topMenus.push(menu.children ? menu.children[0] : menu);
  50. } else {
  51. topMenus.push(menu);
  52. }
  53. }
  54. });
  55. return topMenus;
  56. });
  57. // 设置子路由
  58. const childrenMenus = computed(() => {
  59. const childrenMenus: RouteRecordRaw[] = [];
  60. routers.value.map((router) => {
  61. router.children?.forEach((item) => {
  62. if (item.parentPath === undefined) {
  63. if (router.path === '/') {
  64. item.path = '/' + item.path;
  65. } else {
  66. if (!isHttp(item.path)) {
  67. item.path = router.path + '/' + item.path;
  68. }
  69. }
  70. item.parentPath = router.path;
  71. }
  72. childrenMenus.push(item);
  73. });
  74. });
  75. return constantRoutes.concat(childrenMenus);
  76. });
  77. // 默认激活的菜单
  78. const activeMenu = computed(() => {
  79. let path = route.path;
  80. if (path === '/index') {
  81. path = '/system/user';
  82. }
  83. let activePath = path;
  84. if (path !== undefined && path.lastIndexOf('/') > 0 && hideList.indexOf(path) === -1) {
  85. const tmpPath = path.substring(1, path.length);
  86. if (!route.meta.link) {
  87. activePath = '/' + tmpPath.substring(0, tmpPath.indexOf('/'));
  88. appStore.toggleSideBarHide(false);
  89. }
  90. } else if (!route.children) {
  91. activePath = path;
  92. appStore.toggleSideBarHide(true);
  93. }
  94. activeRoutes(activePath);
  95. return activePath;
  96. });
  97. const setVisibleNumber = () => {
  98. const width = document.body.getBoundingClientRect().width / 3;
  99. visibleNumber.value = parseInt(String(width / 85));
  100. };
  101. const handleSelect = (key: string) => {
  102. currentIndex.value = key;
  103. const route = routers.value.find((item) => item.path === key);
  104. if (isHttp(key)) {
  105. // http(s):// 路径新窗口打开
  106. window.open(key, '_blank');
  107. } else if (!route || !route.children) {
  108. // 没有子路由路径内部打开
  109. const routeMenu = childrenMenus.value.find((item) => item.path === key);
  110. if (routeMenu && routeMenu.query) {
  111. const query = JSON.parse(routeMenu.query);
  112. router.push({ path: key, query: query });
  113. } else {
  114. router.push({ path: key });
  115. }
  116. appStore.toggleSideBarHide(true);
  117. } else {
  118. // 显示左侧联动菜单
  119. activeRoutes(key);
  120. appStore.toggleSideBarHide(false);
  121. }
  122. };
  123. const activeRoutes = (key: string) => {
  124. const routes: RouteRecordRaw[] = [];
  125. if (childrenMenus.value && childrenMenus.value.length > 0) {
  126. childrenMenus.value.map((item) => {
  127. if (key == item.parentPath || (key == 'index' && '' == item.path)) {
  128. routes.push(item);
  129. }
  130. });
  131. }
  132. if (routes.length > 0) {
  133. permissionStore.setSidebarRouters(routes);
  134. } else {
  135. appStore.toggleSideBarHide(true);
  136. }
  137. return routes;
  138. };
  139. onMounted(() => {
  140. window.addEventListener('resize', setVisibleNumber);
  141. });
  142. onBeforeUnmount(() => {
  143. window.removeEventListener('resize', setVisibleNumber);
  144. });
  145. onMounted(() => {
  146. setVisibleNumber();
  147. });
  148. </script>
  149. <style lang="scss">
  150. .topmenu-container.el-menu--horizontal > .el-menu-item {
  151. float: left;
  152. height: 50px !important;
  153. line-height: 50px !important;
  154. color: #999093 !important;
  155. padding: 0 5px !important;
  156. margin: 0 10px !important;
  157. }
  158. .topmenu-container.el-menu--horizontal > .el-menu-item.is-active,
  159. .el-menu--horizontal > .el-sub-menu.is-active .el-submenu__title {
  160. border-bottom: 2px solid #{'var(--theme)'} !important;
  161. color: #303133;
  162. }
  163. /* sub-menu item */
  164. .topmenu-container.el-menu--horizontal > .el-sub-menu .el-sub-menu__title {
  165. float: left;
  166. height: 50px !important;
  167. line-height: 50px !important;
  168. color: #999093 !important;
  169. padding: 0 5px !important;
  170. margin: 0 10px !important;
  171. }
  172. /* 背景色隐藏 */
  173. .topmenu-container.el-menu--horizontal > .el-menu-item:not(.is-disabled):focus,
  174. .topmenu-container.el-menu--horizontal > .el-menu-item:not(.is-disabled):hover,
  175. .topmenu-container.el-menu--horizontal > .el-submenu .el-submenu__title:hover {
  176. background-color: #ffffff !important;
  177. }
  178. /* 图标右间距 */
  179. .topmenu-container .svg-icon {
  180. margin-right: 4px;
  181. }
  182. </style>