index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <el-menu
  3. :default-active="activeMenu"
  4. mode="horizontal"
  5. @select="handleSelect"
  6. >
  7. <template v-for="(item, index) in topMenus">
  8. <el-menu-item :style="{'--theme': theme}" :index="item.path" :key="index" v-if="index < visibleNumber"
  9. ><svg-icon :icon-class="item.meta.icon" />
  10. {{ item.meta.title }}</el-menu-item
  11. >
  12. </template>
  13. <!-- 顶部菜单超出数量折叠 -->
  14. <el-sub-menu :style="{'--theme': theme}" index="more" v-if="topMenus.length > visibleNumber">
  15. <template #title>更多菜单</template>
  16. <template v-for="(item, index) in topMenus">
  17. <el-menu-item
  18. :index="item.path"
  19. :key="index"
  20. v-if="index >= visibleNumber"
  21. ><svg-icon :icon-class="item.meta.icon" />
  22. {{ item.meta.title }}</el-menu-item
  23. >
  24. </template>
  25. </el-sub-menu>
  26. </el-menu>
  27. </template>
  28. <script setup>
  29. import { constantRoutes } from "@/router"
  30. import { isHttp } from '@/utils/validate'
  31. // 顶部栏初始数
  32. const visibleNumber = ref(null);
  33. // 是否为首次加载
  34. const isFrist = ref(null);
  35. // 当前激活菜单的 index
  36. const currentIndex = ref(null);
  37. const store = useStore();
  38. const route = useRoute();
  39. const router = useRouter();
  40. // 主题颜色
  41. const theme = computed(() => store.state.settings.theme);
  42. // 所有的路由信息
  43. const routers = computed(() => store.state.permission.topbarRouters);
  44. // 顶部显示菜单
  45. const topMenus = computed(() => {
  46. let topMenus = [];
  47. routers.value.map((menu) => {
  48. if (menu.hidden !== true) {
  49. // 兼容顶部栏一级菜单内部跳转
  50. if (menu.path === "/") {
  51. topMenus.push(menu.children[0]);
  52. } else {
  53. topMenus.push(menu);
  54. }
  55. }
  56. })
  57. return topMenus;
  58. })
  59. // 设置子路由
  60. const childrenMenus = computed(() => {
  61. let childrenMenus = [];
  62. routers.value.map((router) => {
  63. for (let item in router.children) {
  64. if (router.children[item].parentPath === undefined) {
  65. if(router.path === "/") {
  66. router.children[item].path = "/" + router.children[item].path;
  67. } else {
  68. if(!isHttp(router.children[item].path)) {
  69. router.children[item].path = router.path + "/" + router.children[item].path;
  70. }
  71. }
  72. router.children[item].parentPath = router.path;
  73. }
  74. childrenMenus.push(router.children[item]);
  75. }
  76. })
  77. return constantRoutes.concat(childrenMenus);
  78. })
  79. // 默认激活的菜单
  80. const activeMenu = computed(() => {
  81. const path = route.path;
  82. let activePath = path;
  83. if (path !== undefined && path.lastIndexOf("/") > 0) {
  84. const tmpPath = path.substring(1, path.length);
  85. activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
  86. store.dispatch('app/toggleSideBarHide', false);
  87. } else if ("/index" == path || "" == path) {
  88. if (!isFrist.value) {
  89. isFrist.value = true;
  90. } else {
  91. activePath = "index";
  92. }
  93. store.dispatch('app/toggleSideBarHide', true);
  94. } else if(!route.children) {
  95. activePath = path;
  96. store.dispatch('app/toggleSideBarHide', true);
  97. }
  98. activeRoutes(activePath);
  99. return activePath;
  100. })
  101. function setVisibleNumber() {
  102. const width = document.body.getBoundingClientRect().width / 3;
  103. visibleNumber.value = parseInt(width / 85);
  104. }
  105. function handleSelect(key, keyPath) {
  106. currentIndex.value = key;
  107. const route = routers.value.find(item => item.path === key);
  108. if (isHttp(key)) {
  109. // http(s):// 路径新窗口打开
  110. window.open(key, "_blank");
  111. } else if (!route || !route.children) {
  112. // 没有子路由路径内部打开
  113. router.push({ path: key });
  114. store.dispatch('app/toggleSideBarHide', true);
  115. } else {
  116. // 显示左侧联动菜单
  117. activeRoutes(key);
  118. store.dispatch('app/toggleSideBarHide', false);
  119. }
  120. }
  121. function activeRoutes(key) {
  122. let routes = [];
  123. if (childrenMenus.value && childrenMenus.value.length > 0) {
  124. childrenMenus.value.map((item) => {
  125. if (key == item.parentPath || (key == "index" && "" == item.path)) {
  126. routes.push(item);
  127. }
  128. });
  129. }
  130. if(routes.length > 0) {
  131. store.commit("SET_SIDEBAR_ROUTERS", routes);
  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>