2022-01-03 22:20:10 +08:00
|
|
|
import { defineStore } from 'pinia';
|
2022-08-10 21:31:59 +08:00
|
|
|
import { ROOT_ROUTE, constantRoutes, router, routes as staticRoutes } from '@/router';
|
2022-01-03 22:20:10 +08:00
|
|
|
import { fetchUserRoutes } from '@/service';
|
2022-01-24 15:34:57 +08:00
|
|
|
import {
|
2022-08-10 21:31:59 +08:00
|
|
|
filterAuthRoutesByUserPermission,
|
|
|
|
getCacheRoutes,
|
|
|
|
getConstantRouteNames,
|
2022-01-24 15:34:57 +08:00
|
|
|
getUserInfo,
|
2022-11-08 18:24:32 +08:00
|
|
|
transformAuthRouteToVueRoutes,
|
|
|
|
transformAuthRouteToVueRoute,
|
2022-01-24 15:34:57 +08:00
|
|
|
transformAuthRouteToMenu,
|
2022-11-08 01:14:59 +08:00
|
|
|
transformAuthRouteToSearchMenus,
|
2022-06-06 22:44:53 +08:00
|
|
|
transformRouteNameToRoutePath,
|
2022-08-10 21:31:59 +08:00
|
|
|
transformRoutePathToRouteName
|
2022-01-24 15:34:57 +08:00
|
|
|
} from '@/utils';
|
2022-04-23 02:21:02 +08:00
|
|
|
import { useAuthStore } from '../auth';
|
2022-01-20 00:56:59 +08:00
|
|
|
import { useTabStore } from '../tab';
|
2022-01-03 22:20:10 +08:00
|
|
|
|
2022-01-16 20:13:11 +08:00
|
|
|
interface RouteState {
|
2022-03-30 01:19:37 +08:00
|
|
|
/**
|
|
|
|
* 权限路由模式:
|
|
|
|
* - static - 前端声明的静态
|
|
|
|
* - dynamic - 后端返回的动态
|
|
|
|
*/
|
|
|
|
authRouteMode: ImportMetaEnv['VITE_AUTH_ROUTE_MODE'];
|
|
|
|
/** 是否初始化了权限路由 */
|
2022-04-23 02:21:02 +08:00
|
|
|
isInitAuthRoute: boolean;
|
2022-03-30 01:19:37 +08:00
|
|
|
/** 路由首页name(前端静态路由时生效,后端动态路由该值会被后端返回的值覆盖) */
|
2022-11-08 01:14:59 +08:00
|
|
|
routeHomeName: AuthRoute.AllRouteKey;
|
2022-01-11 08:22:31 +08:00
|
|
|
/** 菜单 */
|
2022-11-08 01:14:59 +08:00
|
|
|
menus: App.GlobalMenuOption[];
|
2022-03-05 01:55:21 +08:00
|
|
|
/** 搜索的菜单 */
|
2022-03-12 17:45:37 +08:00
|
|
|
searchMenus: AuthRoute.Route[];
|
2022-01-21 23:59:14 +08:00
|
|
|
/** 缓存的路由名称 */
|
|
|
|
cacheRoutes: string[];
|
2022-01-03 22:20:10 +08:00
|
|
|
}
|
|
|
|
|
2022-01-16 20:13:11 +08:00
|
|
|
export const useRouteStore = defineStore('route-store', {
|
|
|
|
state: (): RouteState => ({
|
2022-03-30 01:19:37 +08:00
|
|
|
authRouteMode: import.meta.env.VITE_AUTH_ROUTE_MODE,
|
2022-04-23 02:21:02 +08:00
|
|
|
isInitAuthRoute: false,
|
|
|
|
routeHomeName: transformRoutePathToRouteName(import.meta.env.VITE_ROUTE_HOME_PATH),
|
2022-01-21 23:59:14 +08:00
|
|
|
menus: [],
|
2022-03-05 01:55:21 +08:00
|
|
|
searchMenus: [],
|
2022-04-01 14:47:57 +08:00
|
|
|
cacheRoutes: []
|
2022-01-16 20:13:11 +08:00
|
|
|
}),
|
|
|
|
actions: {
|
2022-05-18 23:43:41 +08:00
|
|
|
/** 重置路由的store */
|
2022-04-29 02:00:51 +08:00
|
|
|
resetRouteStore() {
|
2022-05-09 23:51:19 +08:00
|
|
|
this.resetRoutes();
|
2022-04-29 02:00:51 +08:00
|
|
|
this.$reset();
|
|
|
|
},
|
2022-05-09 23:51:19 +08:00
|
|
|
/** 重置路由数据,保留固定路由 */
|
|
|
|
resetRoutes() {
|
|
|
|
const routes = router.getRoutes();
|
|
|
|
routes.forEach(route => {
|
2022-11-08 01:14:59 +08:00
|
|
|
const name = (route.name || 'root') as AuthRoute.AllRouteKey;
|
2022-09-21 12:45:00 +08:00
|
|
|
if (!this.isConstantRoute(name)) {
|
2022-05-09 23:51:19 +08:00
|
|
|
router.removeRoute(name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2022-09-21 12:45:00 +08:00
|
|
|
/**
|
|
|
|
* 是否是固定路由
|
|
|
|
* @param name 路由名称
|
|
|
|
*/
|
2022-11-08 01:14:59 +08:00
|
|
|
isConstantRoute(name: AuthRoute.AllRouteKey) {
|
2022-09-21 12:45:00 +08:00
|
|
|
const constantRouteNames = getConstantRouteNames(constantRoutes);
|
|
|
|
return constantRouteNames.includes(name);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* 是否是有效的固定路由
|
|
|
|
* @param name 路由名称
|
|
|
|
*/
|
2022-11-08 01:14:59 +08:00
|
|
|
isValidConstantRoute(name: AuthRoute.AllRouteKey) {
|
|
|
|
const NOT_FOUND_PAGE_NAME: AuthRoute.NotFoundRouteKey = 'not-found';
|
2022-09-21 12:45:00 +08:00
|
|
|
const constantRouteNames = getConstantRouteNames(constantRoutes);
|
|
|
|
return constantRouteNames.includes(name) && name !== NOT_FOUND_PAGE_NAME;
|
|
|
|
},
|
2022-03-30 01:19:37 +08:00
|
|
|
/**
|
|
|
|
* 处理权限路由
|
|
|
|
* @param routes - 权限路由
|
|
|
|
*/
|
2022-11-08 01:14:59 +08:00
|
|
|
handleAuthRoute(routes: AuthRoute.Route[]) {
|
|
|
|
(this.menus as App.GlobalMenuOption[]) = transformAuthRouteToMenu(routes);
|
|
|
|
this.searchMenus = transformAuthRouteToSearchMenus(routes);
|
2022-03-30 01:19:37 +08:00
|
|
|
|
2022-11-08 01:14:59 +08:00
|
|
|
const vueRoutes = transformAuthRouteToVueRoutes(routes);
|
2022-04-29 02:00:51 +08:00
|
|
|
|
2022-04-01 14:47:57 +08:00
|
|
|
vueRoutes.forEach(route => {
|
2022-03-30 01:19:37 +08:00
|
|
|
router.addRoute(route);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.cacheRoutes = getCacheRoutes(vueRoutes);
|
|
|
|
},
|
2022-06-06 22:44:53 +08:00
|
|
|
/** 动态路由模式下:更新根路由的重定向 */
|
2022-11-08 01:14:59 +08:00
|
|
|
handleUpdateRootRedirect(routeKey: AuthRoute.AllRouteKey) {
|
|
|
|
if (routeKey === 'root' || routeKey === 'not-found') {
|
|
|
|
throw new Error('routeKey的值不能为root或者not-found');
|
2022-06-06 22:44:53 +08:00
|
|
|
}
|
|
|
|
const rootRoute: AuthRoute.Route = { ...ROOT_ROUTE, redirect: transformRouteNameToRoutePath(routeKey) };
|
2022-11-08 01:14:59 +08:00
|
|
|
const rootRouteName: AuthRoute.AllRouteKey = 'root';
|
2022-06-06 22:44:53 +08:00
|
|
|
router.removeRoute(rootRouteName);
|
|
|
|
const rootVueRoute = transformAuthRouteToVueRoute(rootRoute)[0];
|
|
|
|
router.addRoute(rootVueRoute);
|
|
|
|
},
|
2022-05-09 23:51:19 +08:00
|
|
|
/** 初始化动态路由 */
|
|
|
|
async initDynamicRoute() {
|
2022-01-22 14:05:19 +08:00
|
|
|
const { userId } = getUserInfo();
|
|
|
|
const { data } = await fetchUserRoutes(userId);
|
2022-01-16 20:13:11 +08:00
|
|
|
if (data) {
|
2022-01-20 00:56:59 +08:00
|
|
|
this.routeHomeName = data.home;
|
2022-06-06 22:44:53 +08:00
|
|
|
this.handleUpdateRootRedirect(data.home);
|
2022-11-08 01:14:59 +08:00
|
|
|
this.handleAuthRoute(data.routes);
|
2022-03-30 01:19:37 +08:00
|
|
|
}
|
|
|
|
},
|
2022-05-09 23:51:19 +08:00
|
|
|
/** 初始化静态路由 */
|
|
|
|
async initStaticRoute() {
|
2022-04-23 02:21:02 +08:00
|
|
|
const auth = useAuthStore();
|
|
|
|
const routes = filterAuthRoutesByUserPermission(staticRoutes, auth.userInfo.userRole);
|
2022-11-08 01:14:59 +08:00
|
|
|
this.handleAuthRoute(routes);
|
2022-03-30 01:19:37 +08:00
|
|
|
},
|
2022-05-09 23:51:19 +08:00
|
|
|
/** 初始化权限路由 */
|
|
|
|
async initAuthRoute() {
|
2022-03-30 01:19:37 +08:00
|
|
|
const { initHomeTab } = useTabStore();
|
|
|
|
const { userId } = getUserInfo();
|
2022-04-23 02:21:02 +08:00
|
|
|
|
2022-03-30 01:19:37 +08:00
|
|
|
if (!userId) return;
|
2022-01-21 23:59:14 +08:00
|
|
|
|
2022-03-30 01:19:37 +08:00
|
|
|
const isDynamicRoute = this.authRouteMode === 'dynamic';
|
|
|
|
if (isDynamicRoute) {
|
2022-05-09 23:51:19 +08:00
|
|
|
await this.initDynamicRoute();
|
2022-03-30 01:19:37 +08:00
|
|
|
} else {
|
2022-05-09 23:51:19 +08:00
|
|
|
await this.initStaticRoute();
|
2022-01-16 20:13:11 +08:00
|
|
|
}
|
2022-03-30 01:19:37 +08:00
|
|
|
|
|
|
|
initHomeTab(this.routeHomeName, router);
|
2022-04-23 02:21:02 +08:00
|
|
|
|
|
|
|
this.isInitAuthRoute = true;
|
2022-04-01 14:47:57 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-03 22:20:10 +08:00
|
|
|
});
|