index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. const routeMenu = childrenMenus.value.find(item => item.path === key);
  115. if (routeMenu && routeMenu.query) {
  116. let query = JSON.parse(routeMenu.query);
  117. router.push({ path: key, query: query });
  118. } else {
  119. router.push({ path: key });
  120. }
  121. appStore.toggleSideBarHide(true);
  122. } else {
  123. // 显示左侧联动菜单
  124. activeRoutes(key);
  125. appStore.toggleSideBarHide(false);
  126. }
  127. }
  128. function activeRoutes(key) {
  129. let routes = [];
  130. if (childrenMenus.value && childrenMenus.value.length > 0) {
  131. childrenMenus.value.map((item) => {
  132. if (key == item.parentPath || (key == "index" && "" == item.path)) {
  133. routes.push(item);
  134. }
  135. });
  136. }
  137. if(routes.length > 0) {
  138. permissionStore.setSidebarRouters(routes);
  139. } else {
  140. appStore.toggleSideBarHide(true);
  141. }
  142. return routes;
  143. }
  144. onMounted(() => {
  145. window.addEventListener('resize', setVisibleNumber)
  146. })
  147. onBeforeUnmount(() => {
  148. window.removeEventListener('resize', setVisibleNumber)
  149. })
  150. onMounted(() => {
  151. setVisibleNumber()
  152. })
  153. </script>
  154. <style lang="scss">
  155. .topmenu-container.el-menu--horizontal > .el-menu-item {
  156. float: left;
  157. height: 50px !important;
  158. line-height: 50px !important;
  159. color: #999093 !important;
  160. padding: 0 5px !important;
  161. margin: 0 10px !important;
  162. }
  163. .topmenu-container.el-menu--horizontal > .el-menu-item.is-active, .el-menu--horizontal > .el-sub-menu.is-active .el-submenu__title {
  164. border-bottom: 2px solid #{'var(--theme)'} !important;
  165. color: #303133;
  166. }
  167. /* sub-menu item */
  168. .topmenu-container.el-menu--horizontal > .el-sub-menu .el-sub-menu__title {
  169. float: left;
  170. height: 50px !important;
  171. line-height: 50px !important;
  172. color: #999093 !important;
  173. padding: 0 5px !important;
  174. margin: 0 10px !important;
  175. }
  176. </style>