index.vue 5.4 KB

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