index.vue 6.0 KB

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