index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <!-- 横向布局 -->
  2. <template>
  3. <el-container class="layout">
  4. <el-header>
  5. <div class="logo flx-center">
  6. <img class="logo-img" src="@/assets/images/logo.png" alt="logo" />
  7. <span class="logo-text">{{ title }}</span>
  8. </div>
  9. <el-menu mode="horizontal" :router="false" :default-active="activeMenu">
  10. <!-- 不能直接使用 SubMenu 组件,无法触发 el-menu 隐藏省略功能 -->
  11. <template v-for="subItem in menuList" :key="subItem.path">
  12. <el-sub-menu v-if="subItem.children?.length" :key="subItem.path" :index="subItem.path + 'el-sub-menu'">
  13. <template #title>
  14. <el-icon>
  15. <component :is="subItem.meta.icon" />
  16. </el-icon>
  17. <span>{{ subItem.meta.title }}</span>
  18. </template>
  19. <SubMenu :menu-list="subItem.children" />
  20. </el-sub-menu>
  21. <el-menu-item v-else :key="subItem.path + 'el-menu-item'" :index="subItem.path" @click="handleClickMenu(subItem)">
  22. <el-icon>
  23. <component :is="subItem.meta.icon" />
  24. </el-icon>
  25. <template #title>
  26. <span>{{ subItem.meta.title }}</span>
  27. </template>
  28. </el-menu-item>
  29. </template>
  30. </el-menu>
  31. <ToolBarRight />
  32. </el-header>
  33. <Main />
  34. </el-container>
  35. </template>
  36. <script setup lang="ts" name="layoutTransverse">
  37. import { computed } from "vue";
  38. import { ElMessage } from "element-plus";
  39. import { useAuthStore } from "@/stores/modules/auth";
  40. import { useRoute, useRouter } from "vue-router";
  41. import { checkMenuClickPermission } from "@/utils/permission";
  42. import Main from "@/layouts/components/Main/index.vue";
  43. import ToolBarRight from "@/layouts/components/Header/ToolBarRight.vue";
  44. import SubMenu from "@/layouts/components/Menu/SubMenu.vue";
  45. const title = import.meta.env.VITE_GLOB_APP_TITLE;
  46. const route = useRoute();
  47. const router = useRouter();
  48. const authStore = useAuthStore();
  49. const menuList = computed(() => authStore.showMenuListGet);
  50. const activeMenu = computed(() => {
  51. if (!route || !route.path) return "";
  52. return (route.meta?.activeMenu ? route.meta.activeMenu : route.path) as string;
  53. });
  54. const handleClickMenu = async (subItem: Menu.MenuOptions) => {
  55. if (subItem.meta.isLink) return window.open(subItem.meta.isLink, "_blank");
  56. if (!subItem.path) {
  57. ElMessage.warning("菜单路径不存在");
  58. return;
  59. }
  60. // 调用权限检查方法,判断是否可以点击(方法内部会显示相应的提示信息)
  61. const { canClick } = await checkMenuClickPermission(subItem.path);
  62. if (!canClick) {
  63. return;
  64. }
  65. // 允许访问,执行路由跳转
  66. try {
  67. await router.push(subItem.path);
  68. } catch (error: any) {
  69. handleRouteError(error);
  70. }
  71. };
  72. // 处理路由错误
  73. const handleRouteError = (error: any) => {
  74. // 如果是导航重复的错误,忽略它
  75. if (error?.name === "NavigationDuplicated") {
  76. return;
  77. }
  78. // 如果是路由不存在的错误
  79. if (error?.message?.includes("No match") || error?.message?.includes("not found")) {
  80. ElMessage.warning("该页面不存在或尚未配置");
  81. } else {
  82. console.error("路由跳转失败:", error);
  83. ElMessage.error("页面跳转失败,请稍后重试");
  84. }
  85. };
  86. </script>
  87. <style scoped lang="scss">
  88. @import "./index";
  89. </style>