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

113 lines
3.3 KiB
TypeScript
Raw Normal View History

import { defineStore } from 'pinia';
import { router, constantRoutes, routes as staticRoutes } from '@/router';
import { fetchUserRoutes } from '@/service';
import {
getUserInfo,
transformAuthRouteToMenu,
transformAuthRoutesToVueRoutes,
transformAuthRoutesToSearchMenus,
getCacheRoutes,
filterAuthRoutesByUserPermission,
transformRoutePathToRouteName,
getConstantRouteNames
} 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(前端静态路由时生效,后端动态路由该值会被后端返回的值覆盖) */
2022-01-20 00:56:59 +08:00
routeHomeName: AuthRoute.RouteKey;
2022-01-11 08:22:31 +08:00
/** 菜单 */
menus: 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();
const constantRouteNames = getConstantRouteNames(constantRoutes);
routes.forEach(route => {
const name: AuthRoute.RouteKey = (route.name || 'root') as AuthRoute.RouteKey;
if (!constantRouteNames.includes(name)) {
router.removeRoute(name);
}
});
},
2022-03-30 01:19:37 +08:00
/**
*
* @param routes -
*/
handleAuthRoutes(routes: AuthRoute.Route[]) {
2022-03-30 01:19:37 +08:00
this.menus = transformAuthRouteToMenu(routes);
this.searchMenus = transformAuthRoutesToSearchMenus(routes);
const vueRoutes = transformAuthRoutesToVueRoutes(routes);
vueRoutes.forEach(route => {
2022-03-30 01:19:37 +08:00
router.addRoute(route);
});
this.cacheRoutes = getCacheRoutes(vueRoutes);
},
/** 初始化动态路由 */
async initDynamicRoute() {
const { userId } = getUserInfo();
const { data } = await fetchUserRoutes(userId);
if (data) {
2022-01-20 00:56:59 +08:00
this.routeHomeName = data.home;
this.handleAuthRoutes(data.routes);
2022-03-30 01:19:37 +08:00
}
},
/** 初始化静态路由 */
async initStaticRoute() {
const auth = useAuthStore();
const routes = filterAuthRoutesByUserPermission(staticRoutes, auth.userInfo.userRole);
this.handleAuthRoutes(routes);
2022-03-30 01:19:37 +08:00
},
/** 初始化权限路由 */
async initAuthRoute() {
2022-03-30 01:19:37 +08:00
const { initHomeTab } = useTabStore();
const { userId } = getUserInfo();
2022-03-30 01:19:37 +08:00
if (!userId) return;
2022-03-30 01:19:37 +08:00
const isDynamicRoute = this.authRouteMode === 'dynamic';
if (isDynamicRoute) {
await this.initDynamicRoute();
2022-03-30 01:19:37 +08:00
} else {
await this.initStaticRoute();
}
2022-03-30 01:19:37 +08:00
initHomeTab(this.routeHomeName, router);
this.isInitAuthRoute = true;
}
}
});