From 21a1e182bd7dc770ef31df707bc9b5e1985f30a2 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 26 Oct 2026 16:10:18 +0800 Subject: [PATCH] refactor: implement automatic redirection to first accessible workspace for users with limited access --- ui/src/router/ROUTE_README.md | 6 ++++-- ui/src/router/admin/index.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ui/src/router/ROUTE_README.md b/ui/src/router/ROUTE_README.md index 6ee80fbea0a..b79b514f493 100644 --- a/ui/src/router/ROUTE_README.md +++ b/ui/src/router/ROUTE_README.md @@ -19,8 +19,10 @@ Workspace 使用 `AppLayout`,左侧导航根据 `scope: 'workspace'` 对应路由的 `children` 自动生成。 Workspace 根地址为 `/admin/workspace/:workspaceId`。`workspaceId` 只以当前路由参数为准。应用根 -地址、登录成功和从 System 返回 Workspace 时统一进入 `/admin/workspace/default`。父路由的 -`scope` 会合并到匹配的子路由 `meta` 中。 +地址、登录成功和从 System 返回 Workspace 时统一进入 `/admin/workspace/default`;若当前用户 +只属于 `workspace_list` 中的部分工作空间且目标 `workspaceId` 不在其中,路由守卫会自动重定向到 +其首个可访问工作空间(仅对携带 `workspaceId` 参数的工作空间路由生效)。父路由的 `scope` +会合并到匹配的子路由 `meta` 中。 顶部工作空间切换统一由 `layout/header-workspace-dropdown/index.vue` 处理:选择不同工作空间时, 通过 `router.resolve()` 生成目标地址,再调用 `window.location.assign()` 整页加载,重新初始化 diff --git a/ui/src/router/admin/index.ts b/ui/src/router/admin/index.ts index 2223e431600..92acd2f12e5 100644 --- a/ui/src/router/admin/index.ts +++ b/ui/src/router/admin/index.ts @@ -45,6 +45,20 @@ router.beforeEach(async (to, from) => { if (!user.userInfo) { await auth.loadAuthBaseProfile() } + + // 用户只属于部分工作空间时,自动落到其首个可访问工作空间; + // 避免登录/根路径/返回等入口硬编码进入 default 而出现选中为空、无权限。 + const workspaceId = to.params?.workspaceId as string | undefined + const workspace_list = user.userInfo?.workspace_list + if (to.name && workspaceId && workspace_list?.length) { + const accessibleIds = new Set(workspace_list.map((ws) => ws.id).filter((id): id is string => Boolean(id))) + if (!accessibleIds.has(workspaceId)) { + const fallbackId = workspace_list.find((ws) => ws.id)?.id ?? 'default' + if (fallbackId !== workspaceId) { + return { name: to.name, params: { ...to.params, workspaceId: fallbackId }, replace: true } + } + } + } } // 当前路由有权限,放行