From 1e584e9bc5079b2e067159f75041fe0eb882083d Mon Sep 17 00:00:00 2001 From: "Ray.Hao" <1490493387@qq.com> Date: Tue, 20 May 2025 23:14:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20:recycle:=20=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- .../Sidebar/components/SidebarMixTopMenu.vue | 12 ++++++++-- src/layout/index.vue | 6 ++--- src/plugins/permission.ts | 5 ++-- src/store/modules/permission.store.ts | 24 +++++++++---------- src/types/websocket.ts | 15 ------------ 6 files changed, 29 insertions(+), 35 deletions(-) delete mode 100644 src/types/websocket.ts diff --git a/package.json b/package.json index a57448a3..4925a998 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vue3-element-admin", "description": "Vue3 + Vite + TypeScript + Element-Plus 的后台管理模板,vue-element-admin 的 Vue3 版本", - "version": "2.28.4", + "version": "2.29.0", "private": true, "type": "module", "scripts": { diff --git a/src/layout/components/Sidebar/components/SidebarMixTopMenu.vue b/src/layout/components/Sidebar/components/SidebarMixTopMenu.vue index 7f0f4154..2fd657d1 100644 --- a/src/layout/components/Sidebar/components/SidebarMixTopMenu.vue +++ b/src/layout/components/Sidebar/components/SidebarMixTopMenu.vue @@ -78,10 +78,18 @@ appStore.activeTopMenu(activeTopMenuPath); */ const handleMenuSelect = (routePath: string) => { appStore.activeTopMenu(routePath); // 设置激活的顶部菜单 - permissionStore.setMixedLayoutLeftRoutes(routePath); // 更新左侧菜单 - navigateToFirstLeftMenu(permissionStore.mixedLayoutLeftRoutes); // 跳转到左侧第一个菜单 + activateFirstLevelMenu(routePath); // 激活一级菜单并设置左侧二级菜单 }; +/** + * 激活一级菜单并设置左侧二级菜单 + * @param routePath 点击的菜单路径 + */ +function activateFirstLevelMenu(routePath: string) { + permissionStore.updateSideMenu(routePath); // 更新左侧菜单 + navigateToFirstLeftMenu(permissionStore.sideMenuRoutes); // 跳转到左侧第一个菜单 +} + /** * 跳转到左侧第一个可访问的菜单 * @param menus 左侧菜单列表 diff --git a/src/layout/index.vue b/src/layout/index.vue index 4ac10c6f..9227afc8 100644 --- a/src/layout/index.vue +++ b/src/layout/index.vue @@ -11,7 +11,7 @@
- +
@@ -72,13 +72,13 @@ const isSidebarOpen = computed(() => appStore.sidebar.opened); // 侧边栏是 const isShowTagsView = computed(() => settingsStore.tagsView); // 是否显示标签视图 const layout = computed(() => settingsStore.layout); // 当前布局模式(left、top、mix) const activeTopMenuPath = computed(() => appStore.activeTopMenuPath); // 顶部菜单激活路径 -const mixedLayoutLeftRoutes = computed(() => permissionStore.mixedLayoutLeftRoutes); // 混合布局左侧菜单路由 +const sideMenuRoutes = computed(() => permissionStore.sideMenuRoutes); // 混合布局左侧菜单路由 // 监听顶部菜单激活路径变化,更新混合布局左侧菜单路由 watch( () => activeTopMenuPath.value, (newVal: string) => { - permissionStore.setMixedLayoutLeftRoutes(newVal); + permissionStore.updateSideMenu(newVal); }, { deep: true, immediate: true } ); diff --git a/src/plugins/permission.ts b/src/plugins/permission.ts index 4d934fcc..254bf993 100644 --- a/src/plugins/permission.ts +++ b/src/plugins/permission.ts @@ -16,12 +16,13 @@ export function setupPermission() { const isLogin = !!Storage.get(ACCESS_TOKEN_KEY, ""); // 判断是否登录 if (isLogin) { if (to.path === "/login") { - // 已登录,访问登录页,跳转到首页 + // 已登录,跳转到首页 next({ path: "/" }); } else { + // 未登录 const permissionStore = usePermissionStore(); // 判断路由是否加载完成 - if (permissionStore.isRoutesLoaded) { + if (permissionStore.routesLoaded) { if (to.matched.length === 0) { // 路由未匹配,跳转到404 next("/404"); diff --git a/src/store/modules/permission.store.ts b/src/store/modules/permission.store.ts index 647d3e75..d08fce91 100644 --- a/src/store/modules/permission.store.ts +++ b/src/store/modules/permission.store.ts @@ -8,12 +8,12 @@ const modules = import.meta.glob("../../views/**/**.vue"); const Layout = () => import("@/layout/index.vue"); export const usePermissionStore = defineStore("permission", () => { - // 储所有路由,包括静态路由和动态路由 + // 存储所有路由,包括静态路由和动态路由 const routes = ref([]); // 混合模式左侧菜单路由 - const mixedLayoutLeftRoutes = ref([]); + const sideMenuRoutes = ref([]); // 路由是否加载完成 - const isRoutesLoaded = ref(false); + const routesLoaded = ref(false); /** * 获取后台动态路由数据,解析并注册到全局路由 @@ -26,7 +26,7 @@ export const usePermissionStore = defineStore("permission", () => { .then((data) => { const dynamicRoutes = parseDynamicRoutes(data); routes.value = [...constantRoutes, ...dynamicRoutes]; - isRoutesLoaded.value = true; + routesLoaded.value = true; resolve(dynamicRoutes); }) .catch((error) => { @@ -36,14 +36,14 @@ export const usePermissionStore = defineStore("permission", () => { } /** - * 根据父菜单路径设置混合模式左侧菜单 + * 根据父菜单路径设置侧边菜单 * * @param parentPath 父菜单的路径,用于查找对应的菜单项 */ - const setMixedLayoutLeftRoutes = (parentPath: string) => { + const updateSideMenu = (parentPath: string) => { const matchedItem = routes.value.find((item) => item.path === parentPath); if (matchedItem && matchedItem.children) { - mixedLayoutLeftRoutes.value = matchedItem.children; + sideMenuRoutes.value = matchedItem.children; } }; @@ -60,16 +60,16 @@ export const usePermissionStore = defineStore("permission", () => { // 清空本地存储的路由和菜单数据 routes.value = []; - mixedLayoutLeftRoutes.value = []; - isRoutesLoaded.value = false; + sideMenuRoutes.value = []; + routesLoaded.value = false; }; return { routes, - mixedLayoutLeftRoutes, - isRoutesLoaded, + sideMenuRoutes, + routesLoaded, generateRoutes, - setMixedLayoutLeftRoutes, + updateSideMenu, resetRouter, }; }); diff --git a/src/types/websocket.ts b/src/types/websocket.ts deleted file mode 100644 index 30b56a2d..00000000 --- a/src/types/websocket.ts +++ /dev/null @@ -1,15 +0,0 @@ -/** - * WebSocket相关类型定义 - */ - -/** - * 字典WebSocket事件类型 - */ -export interface DictWebSocketEvent { - /** 事件类型:更新或删除 */ - type: "DICT_UPDATED" | "DICT_DELETED"; - /** 字典编码 */ - dictCode: string; - /** 时间戳 */ - timestamp: number; -}