refactor: ♻️ 布局代码目录结构重构

This commit is contained in:
Ray.Hao
2025-09-14 21:22:20 +08:00
parent 6730608920
commit 265257294b
9 changed files with 98 additions and 111 deletions

View File

@@ -9,6 +9,11 @@
</transition> </transition>
</template> </template>
</router-view> </router-view>
<!-- 返回顶部按钮 -->
<el-backtop target=".app-main">
<div class="i-svg:backtop w-6 h-6" />
</el-backtop>
</section> </section>
</template> </template>

View File

@@ -1,48 +1,36 @@
<template> <template>
<div class="layout-wrapper"> <div class="layout" :class="layoutClass">
<component :is="currentLayoutComponent" /> <!-- 移动端遮罩层 -->
<div v-if="isMobile && isSidebarOpen" class="layout__overlay" @click="closeSidebar" />
<!-- 设置面板 - 独立于布局组件 --> <!-- 布局内容插槽 -->
<Settings v-if="isShowSettings" /> <slot></slot>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed } from "vue"; import { useLayout, useLayoutResponsive } from "@/composables";
import { useRoute } from "vue-router";
import { useLayout } from "@/composables/layout/useLayout";
import LeftLayout from "./views/LeftLayout.vue";
import TopLayout from "./views/TopLayout.vue";
import MixLayout from "./views/MixLayout.vue";
import Settings from "./components/Settings/index.vue";
import { LayoutMode } from "@/enums/settings/layout.enum";
import { defaultSettings } from "@/settings";
const { currentLayout } = useLayout(); // 布局相关
const route = useRoute(); const { layoutClass, isSidebarOpen, closeSidebar } = useLayout();
// 根据当前布局模式选择对应的组件 // 响应式布局
const currentLayoutComponent = computed(() => { const { isMobile } = useLayoutResponsive();
const override = route.meta?.layout as LayoutMode | undefined;
const layoutToUse = override ?? currentLayout.value;
switch (layoutToUse) {
case LayoutMode.TOP:
return TopLayout;
case LayoutMode.MIX:
return MixLayout;
case LayoutMode.LEFT:
default:
return LeftLayout;
}
});
// 是否显示设置面板
const isShowSettings = computed(() => defaultSettings.showSettings);
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.layout-wrapper { .layout {
width: 100%; width: 100%;
height: 100%; height: 100%;
&__overlay {
position: fixed;
top: 0;
left: 0;
z-index: 999;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.3);
}
} }
</style> </style>

View File

@@ -1,41 +0,0 @@
<template>
<div class="layout" :class="layoutClass">
<!-- 移动端遮罩层 -->
<div v-if="isMobile && isSidebarOpen" class="layout__overlay" @click="closeSidebar" />
<!-- 布局内容插槽 -->
<slot></slot>
<!-- 返回顶部按钮 -->
<el-backtop target=".app-main">
<div class="i-svg:backtop w-6 h-6" />
</el-backtop>
</div>
</template>
<script setup lang="ts">
import { useLayout, useLayoutResponsive } from "@/composables";
// 布局相关
const { layoutClass, isSidebarOpen, closeSidebar } = useLayout();
// 响应式布局
const { isMobile } = useLayoutResponsive();
</script>
<style lang="scss" scoped>
.layout {
width: 100%;
height: 100%;
&__overlay {
position: fixed;
top: 0;
left: 0;
z-index: 999;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.3);
}
}
</style>

View File

