index.vue 5.3 KB

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