2
0

fix: 路由加载合并排序(还存在切换账号菜单缓存问题)

This commit is contained in:
TsMask
2024-12-02 20:03:31 +08:00
parent f6bfceab29
commit 18dacb0c75
5 changed files with 37 additions and 92 deletions

View File

@@ -1,5 +1,5 @@
import { useRouter } from 'vue-router';
import type {RouteLocationRaw} from 'vue-router'; import type {RouteLocationRaw} from 'vue-router';
import {useRouter} from 'vue-router';
import type {RouteKey} from '@elegant-router/types'; import type {RouteKey} from '@elegant-router/types';
import {router as globalRouter} from '@/router'; import {router as globalRouter} from '@/router';
@@ -53,7 +53,6 @@ export function useRouterPush(inSetup = true) {
*/ */
async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) { async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) {
const module = loginModule || 'pwd-login'; const module = loginModule || 'pwd-login';
const options: RouterPushOptions = { const options: RouterPushOptions = {
params: { params: {
module module

View File

@@ -79,6 +79,8 @@ export const generatedRoutes: GeneratedRoute[] = [
i18nKey: 'route.user-center' i18nKey: 'route.user-center'
} }
}, },
// 下面是管理员角色才有
{ {
name: 'function', name: 'function',
path: '/function', path: '/function',
@@ -87,7 +89,7 @@ export const generatedRoutes: GeneratedRoute[] = [
title: 'function', title: 'function',
i18nKey: 'route.function', i18nKey: 'route.function',
icon: 'icon-park-outline:all-application', icon: 'icon-park-outline:all-application',
order: 6, order: 1002,
roles: ['admin'], roles: ['admin'],
}, },
children: [ children: [
@@ -205,7 +207,7 @@ export const generatedRoutes: GeneratedRoute[] = [
title: 'about', title: 'about',
i18nKey: 'route.about', i18nKey: 'route.about',
icon: 'fluent:book-information-24-regular', icon: 'fluent:book-information-24-regular',
order: 100, order: 1003,
roles: ['admin'] roles: ['admin']
} }
}, },

View File

@@ -5,7 +5,7 @@
import type {RouteRecordRaw} from 'vue-router'; import type {RouteRecordRaw} from 'vue-router';
import type {ElegantConstRoute} from '@elegant-router/vue'; 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 BaseLayout from '@/layouts/base-layout/index.vue';
import BlankLayout from '@/layouts/blank-layout/index.vue'; import BlankLayout from '@/layouts/blank-layout/index.vue';
@@ -208,18 +208,5 @@ const routeMap: RouteMap = {
* @param name route name * @param name route name
*/ */
export function getRoutePath<T extends RouteKey>(name: T) { export function getRoutePath<T extends RouteKey>(name: T) {
console.log(name);
return routeMap[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;
}

View File

@@ -146,17 +146,9 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
async function initConstantRoute() { async function initConstantRoute() {
if (isInitConstantRoute.value) return; if (isInitConstantRoute.value) return;
// if (authRouteMode.value === 'static') {
const {constantRoutes} = createStaticRoutes(); const {constantRoutes} = createStaticRoutes();
addAuthRoutes(constantRoutes); addAuthRoutes(constantRoutes);
// } else {
// const { data, error } = await fetchGetConstantRoutes();
// if (!error) {
// addAuthRoutes(data);
// }
// }
handleAuthRoutes(); handleAuthRoutes();
@@ -165,17 +157,12 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
/** Init auth route */ /** Init auth route */
async function initAuthRoute() { async function initAuthRoute() {
// await initStaticAuthRoute();
// await initDynamicAuthRoute();
const {data: routes, error} = await doGetUserRoutes(); const {data: routes, error} = await doGetUserRoutes();
if (!error) { if (!error) {
const {authRoutes: staticAuthRoutes} = createStaticRoutes(); const {authRoutes: staticAuthRoutes} = createStaticRoutes();
const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []); const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles ?? []);
console.log(authStore.userInfo.roles,filteredAuthRoutes) addAuthRoutes(filteredAuthRoutes.concat(routes));
console.log(authStore.userInfo.roles,[ ...filteredAuthRoutes,...routes])
addAuthRoutes([ ...filteredAuthRoutes,...routes]);
handleAuthRoutes(); handleAuthRoutes();
@@ -190,36 +177,6 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
tabStore.initHomeTab(); 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 */ /** handle auth routes */
function handleAuthRoutes() { function handleAuthRoutes() {
const sortRoutes = sortRoutesByOrder(authRoutes.value); const sortRoutes = sortRoutesByOrder(authRoutes.value);

View File

@@ -57,7 +57,7 @@ function sortRouteByOrder(route: ElegantConstRoute) {
* @param routes routes * @param routes routes
*/ */
export function sortRoutesByOrder(routes: ElegantConstRoute[]) { 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); routes.forEach(sortRouteByOrder);
return routes; return routes;