fix: 路由加载合并排序(还存在切换账号菜单缓存问题)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { useRouter } from 'vue-router';
|
||||
import type {RouteLocationRaw} from 'vue-router';
|
||||
import {useRouter} from 'vue-router';
|
||||
import type {RouteKey} from '@elegant-router/types';
|
||||
import {router as globalRouter} from '@/router';
|
||||
|
||||
@@ -53,7 +53,6 @@ export function useRouterPush(inSetup = true) {
|
||||
*/
|
||||
async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) {
|
||||
const module = loginModule || 'pwd-login';
|
||||
|
||||
const options: RouterPushOptions = {
|
||||
params: {
|
||||
module
|
||||
|
||||
@@ -79,6 +79,8 @@ export const generatedRoutes: GeneratedRoute[] = [
|
||||
i18nKey: 'route.user-center'
|
||||
}
|
||||
},
|
||||
|
||||
// 下面是管理员角色才有
|
||||
{
|
||||
name: 'function',
|
||||
path: '/function',
|
||||
@@ -87,7 +89,7 @@ export const generatedRoutes: GeneratedRoute[] = [
|
||||
title: 'function',
|
||||
i18nKey: 'route.function',
|
||||
icon: 'icon-park-outline:all-application',
|
||||
order: 6,
|
||||
order: 1002,
|
||||
roles: ['admin'],
|
||||
},
|
||||
children: [
|
||||
@@ -205,7 +207,7 @@ export const generatedRoutes: GeneratedRoute[] = [
|
||||
title: 'about',
|
||||
i18nKey: 'route.about',
|
||||
icon: 'fluent:book-information-24-regular',
|
||||
order: 100,
|
||||
order: 1003,
|
||||
roles: ['admin']
|
||||
}
|
||||
},
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import type {RouteRecordRaw} from 'vue-router';
|
||||
import type {ElegantConstRoute} from '@elegant-router/vue';
|
||||
import type { RouteKey, RouteMap, RoutePath } from '@elegant-router/types';
|
||||
import type {RouteKey, RouteMap} from '@elegant-router/types';
|
||||
import BaseLayout from '@/layouts/base-layout/index.vue';
|
||||
import BlankLayout from '@/layouts/blank-layout/index.vue';
|
||||
|
||||
@@ -208,18 +208,5 @@ const routeMap: RouteMap = {
|
||||
* @param name route name
|
||||
*/
|
||||
export function getRoutePath<T extends RouteKey>(name: T) {
|
||||
console.log(name);
|
||||
return routeMap[name];
|
||||
}
|
||||
|
||||
/**
|
||||
* get route name by route path
|
||||
* @param path route path
|
||||
*/
|
||||
export function getRouteName(path: RoutePath) {
|
||||
const routeEntries = Object.entries(routeMap) as [RouteKey, RoutePath][];
|
||||
|
||||
const routeName: RouteKey | null = routeEntries.find(([, routePath]) => routePath === path)?.[0] || null;
|
||||
|
||||
return routeName;
|
||||
}
|
||||
|
||||
@@ -146,17 +146,9 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
async function initConstantRoute() {
|
||||
if (isInitConstantRoute.value) return;
|
||||
|
||||
// if (authRouteMode.value === 'static') {
|
||||
const {constantRoutes} = createStaticRoutes();
|
||||
|
||||
addAuthRoutes(constantRoutes);
|
||||
// } else {
|
||||
// const { data, error } = await fetchGetConstantRoutes();
|
||||
|
||||
// if (!error) {
|
||||
// addAuthRoutes(data);
|
||||
// }
|
||||
// }
|
||||
|
||||
handleAuthRoutes();
|
||||
|
||||
@@ -165,17 +157,12 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
|
||||
/** Init auth route */
|
||||
async function initAuthRoute() {
|
||||
// await initStaticAuthRoute();
|
||||
// await initDynamicAuthRoute();
|
||||
|
||||
|
||||
const {data: routes, error} = await doGetUserRoutes();
|
||||
if (!error) {
|
||||
const {authRoutes: staticAuthRoutes} = createStaticRoutes();
|
||||
const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []);
|
||||
console.log(authStore.userInfo.roles,filteredAuthRoutes)
|
||||
console.log(authStore.userInfo.roles,[ ...filteredAuthRoutes,...routes])
|
||||
addAuthRoutes([ ...filteredAuthRoutes,...routes]);
|
||||
addAuthRoutes(filteredAuthRoutes.concat(routes));
|
||||
|
||||
handleAuthRoutes();
|
||||
|
||||
@@ -190,36 +177,6 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
tabStore.initHomeTab();
|
||||
}
|
||||
|
||||
/** Init static auth route */
|
||||
async function initStaticAuthRoute() {
|
||||
const { authRoutes: staticAuthRoutes } = createStaticRoutes();
|
||||
const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []);
|
||||
|
||||
addAuthRoutes(filteredAuthRoutes);
|
||||
|
||||
handleAuthRoutes();
|
||||
|
||||
setIsInitAuthRoute(true);
|
||||
}
|
||||
|
||||
/** Init dynamic auth route */
|
||||
async function initDynamicAuthRoute() {
|
||||
const { data: routes, error } = await doGetUserRoutes();
|
||||
if (!error) {
|
||||
addAuthRoutes(routes);
|
||||
|
||||
handleAuthRoutes();
|
||||
|
||||
setRouteHome('home');
|
||||
|
||||
handleUpdateRootRouteRedirect('home');
|
||||
|
||||
setIsInitAuthRoute(true);
|
||||
} else {
|
||||
await authStore.resetStore();
|
||||
}
|
||||
}
|
||||
|
||||
/** handle auth routes */
|
||||
function handleAuthRoutes() {
|
||||
const sortRoutes = sortRoutesByOrder(authRoutes.value);
|
||||
|
||||
@@ -57,7 +57,7 @@ function sortRouteByOrder(route: ElegantConstRoute) {
|
||||
* @param routes routes
|
||||
*/
|
||||
export function sortRoutesByOrder(routes: ElegantConstRoute[]) {
|
||||
routes.sort((next, prev) => (Number(next.meta?.order) || 0) - (Number(prev.meta?.order) || 0));
|
||||
routes.sort((next, prev) => (Number(next.meta?.order) || 1) - (Number(prev.meta?.order) || 1));
|
||||
routes.forEach(sortRouteByOrder);
|
||||
|
||||
return routes;
|
||||
|
||||
Reference in New Issue
Block a user