index.vue 6.1 KB

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