Merge branch 'developer' into 'master'

1

See merge request scj/zhxy/v3/cloud-ui!6
This commit is contained in:
Administrator
2026-03-08 18:19:49 +00:00
3 changed files with 28 additions and 10 deletions

View File

@@ -82,6 +82,19 @@ async function validateCachedRoleId(): Promise<boolean> {
} }
} }
/** 有 token 时执行角色校验:缓存 roleId 无效则清缓存并弹框,缺角色信息则弹框(与 onMounted 内逻辑一致) */
async function runRoleValidationAndOpenDialogIfNeeded() {
if (!Session.getToken()) return;
const needOpenByInvalidRole = await validateCachedRoleId();
if (needOpenByInvalidRole && !isRoleDialogTriggered()) {
setRoleDialogTriggered(true);
mittBus.emit('openRoleSelectDialog');
} else if (!needOpenByInvalidRole && needRoleSelection() && !isRoleDialogTriggered()) {
setRoleDialogTriggered(true);
mittBus.emit('openRoleSelectDialog');
}
}
onMounted(() => { onMounted(() => {
// 唯一入口:只通过事件打开,且只打开一次;延迟打开以等待异步组件挂载 // 唯一入口:只通过事件打开,且只打开一次;延迟打开以等待异步组件挂载
mittBus.on('openRoleSelectDialog', () => { mittBus.on('openRoleSelectDialog', () => {
@@ -99,6 +112,10 @@ onMounted(() => {
}; };
tryOpen(); tryOpen();
}); });
// token 来自 URL 时由路由 afterEach 发出,此时 App 已挂载但 onMounted 时可能尚无 token需在此补跑一次校验
mittBus.on('validateRoleFromUrl', () => {
runRoleValidationAndOpenDialogIfNeeded();
});
nextTick(async () => { nextTick(async () => {
// 监听布局配置弹窗点击打开 // 监听布局配置弹窗点击打开
mittBus.on('openSettingsDrawer', () => { mittBus.on('openSettingsDrawer', () => {
@@ -114,22 +131,14 @@ onMounted(() => {
stores.setCurrenFullscreen(Session.get('isTagsViewCurrenFull')); stores.setCurrenFullscreen(Session.get('isTagsViewCurrenFull'));
} }
// 有 token 时:先校验缓存 roleId 是否仍有效,无效则清缓存并弹框选角色 // 有 token 时:先校验缓存 roleId 是否仍有效,无效则清缓存并弹框选角色
if (Session.getToken()) { await runRoleValidationAndOpenDialogIfNeeded();
const needOpenByInvalidRole = await validateCachedRoleId();
if (needOpenByInvalidRole && !isRoleDialogTriggered()) {
setRoleDialogTriggered(true);
mittBus.emit('openRoleSelectDialog');
} else if (!needOpenByInvalidRole && needRoleSelection() && !isRoleDialogTriggered()) {
setRoleDialogTriggered(true);
mittBus.emit('openRoleSelectDialog');
}
}
}); });
}); });
// 页面销毁时,关闭监听 // 页面销毁时,关闭监听
onUnmounted(() => { onUnmounted(() => {
mittBus.off('openSettingsDrawer', () => {}); mittBus.off('openSettingsDrawer', () => {});
mittBus.off('openRoleSelectDialog'); mittBus.off('openRoleSelectDialog');
mittBus.off('validateRoleFromUrl');
}); });
// 监听路由的变化,设置网站标题 // 监听路由的变化,设置网站标题
watch( watch(

View File

@@ -7,6 +7,7 @@ import { useKeepALiveNames } from '/@/stores/keepAliveNames';
import { useRoutesList } from '/@/stores/routesList'; import { useRoutesList } from '/@/stores/routesList';
import { useUserInfo } from '/@/stores/userInfo'; import { useUserInfo } from '/@/stores/userInfo';
import { Session } from '/@/utils/storage'; import { Session } from '/@/utils/storage';
import mittBus from '/@/utils/mitt';
import { staticRoutes, notFoundAndNoPower } from '/@/router/route'; import { staticRoutes, notFoundAndNoPower } from '/@/router/route';
import { initBackEndControlRoutes } from '/@/router/backEnd'; import { initBackEndControlRoutes } from '/@/router/backEnd';
import { flowConfig } from '/@/flow/designer/config/flow-config'; import { flowConfig } from '/@/flow/designer/config/flow-config';
@@ -98,6 +99,8 @@ router.beforeEach(async (to, from, next) => {
const urlToken = to.query?.token as string | undefined; const urlToken = to.query?.token as string | undefined;
if (urlToken) { if (urlToken) {
useUserInfo().setTokenCache(urlToken, to.query?.refresh_token as string | undefined); useUserInfo().setTokenCache(urlToken, to.query?.refresh_token as string | undefined);
// 标记「本次 token 来自 URL」供 afterEach 触发角色校验;因 setTokenCache 在 beforeEach 执行,可能晚于 App.onMounted需单独触发校验以清除无效 roleId
Session.set('tokenFromUrl', 1);
} }
// 若上面刚写了 token后续用去掉 token 的 query 做一次 replace在 init 之后统一做保证只一次导航、tagsView 能正确加 tag // 若上面刚写了 token后续用去掉 token 的 query 做一次 replace在 init 之后统一做保证只一次导航、tagsView 能正确加 tag
const stripTokenQuery = const stripTokenQuery =
@@ -148,6 +151,11 @@ router.beforeEach(async (to, from, next) => {
router.afterEach(() => { router.afterEach(() => {
NProgress.done(); NProgress.done();
NextLoading.done(); NextLoading.done();
// token 来自 URL 时App.onMounted 可能已先执行且当时无 token需在导航完成后触发一次角色校验清无效 roleId + 弹框)
if (Session.get('tokenFromUrl')) {
Session.remove('tokenFromUrl');
mittBus.emit('validateRoleFromUrl');
}
}); });
// 导出路由 // 导出路由

1
src/types/mitt.d.ts vendored
View File

@@ -24,6 +24,7 @@ declare type MittType<T = any> = {
onTagsViewRefreshRouterView?: T; onTagsViewRefreshRouterView?: T;
onCurrentContextmenuClick?: T; onCurrentContextmenuClick?: T;
openRoleSelectDialog?: string; openRoleSelectDialog?: string;
validateRoleFromUrl?: string;
}; };
// mitt 参数类型定义 // mitt 参数类型定义