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