1
This commit is contained in:
45
src/App.vue
45
src/App.vue
@@ -17,6 +17,7 @@ import { Local, Session } from '/@/utils/storage';
|
||||
import mittBus from '/@/utils/mitt';
|
||||
import { needRoleSelection, isRoleDialogTriggered, setRoleDialogTriggered } from '/@/utils/roleSelect';
|
||||
import setIntroduction from '/@/utils/setIconfont';
|
||||
import { listAllRole } from '/@/api/admin/role';
|
||||
|
||||
// 引入组件
|
||||
const LockScreen = defineAsyncComponent(() => import('/@/layout/lockScreen/index.vue'));
|
||||
@@ -58,6 +59,34 @@ onBeforeMount(() => {
|
||||
});
|
||||
// 角色选择弹框是否已在本轮打开过(防止事件被触发两次)
|
||||
let roleDialogOpenedThisSession = false
|
||||
|
||||
/** 校验缓存中的 roleId 是否仍在 listAllRole 结果中;若不存在则清除 roleId/roleCode/roleName 并返回 true(需要弹框) */
|
||||
async function validateCachedRoleId(): Promise<boolean> {
|
||||
const cachedRoleId = Local.get('roleId');
|
||||
if (cachedRoleId == null || cachedRoleId === '') return false;
|
||||
try {
|
||||
const res = await listAllRole();
|
||||
const data = res?.data;
|
||||
const allRoles: any[] = Array.isArray(data)
|
||||
? data
|
||||
: data && typeof data === 'object'
|
||||
? (Object.values(data) as any[]).flat()
|
||||
: [];
|
||||
const exists = allRoles.some(
|
||||
(r: any) => r && String(r.roleId) === String(cachedRoleId)
|
||||
);
|
||||
if (!exists) {
|
||||
Local.remove('roleId');
|
||||
Local.remove('roleCode');
|
||||
Local.remove('roleName');
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
// 唯一入口:只通过事件打开,且只打开一次;延迟打开以等待异步组件挂载
|
||||
mittBus.on('openRoleSelectDialog', () => {
|
||||
@@ -67,7 +96,7 @@ onMounted(() => {
|
||||
changeRoleFirRef.value?.open()
|
||||
}, 300)
|
||||
})
|
||||
nextTick(() => {
|
||||
nextTick(async () => {
|
||||
// 监听布局配置弹窗点击打开
|
||||
mittBus.on('openSettingsDrawer', () => {
|
||||
settingsRef.value.openDrawer();
|
||||
@@ -81,10 +110,16 @@ onMounted(() => {
|
||||
if (Session.get('isTagsViewCurrenFull')) {
|
||||
stores.setCurrenFullscreen(Session.get('isTagsViewCurrenFull'));
|
||||
}
|
||||
// 与请求拦截器共用同一逻辑:先设标志再 emit,由监听器统一打开(监听器内会延迟 300ms 以等待异步组件挂载)
|
||||
if (Session.getToken() && needRoleSelection() && !isRoleDialogTriggered()) {
|
||||
setRoleDialogTriggered(true)
|
||||
mittBus.emit('openRoleSelectDialog')
|
||||
// 有 token 时:先校验缓存 roleId 是否仍有效,无效则清缓存并弹框选角色
|
||||
if (Session.getToken()) {
|
||||
const needOpenByInvalidRole = await validateCachedRoleId();
|
||||
if (needOpenByInvalidRole && !isRoleDialogTriggered()) {
|
||||
setRoleDialogTriggered(true);
|
||||
mittBus.emit('openRoleSelectDialog');
|
||||
} else if (!needOpenByInvalidRole && needRoleSelection() && !isRoleDialogTriggered()) {
|
||||
setRoleDialogTriggered(true);
|
||||
mittBus.emit('openRoleSelectDialog');
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user