@@ -0,0 +1,47 @@
<template>
<div class="layout-wrapper">
<component :is="currentLayoutComponent" />
<!-- 设置面板 - 独立于布局组件 -->
<Settings v-if="isShowSettings" />
</div>
</template>
<script setup lang="ts">
import { useRoute } from "vue-router";
import { useLayout } from "@/composables/layout/useLayout";
import LeftLayout from "@/layouts/modes/left/index.vue";
import TopLayout from "@/layouts/modes/top/index.vue";
import MixLayout from "@/layouts/modes/mix/index.vue";
import Settings from "../components/Settings/index.vue";
import { LayoutMode } from "@/enums/settings/layout.enum";
import { defaultSettings } from "@/settings";
const { currentLayout } = useLayout();
const route = useRoute();
// 根据当前布局模式选择对应的组件
const currentLayoutComponent = computed(() => {
const override = route.meta?.layout as LayoutMode | undefined;
const layoutToUse = override ?? currentLayout.value;
switch (layoutToUse) {
case LayoutMode.TOP:
return TopLayout;
case LayoutMode.MIX:
return MixLayout;
case LayoutMode.LEFT:
default:
return LeftLayout;
}
});
// 是否显示设置面板
const isShowSettings = computed(() => defaultSettings.showSettings);
</script>
<style lang="scss" scoped>
.layout-wrapper {
width: 100%;
height: 100%;
}
</style>

View File

@@ -30,12 +30,12 @@
<script setup lang="ts"> <script setup lang="ts">
import { useLayout } from "@/composables/layout/useLayout"; import { useLayout } from "@/composables/layout/useLayout";
import { useLayoutMenu } from "@/composables/layout/useLayoutMenu"; import { useLayoutMenu } from "@/composables/layout/useLayoutMenu";
import BaseLayout from "./BaseLayout.vue"; import BaseLayout from "@/layouts/index.vue";
import AppLogo from "../components/AppLogo/index.vue"; import AppLogo from "../../components/AppLogo/index.vue";
import NavBar from "../components/NavBar/index.vue"; import NavBar from "../../components/NavBar/index.vue";
import TagsView from "../components/TagsView/index.vue"; import TagsView from "../../components/TagsView/index.vue";
import AppMain from "../components/AppMain/index.vue"; import AppMain from "../../components/AppMain/index.vue";
import BasicMenu from "../components/Menu/BasicMenu.vue"; import BasicMenu from "../../components/Menu/BasicMenu.vue";
// //
const { isShowTagsView, isShowLogo, isSidebarOpen } = useLayout(); const { isShowTagsView, isShowLogo, isSidebarOpen } = useLayout();

View File

