index.vue 5.8 KB

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