index.vue 6.2 KB

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