index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <el-menu :default-active="activeMenu" mode="horizontal" :ellipsis="false" @select="handleSelect">
  3. <template v-for="(item, index) in topMenus">
  4. <el-menu-item v-if="index < visibleNumber" :key="index" :style="{ '--theme': theme }" :index="item.path"
  5. >
  6. <svg-icon v-if="item.meta && item.meta.icon && item.meta.icon !== '#'" :icon-class="item.meta ? item.meta.icon : ''" />
  7. {{ item.meta?.title }}</el-menu-item
  8. >
  9. </template>
  10. <!-- 顶部菜单超出数量折叠 -->
  11. <el-sub-menu v-if="topMenus.length > visibleNumber" :style="{ '--theme': theme }" index="more">
  12. <template #title>更多菜单</template>
  13. <template v-for="(item, index) in topMenus">
  14. <el-menu-item v-if="index >= visibleNumber" :key="index" :index="item.path"
  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 { RouteRecordRaw } 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.getTopbarRoutes());
  43. // 顶部显示菜单
  44. const topMenus = computed(() => {
  45. let topMenus: RouteRecordRaw[] = [];
  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: RouteRecordRaw[] = [];
  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. let path = route.path;
  81. if (path === '/index') {
  82. path = '/system/user';
  83. }
  84. let activePath = path;
  85. if (path !== undefined && path.lastIndexOf('/') > 0 && hideList.indexOf(path) === -1) {
  86. const tmpPath = path.substring(1, path.length);
  87. activePath = '/' + tmpPath.substring(0, tmpPath.indexOf('/'));
  88. if (!route.meta.link) {
  89. appStore.toggleSideBarHide(false);
  90. }
  91. } else if (!route.children) {
  92. activePath = path;
  93. appStore.toggleSideBarHide(true);
  94. }
  95. activeRoutes(activePath);
  96. return activePath;
  97. });
  98. const setVisibleNumber = () => {
  99. const width = document.body.getBoundingClientRect().width / 3;
  100. visibleNumber.value = parseInt(String(width / 85));
  101. };
  102. const handleSelect = (key: string) => {
  103. currentIndex.value = key;
  104. const route = routers.value.find((item) => item.path === key);
  105. if (isHttp(key)) {
  106. // http(s):// 路径新窗口打开
  107. window.open(key, '_blank');
  108. } else if (!route || !route.children) {
  109. // 没有子路由路径内部打开
  110. const routeMenu = childrenMenus.value.find((item) => item.path === key);
  111. if (routeMenu && routeMenu.query) {
  112. let query = JSON.parse(routeMenu.query);
  113. router.push({ path: key, query: query });
  114. } else {
  115. router.push({ path: key });
  116. }
  117. appStore.toggleSideBarHide(true);
  118. } else {
  119. // 显示左侧联动菜单
  120. activeRoutes(key);
  121. appStore.toggleSideBarHide(false);
  122. }
  123. };
  124. const activeRoutes = (key: string) => {
  125. let routes: RouteRecordRaw[] = [];
  126. if (childrenMenus.value && childrenMenus.value.length > 0) {
  127. childrenMenus.value.map((item) => {
  128. if (key == item.parentPath || (key == 'index' && '' == item.path)) {
  129. routes.push(item);
  130. }
  131. });
  132. }
  133. if (routes.length > 0) {
  134. permissionStore.setSidebarRouters(routes);
  135. } else {
  136. appStore.toggleSideBarHide(true);
  137. }
  138. return routes;
  139. };
  140. onMounted(() => {
  141. window.addEventListener('resize', setVisibleNumber);
  142. });
  143. onBeforeUnmount(() => {
  144. window.removeEventListener('resize', setVisibleNumber);
  145. });
  146. onMounted(() => {
  147. setVisibleNumber();
  148. });
  149. </script>
  150. <style lang="scss">
  151. .topmenu-container.el-menu--horizontal > .el-menu-item {
  152. float: left;
  153. height: 50px !important;
  154. line-height: 50px !important;
  155. color: #999093 !important;
  156. padding: 0 5px !important;
  157. margin: 0 10px !important;
  158. }
  159. .topmenu-container.el-menu--horizontal > .el-menu-item.is-active,
  160. .el-menu--horizontal > .el-sub-menu.is-active .el-submenu__title {
  161. border-bottom: 2px solid #{'var(--theme)'} !important;
  162. color: #303133;
  163. }
  164. /* sub-menu item */
  165. .topmenu-container.el-menu--horizontal > .el-sub-menu .el-sub-menu__title {
  166. float: left;
  167. height: 50px !important;
  168. line-height: 50px !important;
  169. color: #999093 !important;
  170. padding: 0 5px !important;
  171. margin: 0 10px !important;
  172. }
  173. /* 背景色隐藏 */
  174. .topmenu-container.el-menu--horizontal > .el-menu-item:not(.is-disabled):focus,
  175. .topmenu-container.el-menu--horizontal > .el-menu-item:not(.is-disabled):hover,
  176. .topmenu-container.el-menu--horizontal > .el-submenu .el-submenu__title:hover {
  177. background-color: #ffffff !important;
  178. }
  179. /* 图标右间距 */
  180. .topmenu-container .svg-icon {
  181. margin-right: 4px;
  182. }
  183. </style>