@@ -58,16 +58,17 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, watch } from "vue";
import { useRoute } from "vue-router"; import { useRoute } from "vue-router";
import { useWindowSize } from "@vueuse/core"; import { useWindowSize } from "@vueuse/core";
import { useLayout, useLayoutMenu } from "@/composables"; import { useLayout, useLayoutMenu } from "@/composables";
import BaseLayout from "./BaseLayout.vue"; import BaseLayout from "@/layouts/index.vue";
import AppLogo from "../components/AppLogo/index.vue"; import AppLogo from "../../components/AppLogo/index.vue";
import MixTopMenu from "../components/Menu/MixTopMenu.vue"; import MixTopMenu from "../../components/Menu/MixTopMenu.vue";
import NavbarActions from "../components/NavBar/components/NavbarActions.vue"; import NavbarActions from "../../components/NavBar/components/NavbarActions.vue";
import TagsView from "../components/TagsView/index.vue"; import TagsView from "../../components/TagsView/index.vue";
import AppMain from "../components/AppMain/index.vue"; import AppMain from "../../components/AppMain/index.vue";
import MenuItem from "../components/Menu/components/MenuItem.vue"; import MenuItem from "../../components/Menu/components/MenuItem.vue";
import Hamburger from "@/components/Hamburger/index.vue"; import Hamburger from "@/components/Hamburger/index.vue";
import variables from "@/styles/variables.module.scss"; import variables from "@/styles/variables.module.scss";
import { isExternal } from "@/utils/index"; import { isExternal } from "@/utils/index";
@@ -91,8 +92,8 @@ const isLogoCollapsed = computed(() => width.value < 768);
const activeLeftMenuPath = computed(() => { const activeLeftMenuPath = computed(() => {
const { meta, path } = route; const { meta, path } = route;
// activeMenu使 // activeMenu使
if (meta?.activeMenu && typeof meta.activeMenu === "string") { if ((meta?.activeMenu as unknown as string) && typeof meta.activeMenu === "string") {
return meta.activeMenu; return meta.activeMenu as unknown as string;
} }
return path; return path;
}); });
@@ -116,26 +117,16 @@ function resolvePath(routePath: string) {
watch( watch(
() => route.path, () => route.path,
(newPath) => { (newPath) => {
console.log("📍 Route changed in MixLayout:", newPath);
// //
const topMenuPath = const topMenuPath =
newPath.split("/").filter(Boolean).length > 1 ? newPath.match(/^\/[^/]+/)?.[0] || "/" : "/"; newPath.split("/").filter(Boolean).length > 1 ? newPath.match(/^\/[^/]+/)?.[0] || "/" : "/";
// //
if (newPath.startsWith(activeTopMenuPath.value)) { if (newPath.startsWith(activeTopMenuPath.value)) {
console.log("📍 Route is under active top menu, ensuring menu item is activated"); // no-op
} }
// //
else if (topMenuPath !== activeTopMenuPath.value) { else if (topMenuPath !== activeTopMenuPath.value) {
console.log(
"📍 Top menu changed, updating active menu from:",
activeTopMenuPath.value,
"to:",
topMenuPath
);
//
const appStore = useAppStore(); const appStore = useAppStore();
const permissionStore = usePermissionStore(); const permissionStore = usePermissionStore();
@@ -283,7 +274,6 @@ watch(
} }
} }
} }
:deep(.hasTagsView) { :deep(.hasTagsView) {
.app-main { .app-main {
height: calc(100vh - $navbar-height - $tags-view-height) !important; height: calc(100vh - $navbar-height - $tags-view-height) !important;

View File

@@ -23,16 +23,14 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed } from "vue";
import { useWindowSize } from "@vueuse/core";
import { useLayout } from "@/composables/layout/useLayout"; import { useLayout } from "@/composables/layout/useLayout";
import { useLayoutMenu } from "@/composables/layout/useLayoutMenu"; import { useLayoutMenu } from "@/composables/layout/useLayoutMenu";
import BaseLayout from "./BaseLayout.vue"; import BaseLayout from "@/layouts/index.vue";
import AppLogo from "../components/AppLogo/index.vue"; import AppLogo from "../../components/AppLogo/index.vue";
import BasicMenu from "../components/Menu/BasicMenu.vue"; import BasicMenu from "../../components/Menu/BasicMenu.vue";
import NavbarActions from "../components/NavBar/components/NavbarActions.vue"; import NavbarActions from "../../components/NavBar/components/NavbarActions.vue";
import TagsView from "../components/TagsView/index.vue"; import TagsView from "../../components/TagsView/index.vue";
import AppMain from "../components/AppMain/index.vue"; import AppMain from "../../components/AppMain/index.vue";
// //
const { isShowTagsView, isShowLogo } = useLayout(); const { isShowTagsView, isShowLogo } = useLayout();

View File

@@ -1,7 +1,7 @@
import type { App } from "vue"; import type { App } from "vue";
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router"; import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";
export const Layout = () => import("@/layouts/index.vue"); export const Layout = () => import("@/layouts/modes/index.vue");
// 静态路由 // 静态路由
export const constantRoutes: RouteRecordRaw[] = [ export const constantRoutes: RouteRecordRaw[] = [

View File

@@ -5,7 +5,7 @@ import router from "@/router";
import MenuAPI, { type RouteVO } from "@/api/system/menu-api"; import MenuAPI, { type RouteVO } from "@/api/system/menu-api";
const modules = import.meta.glob("../../views/**/**.vue"); const modules = import.meta.glob("../../views/**/**.vue");
const Layout = () => import("@/layouts/index.vue"); const Layout = () => import("@/layouts/modes/index.vue");
export const usePermissionStore = defineStore("permission", () => { export const usePermissionStore = defineStore("permission", () => {
// 所有路由(静态路由 + 动态路由) // 所有路由(静态路由 + 动态路由)