index.vue 5.1 KB

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