index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. let topMenus: RouteRecordRaw[] = [];
  45. routers.value.map((menu) => {
  46. if (menu.hidden !== true) {
  47. // 兼容顶部栏一级菜单内部跳转
  48. if (menu.path === '/') {
  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. let 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. const path = route.path;
  80. let activePath = path;
  81. if (path !== undefined && path.lastIndexOf('/') > 0 && hideList.indexOf(path) === -1) {
  82. const tmpPath = path.substring(1, path.length);
  83. activePath = '/' + tmpPath.substring(0, tmpPath.indexOf('/'));
  84. if (!route.meta.link) {
  85. appStore.toggleSideBarHide(false);
  86. }
  87. } else if (!route.children) {
  88. activePath = path;
  89. appStore.toggleSideBarHide(true);
  90. }
  91. activeRoutes(activePath);
  92. return activePath;
  93. });
  94. const setVisibleNumber = () => {
  95. const width = document.body.getBoundingClientRect().width / 3;
  96. visibleNumber.value = parseInt(String(width / 85));
  97. };
  98. const handleSelect = (key: string) => {
  99. currentIndex.value = key;
  100. const route = routers.value.find((item) => item.path === key);
  101. if (isHttp(key)) {
  102. // http(s):// 路径新窗口打开
  103. window.open(key, '_blank');
  104. } else if (!route || !route.children) {
  105. // 没有子路由路径内部打开
  106. const routeMenu = childrenMenus.value.find((item) => item.path === key);
  107. if (routeMenu && routeMenu.query) {
  108. let query = JSON.parse(routeMenu.query);
  109. router.push({ path: key, query: query });
  110. } else {
  111. router.push({ path: key });
  112. }
  113. appStore.toggleSideBarHide(true);
  114. } else {
  115. // 显示左侧联动菜单
  116. activeRoutes(key);
  117. appStore.toggleSideBarHide(false);
  118. }
  119. };
  120. const activeRoutes = (key: string) => {
  121. let routes: RouteRecordRaw[] = [];
  122. if (childrenMenus.value && childrenMenus.value.length > 0) {
  123. childrenMenus.value.map((item) => {
  124. if (key == item.parentPath || (key == 'index' && '' == item.path)) {
  125. routes.push(item);
  126. }
  127. });
  128. }
  129. if (routes.length > 0) {
  130. permissionStore.setSidebarRouters(routes);
  131. } else {
  132. appStore.toggleSideBarHide(true);
  133. }
  134. return routes;
  135. };
  136. onMounted(() => {
  137. window.addEventListener('resize', setVisibleNumber);
  138. });
  139. onBeforeUnmount(() => {
  140. window.removeEventListener('resize', setVisibleNumber);
  141. });
  142. onMounted(() => {
  143. setVisibleNumber();
  144. });
  145. </script>
  146. <style lang="scss">
  147. .topmenu-container.el-menu--horizontal > .el-menu-item {
  148. float: left;
  149. height: 50px !important;
  150. line-height: 50px !important;
  151. color: #999093 !important;
  152. padding: 0 5px !important;
  153. margin: 0 10px !important;
  154. }
  155. .topmenu-container.el-menu--horizontal > .el-menu-item.is-active,
  156. .el-menu--horizontal > .el-sub-menu.is-active .el-submenu__title {
  157. border-bottom: 2px solid #{'var(--theme)'} !important;
  158. color: #303133;
  159. }
  160. /* sub-menu item */
  161. .topmenu-container.el-menu--horizontal > .el-sub-menu .el-sub-menu__title {
  162. float: left;
  163. height: 50px !important;
  164. line-height: 50px !important;
  165. color: #999093 !important;
  166. padding: 0 5px !important;
  167. margin: 0 10px !important;
  168. }
  169. /* 背景色隐藏 */
  170. .topmenu-container.el-menu--horizontal > .el-menu-item:not(.is-disabled):focus,
  171. .topmenu-container.el-menu--horizontal > .el-menu-item:not(.is-disabled):hover,
  172. .topmenu-container.el-menu--horizontal > .el-submenu .el-submenu__title:hover {
  173. background-color: #ffffff !important;
  174. }
  175. /* 图标右间距 */
  176. .topmenu-container .svg-icon {
  177. margin-right: 4px;
  178. }
  179. </style>