ruoyi-plus-soybean/src/store/modules/route/index.ts

351 lines
8.5 KiB
TypeScript
Raw Normal View History

2024-03-25 02:42:50 +08:00
import { computed, ref, shallowRef } from 'vue';
2024-03-10 14:36:05 +08:00
import type { RouteRecordRaw } from 'vue-router';
import { defineStore } from 'pinia';
2023-11-17 08:45:00 +08:00
import { useBoolean } from '@sa/hooks';
2023-12-14 21:45:29 +08:00
import type { CustomRoute, ElegantConstRoute, LastLevelRouteKey, RouteKey, RouteMap } from '@elegant-router/types';
2023-11-17 08:45:00 +08:00
import { SetupStoreId } from '@/enum';
import { router } from '@/router';
2024-03-25 02:42:50 +08:00
import { createStaticRoutes, getAuthVueRoutes } from '@/router/routes';
import { ROOT_ROUTE } from '@/router/routes/builtin';
2023-12-14 21:45:29 +08:00
import { getRouteName, getRoutePath } from '@/router/elegant/transform';
2024-03-25 02:42:50 +08:00
import { fetchGetConstantRoutes, fetchGetUserRoutes, fetchIsRouteExist } from '@/service/api';
2023-12-14 21:45:29 +08:00
import { useAppStore } from '../app';
import { useAuthStore } from '../auth';
import { useTabStore } from '../tab';
import {
2023-11-17 08:45:00 +08:00
filterAuthRoutesByRoles,
2023-12-14 21:45:29 +08:00
getBreadcrumbsByRoute,
2023-11-17 08:45:00 +08:00
getCacheRouteNames,
2023-12-14 21:45:29 +08:00
getGlobalMenusByAuthRoutes,
2023-11-17 08:45:00 +08:00
getSelectedMenuKeyPathByKey,
2023-12-14 21:45:29 +08:00
isRouteExistByRouteName,
sortRoutesByOrder,
transformMenuToSearchMenus,
2023-12-14 21:45:29 +08:00
updateLocaleOfGlobalMenus
2023-11-17 08:45:00 +08:00
} from './shared';
2023-11-17 08:45:00 +08:00
export const useRouteStore = defineStore(SetupStoreId.Route, () => {
const appStore = useAppStore();
const authStore = useAuthStore();
const tabStore = useTabStore();
2024-03-25 02:42:50 +08:00
const { bool: isInitConstantRoute, setBool: setIsInitConstantRoute } = useBoolean();
2023-11-17 08:45:00 +08:00
const { bool: isInitAuthRoute, setBool: setIsInitAuthRoute } = useBoolean();
/**
2023-12-14 21:45:29 +08:00
* Auth route mode
*
* It recommends to use static mode in the development environment, and use dynamic mode in the production
* environment, if use static mode in development environment, the auth routes will be auto generated by plugin
* "@elegant-router/vue"
2023-11-17 08:45:00 +08:00
*/
const authRouteMode = ref(import.meta.env.VITE_AUTH_ROUTE_MODE);
2023-12-14 21:45:29 +08:00
/** Home route key */
2023-11-17 08:45:00 +08:00
const routeHome = ref(import.meta.env.VITE_ROUTE_HOME);
/**
2023-12-14 21:45:29 +08:00
* Set route home
*
* @param routeKey Route key
2023-11-17 08:45:00 +08:00
*/
function setRouteHome(routeKey: LastLevelRouteKey) {
routeHome.value = routeKey;
}
2024-03-25 02:42:50 +08:00
/** auth routes */
const authRoutes = shallowRef<ElegantConstRoute[]>([]);
function addAuthRoutes(routes: ElegantConstRoute[]) {
2024-03-25 03:13:53 +08:00
const authRoutesMap = new Map(authRoutes.value.map(route => [route.name, route]));
routes.forEach(route => {
authRoutesMap.set(route.name, route);
});
authRoutes.value = Array.from(authRoutesMap.values());
2024-03-25 02:42:50 +08:00
}
const removeRouteFns: (() => void)[] = [];
2023-12-14 21:45:29 +08:00
/** Global menus */
2023-11-17 08:45:00 +08:00
const menus = ref<App.Global.Menu[]>([]);
const searchMenus = computed(() => transformMenuToSearchMenus(menus.value));
2023-11-17 08:45:00 +08:00
2023-12-14 21:45:29 +08:00
/** Get global menus */
2023-11-17 08:45:00 +08:00
function getGlobalMenus(routes: ElegantConstRoute[]) {
menus.value = getGlobalMenusByAuthRoutes(routes);
}
2023-12-14 21:45:29 +08:00
/** Update global menus by locale */
2023-11-17 08:45:00 +08:00
function updateGlobalMenusByLocale() {
menus.value = updateLocaleOfGlobalMenus(menus.value);
}
2023-12-14 21:45:29 +08:00
/** Cache routes */
2023-11-17 08:45:00 +08:00
const cacheRoutes = ref<RouteKey[]>([]);
/**
2023-12-14 21:45:29 +08:00
* Get cache routes
*
* @param routes Vue routes
2023-11-17 08:45:00 +08:00
*/
function getCacheRoutes(routes: RouteRecordRaw[]) {
2024-03-25 02:42:50 +08:00
cacheRoutes.value = getCacheRouteNames(routes);
2023-11-17 08:45:00 +08:00
}
/**
2023-12-14 21:45:29 +08:00
* Add cache routes
*
2023-11-17 08:45:00 +08:00
* @param routeKey
*/
function addCacheRoutes(routeKey: RouteKey) {
if (cacheRoutes.value.includes(routeKey)) return;
cacheRoutes.value.push(routeKey);
}
/**
2023-12-14 21:45:29 +08:00
* Remove cache routes
*
2023-11-17 08:45:00 +08:00
* @param routeKey
*/
function removeCacheRoutes(routeKey: RouteKey) {
const index = cacheRoutes.value.findIndex(item => item === routeKey);
if (index === -1) return;
cacheRoutes.value.splice(index, 1);
}
/**
2024-03-24 03:02:08 +08:00
* Re cache routes by route key
2023-12-14 21:45:29 +08:00
*
2023-11-17 08:45:00 +08:00
* @param routeKey
*/
async function reCacheRoutesByKey(routeKey: RouteKey) {
removeCacheRoutes(routeKey);
await appStore.reloadPage();
addCacheRoutes(routeKey);
}
/**
2024-03-24 03:02:08 +08:00
* Re cache routes by route keys
2023-12-14 21:45:29 +08:00
*
2023-11-17 08:45:00 +08:00
* @param routeKeys
*/
async function reCacheRoutesByKeys(routeKeys: RouteKey[]) {
for await (const key of routeKeys) {
await reCacheRoutesByKey(key);
}
}
2023-12-14 21:45:29 +08:00
/** Global breadcrumbs */
2023-11-17 08:45:00 +08:00
const breadcrumbs = computed(() => getBreadcrumbsByRoute(router.currentRoute.value, menus.value));
2023-12-14 21:45:29 +08:00
/** Reset store */
2023-11-17 08:45:00 +08:00
async function resetStore() {
const routeStore = useRouteStore();
routeStore.$reset();
resetVueRoutes();
2024-03-25 08:45:00 +08:00
// after reset store, need to re-init constant route
await initConstantRoute();
2023-11-17 08:45:00 +08:00
}
2023-12-14 21:45:29 +08:00
/** Reset vue routes */
2023-11-17 08:45:00 +08:00
function resetVueRoutes() {
removeRouteFns.forEach(fn => fn());
removeRouteFns.length = 0;
}
2024-03-25 02:42:50 +08:00
/** init constant 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();
setIsInitConstantRoute(true);
}
2023-12-14 21:45:29 +08:00
/** Init auth route */
2023-11-17 08:45:00 +08:00
async function initAuthRoute() {
if (authRouteMode.value === 'static') {
await initStaticAuthRoute();
} else {
await initDynamicAuthRoute();
}
tabStore.initHomeTab();
2023-11-17 08:45:00 +08:00
}
2023-12-14 21:45:29 +08:00
/** Init static auth route */
2023-11-17 08:45:00 +08:00
async function initStaticAuthRoute() {
2024-03-25 02:42:50 +08:00
const { authRoutes: staticAuthRoutes } = createStaticRoutes();
2023-11-17 08:45:00 +08:00
2024-03-25 02:42:50 +08:00
if (authStore.isStaticSuper) {
addAuthRoutes(staticAuthRoutes);
} else {
const filteredAuthRoutes = filterAuthRoutesByRoles(staticAuthRoutes, authStore.userInfo.roles);
2023-11-17 08:45:00 +08:00
2024-03-25 02:42:50 +08:00
addAuthRoutes(filteredAuthRoutes);
}
handleAuthRoutes();
2023-11-17 08:45:00 +08:00
setIsInitAuthRoute(true);
}
2023-12-14 21:45:29 +08:00
/** Init dynamic auth route */
2023-11-17 08:45:00 +08:00
async function initDynamicAuthRoute() {
const { data, error } = await fetchGetUserRoutes();
2023-11-17 08:45:00 +08:00
if (!error) {
const { routes, home } = data;
2023-11-17 08:45:00 +08:00
2024-03-25 02:42:50 +08:00
addAuthRoutes(routes);
handleAuthRoutes();
2023-11-17 08:45:00 +08:00
setRouteHome(home);
2023-11-17 08:45:00 +08:00
handleUpdateRootRouteRedirect(home);
setIsInitAuthRoute(true);
}
2023-11-17 08:45:00 +08:00
}
2024-03-25 02:42:50 +08:00
/** handle auth routes */
function handleAuthRoutes() {
const sortRoutes = sortRoutesByOrder(authRoutes.value);
const vueRoutes = getAuthVueRoutes(sortRoutes);
2023-11-17 08:45:00 +08:00
2024-03-25 03:13:53 +08:00
resetVueRoutes();
2023-11-17 08:45:00 +08:00
addRoutesToVueRouter(vueRoutes);
getGlobalMenus(sortRoutes);
2023-11-17 08:45:00 +08:00
getCacheRoutes(vueRoutes);
}
/**
2023-12-14 21:45:29 +08:00
* Add routes to vue router
*
* @param routes Vue routes
2023-11-17 08:45:00 +08:00
*/
function addRoutesToVueRouter(routes: RouteRecordRaw[]) {
routes.forEach(route => {
const removeFn = router.addRoute(route);
addRemoveRouteFn(removeFn);
});
}
/**
2023-12-14 21:45:29 +08:00
* Add remove route fn
*
2023-11-17 08:45:00 +08:00
* @param fn
*/
function addRemoveRouteFn(fn: () => void) {
removeRouteFns.push(fn);
}
/**
2023-12-14 21:45:29 +08:00
* Update root route redirect when auth route mode is dynamic
*
* @param redirectKey Redirect route key
2023-11-17 08:45:00 +08:00
*/
function handleUpdateRootRouteRedirect(redirectKey: LastLevelRouteKey) {
const redirect = getRoutePath(redirectKey);
if (redirect) {
const rootRoute: CustomRoute = { ...ROOT_ROUTE, redirect };
router.removeRoute(rootRoute.name);
const [rootVueRoute] = getAuthVueRoutes([rootRoute]);
router.addRoute(rootVueRoute);
}
}
2023-11-17 08:45:00 +08:00
/**
2023-12-14 21:45:29 +08:00
* Get is auth route exist
*
* @param routePath Route path
2023-11-17 08:45:00 +08:00
*/
async function getIsAuthRouteExist(routePath: RouteMap[RouteKey]) {
const routeName = getRouteName(routePath);
if (!routeName) {
return false;
}
if (authRouteMode.value === 'static') {
2024-03-25 03:13:53 +08:00
const { authRoutes: staticAuthRoutes } = createStaticRoutes();
return isRouteExistByRouteName(routeName, staticAuthRoutes);
2023-11-17 08:45:00 +08:00
}
const { data } = await fetchIsRouteExist(routeName);
return data;
}
/**
2023-12-14 21:45:29 +08:00
* Get selected menu key path
*
* @param selectedKey Selected menu key
2023-11-17 08:45:00 +08:00
*/
function getSelectedMenuKeyPath(selectedKey: string) {
return getSelectedMenuKeyPathByKey(selectedKey, menus.value);
}
/**
* Get selected menu meta by key
*
* @param selectedKey Selected menu key
*/
2024-03-10 14:36:05 +08:00
function getSelectedMenuMetaByKey(selectedKey: string) {
// The routes in router.options.routes are static, you need to use router.getRoutes() to get all the routes.
2024-03-10 14:36:05 +08:00
const allRoutes = router.getRoutes();
return allRoutes.find(route => route.name === selectedKey)?.meta || null;
}
2023-11-17 08:45:00 +08:00
return {
resetStore,
routeHome,
menus,
searchMenus,
2023-11-17 08:45:00 +08:00
updateGlobalMenusByLocale,
cacheRoutes,
reCacheRoutesByKey,
reCacheRoutesByKeys,
breadcrumbs,
2024-03-25 02:42:50 +08:00
initConstantRoute,
isInitConstantRoute,
2023-11-17 08:45:00 +08:00
initAuthRoute,
isInitAuthRoute,
setIsInitAuthRoute,
getIsAuthRouteExist,
getSelectedMenuKeyPath,
getSelectedMenuMetaByKey
2023-11-17 08:45:00 +08:00
};
});