refactor(路由与用户管理): 优化路由处理和用户登出逻辑
- 修改用户登出时的重定向逻辑,确保用户显式返回登录页,避免保留重定向参数 - 引入默认动态路由子项,简化路由配置 - 更新后端路由初始化逻辑,确保动态路由的正确处理 - 增强代码可读性,修复部分代码风格问题
This commit is contained in:
@@ -4,14 +4,12 @@ import { useUserInfo } from '/@/stores/userInfo';
|
||||
import { useRequestOldRoutes } from '/@/stores/requestOldRoutes';
|
||||
import { Session } from '/@/utils/storage';
|
||||
import { NextLoading } from '/@/utils/loading';
|
||||
import { dynamicRoutes, notFoundAndNoPower } from '/@/router/route';
|
||||
import { dynamicRoutes, defaultDynamicRouteChildren, notFoundAndNoPower } from '/@/router/route';
|
||||
import { formatTwoStageRoutes, formatFlatteningRoutes, router } from '/@/router/index';
|
||||
import { useRoutesList } from '/@/stores/routesList';
|
||||
import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
|
||||
import { getUserMenus } from '/@/api/system/menu/index';
|
||||
|
||||
|
||||
|
||||
const layouModules: any = import.meta.glob('../layout/routerView/*.{vue,tsx}');
|
||||
const viewsModules: any = import.meta.glob('../views/**/*.{vue,tsx}');
|
||||
|
||||
@@ -43,11 +41,12 @@ export async function initBackEndControlRoutes() {
|
||||
await useUserInfo().setPermissions();
|
||||
// 获取路由菜单数据
|
||||
await getBackEndControlRoutes();
|
||||
let menuRoute = Session.get('userMenu')
|
||||
let menuRoute = Session.get('userMenu');
|
||||
// 存储接口原始路由(未处理component),根据需求选择使用
|
||||
useRequestOldRoutes().setRequestOldRoutes(JSON.parse(JSON.stringify(menuRoute)));
|
||||
// 处理路由(component),替换 dynamicRoutes(/@/router/route)第一个顶级 children 的路由
|
||||
dynamicRoutes[0].children?.push(...await backEndComponent(menuRoute));
|
||||
dynamicRoutes[0].children = [...defaultDynamicRouteChildren];
|
||||
dynamicRoutes[0].children?.push(...(await backEndComponent(menuRoute)));
|
||||
// 添加动态路由
|
||||
await setAddRoute();
|
||||
// 设置路由到 vuex routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
|
||||
@@ -104,10 +103,10 @@ export async function setAddRoute() {
|
||||
* @returns 返回后端路由菜单数据
|
||||
*/
|
||||
export async function getBackEndControlRoutes() {
|
||||
let menuRoute = Session.get('userMenu')
|
||||
let permissions = Session.get('permissions')
|
||||
let menuRoute = Session.get('userMenu');
|
||||
let permissions = Session.get('permissions');
|
||||
if (!menuRoute || !permissions) {
|
||||
await refreshBackEndControlRoutes()
|
||||
await refreshBackEndControlRoutes();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,11 +117,11 @@ export async function getBackEndControlRoutes() {
|
||||
*/
|
||||
export async function refreshBackEndControlRoutes() {
|
||||
// 获取路由
|
||||
await getUserMenus().then((res:any)=>{
|
||||
Session.set('userMenu',res.data.menuList)
|
||||
Session.set('permissions',res.data.permissions)
|
||||
})
|
||||
await useUserInfo().setPermissions()
|
||||
await getUserMenus().then((res: any) => {
|
||||
Session.set('userMenu', res.data.menuList);
|
||||
Session.set('permissions', res.data.permissions);
|
||||
});
|
||||
await useUserInfo().setPermissions();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,16 +139,16 @@ export function setBackEndControlRefreshRoutes() {
|
||||
* @returns 返回处理成函数后的 component
|
||||
*/
|
||||
export function backEndComponent(routes: any) {
|
||||
if (!routes) return;
|
||||
if (!routes) return [];
|
||||
return routes.map((item: any) => {
|
||||
if(item.children&&item.children.length>0){
|
||||
item.children.some((ci:any)=>{
|
||||
if(!ci.meta.isHide){
|
||||
item.redirect = ci
|
||||
return true
|
||||
if (item.children && item.children.length > 0) {
|
||||
item.children.some((ci: any) => {
|
||||
if (!ci.meta.isHide) {
|
||||
item.redirect = ci;
|
||||
return true;
|
||||
}
|
||||
return false
|
||||
})
|
||||
return false;
|
||||
});
|
||||
}
|
||||
if (item.component) item.component = dynamicImport(dynamicViewsModules, item.component as string);
|
||||
item.children && backEndComponent(item.children);
|
||||
|
||||
@@ -21,6 +21,39 @@ import { RouteRecordRaw } from 'vue-router';
|
||||
* @description 各字段请查看 `/@/views/system/menu/component/addMenu.vue 下的 ruleForm`
|
||||
* @returns 返回路由菜单数据
|
||||
*/
|
||||
export const defaultDynamicRouteChildren: Array<RouteRecordRaw> = [
|
||||
{
|
||||
path: '/home',
|
||||
name: 'home',
|
||||
component: () => import('/@/views/home/index.vue'),
|
||||
meta: {
|
||||
title: 'message.router.home',
|
||||
isLink: '',
|
||||
isHide: true,
|
||||
isKeepAlive: true,
|
||||
isAffix: true,
|
||||
isIframe: false,
|
||||
roles: ['admin', 'common'],
|
||||
icon: 'iconfont icon-shouye',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/personal',
|
||||
name: 'personals',
|
||||
component: () => import('/@/views/system/personal/index.vue'),
|
||||
meta: {
|
||||
title: '个人中心',
|
||||
isLink: '',
|
||||
isHide: true,
|
||||
isKeepAlive: true,
|
||||
isAffix: false,
|
||||
isIframe: false,
|
||||
roles: ['admin'],
|
||||
icon: 'iconfont icon-diannao',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
export const dynamicRoutes: Array<RouteRecordRaw> = [
|
||||
{
|
||||
path: '/',
|
||||
@@ -30,38 +63,7 @@ export const dynamicRoutes: Array<RouteRecordRaw> = [
|
||||
meta: {
|
||||
isKeepAlive: true,
|
||||
},
|
||||
children: [
|
||||
{
|
||||
path: '/home',
|
||||
name: 'home',
|
||||
component: () => import('/@/views/home/index.vue'),
|
||||
meta: {
|
||||
title: 'message.router.home',
|
||||
isLink: '',
|
||||
isHide: true,
|
||||
isKeepAlive: true,
|
||||
isAffix: true,
|
||||
isIframe: false,
|
||||
roles: ['admin', 'common'],
|
||||
icon: 'iconfont icon-shouye',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/personal',
|
||||
name: 'personals',
|
||||
component: () => import('/@/views/system/personal/index.vue'),
|
||||
meta: {
|
||||
title: '个人中心',
|
||||
isLink: '',
|
||||
isHide: true,
|
||||
isKeepAlive: true,
|
||||
isAffix: false,
|
||||
isIframe: false,
|
||||
roles: ['admin'],
|
||||
icon: 'iconfont icon-diannao',
|
||||
},
|
||||
},
|
||||
],
|
||||
children: [...defaultDynamicRouteChildren],
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user