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

172 lines
5.3 KiB
TypeScript
Raw Normal View History

import { defineStore } from 'pinia';
import { ROOT_ROUTE, constantRoutes, router, routes as staticRoutes } from '@/router';
import { fetchUserRoutes } from '@/service';
import {
localStg,
filterAuthRoutesByUserPermission,
getCacheRoutes,
getConstantRouteNames,
transformAuthRouteToVueRoutes,
transformAuthRouteToVueRoute,
transformAuthRouteToMenu,
transformAuthRouteToSearchMenus,
transformRouteNameToRoutePath,
transformRoutePathToRouteName,
sortRoutes
} from '@/utils';
import { useAuthStore } from '../auth';
2022-01-20 00:56:59 +08:00
import { useTabStore } from '../tab';
interface RouteState {
2022-03-30 01:19:37 +08:00
/**
* :
* - static -
* - dynamic -
*/
authRouteMode: ImportMetaEnv['VITE_AUTH_ROUTE_MODE'];
/** 是否初始化了权限路由 */
isInitAuthRoute: boolean;
2022-03-30 01:19:37 +08:00
/** 路由首页name(前端静态路由时生效,后端动态路由该值会被后端返回的值覆盖) */
routeHomeName: AuthRoute.AllRouteKey;
2022-01-11 08:22:31 +08:00
/** 菜单 */
menus: App.GlobalMenuOption[];
/** 搜索的菜单 */
searchMenus: AuthRoute.Route[];
/** 缓存的路由名称 */
cacheRoutes: string[];
}
export const useRouteStore = defineStore('route-store', {
state: (): RouteState => ({
2022-03-30 01:19:37 +08:00
authRouteMode: import.meta.env.VITE_AUTH_ROUTE_MODE,
isInitAuthRoute: false,
routeHomeName: transformRoutePathToRouteName(import.meta.env.VITE_ROUTE_HOME_PATH),
menus: [],
searchMenus: [],
cacheRoutes: []
}),
actions: {
/** 重置路由的store */
resetRouteStore() {
this.resetRoutes();
this.$reset();
},
/** 重置路由数据,保留固定路由 */
resetRoutes() {
const routes = router.getRoutes();
routes.forEach(route => {
const name = (route.name || 'root') as AuthRoute.AllRouteKey;
if (!this.isConstantRoute(name)) {
router.removeRoute(name);
}
});
},
/**
*
* @param name
*/
isConstantRoute(name: AuthRoute.AllRouteKey) {
const constantRouteNames = getConstantRouteNames(constantRoutes);
return constantRouteNames.includes(name);
},
/**
*
* @param name
*/
isValidConstantRoute(name: AuthRoute.AllRouteKey) {
const NOT_FOUND_PAGE_NAME: AuthRoute.NotFoundRouteKey = 'not-found';
const constantRouteNames = getConstantRouteNames(constantRoutes);
return constantRouteNames.includes(name) && name !== NOT_FOUND_PAGE_NAME;
},
2022-03-30 01:19:37 +08:00
/**
*
* @param routes -
*/
handleAuthRoute(routes: AuthRoute.Route[]) {
(this.menus as App.GlobalMenuOption[]) = transformAuthRouteToMenu(routes);
this.searchMenus = transformAuthRouteToSearchMenus(routes);
2022-03-30 01:19:37 +08:00
const vueRoutes = transformAuthRouteToVueRoutes(routes);
vueRoutes.forEach(route => {
2022-03-30 01:19:37 +08:00
router.addRoute(route);
});
this.cacheRoutes = getCacheRoutes(vueRoutes);
},
/** 动态路由模式下:更新根路由的重定向 */
handleUpdateRootRedirect(routeKey: AuthRoute.AllRouteKey) {
if (routeKey === 'root' || routeKey === 'not-found') {
throw new Error('routeKey的值不能为root或者not-found');
}
const rootRoute: AuthRoute.Route = { ...ROOT_ROUTE, redirect: transformRouteNameToRoutePath(routeKey) };
const rootRouteName: AuthRoute.AllRouteKey = 'root';
router.removeRoute(rootRouteName);
const rootVueRoute = transformAuthRouteToVueRoute(rootRoute)[0];
router.addRoute(rootVueRoute);
},
/** 初始化动态路由 */
async initDynamicRoute() {
const { resetAuthStore } = useAuthStore();
const { initHomeTab } = useTabStore();
const { userId } = localStg.get('userInfo') || {};
if (!userId) {
throw new Error('userId 不能为空!');
}
const { error, data } = await fetchUserRoutes(userId);
if (!error) {
2023-08-23 15:41:40 +08:00
this.handleAuthRoute(sortRoutes(data.routes));
// home相关处理需要在最后否则会出现找不到主页404的情况
2022-01-20 00:56:59 +08:00
this.routeHomeName = data.home;
this.handleUpdateRootRedirect(data.home);
initHomeTab(data.home, router);
this.isInitAuthRoute = true;
} else {
resetAuthStore();
2022-03-30 01:19:37 +08:00
}
},
/** 初始化静态路由 */
async initStaticRoute() {
const { initHomeTab } = useTabStore();
const auth = useAuthStore();
const routes = filterAuthRoutesByUserPermission(staticRoutes, auth.userInfo.userRole);
this.handleAuthRoute(routes);
initHomeTab(this.routeHomeName, router);
this.isInitAuthRoute = true;
2022-03-30 01:19:37 +08:00
},
/** 初始化权限路由 */
async initAuthRoute() {
if (this.authRouteMode === 'dynamic') {
await this.initDynamicRoute();
2022-03-30 01:19:37 +08:00
} else {
await this.initStaticRoute();
}
2023-03-01 13:56:04 +08:00
},
/** 从缓存路由中去除某个路由 */
removeCacheRoute(name: AuthRoute.AllRouteKey) {
const index = this.cacheRoutes.indexOf(name);
if (index > -1) {
this.cacheRoutes.splice(index, 1);
}
},
/** 添加某个缓存路由 */
addCacheRoute(name: AuthRoute.AllRouteKey) {
const index = this.cacheRoutes.indexOf(name);
if (index === -1) {
this.cacheRoutes.push(name);
}
}
}
});