2022-01-03 22:20:10 +08:00
|
|
|
import { defineStore } from 'pinia';
|
2022-05-09 23:51:19 +08:00
|
|
|
import { router, constantRoutes, 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 {
|
|
|
|
getUserInfo,
|
|
|
|
transformAuthRouteToMenu,
|
|
|
|
transformAuthRoutesToVueRoutes,
|
2022-03-05 01:55:21 +08:00
|
|
|
transformAuthRoutesToSearchMenus,
|
2022-04-23 02:21:02 +08:00
|
|
|
getCacheRoutes,
|
|
|
|
filterAuthRoutesByUserPermission,
|
2022-05-09 23:51:19 +08:00
|
|
|
transformRoutePathToRouteName,
|
|
|
|
getConstantRouteNames
|
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-01-20 00:56:59 +08:00
|
|
|
routeHomeName: AuthRoute.RouteKey;
|
2022-01-11 08:22:31 +08:00
|
|
|
/** 菜单 */
|
2022-01-16 20:13:11 +08:00
|
|
|
menus: 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();
|
|
|
|
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 - 权限路由
|
|
|
|
*/
|
2022-05-09 23:51:19 +08:00
|
|
|
handleAuthRoutes(routes: AuthRoute.Route[]) {
|
2022-03-30 01:19:37 +08:00
|
|
|
this.menus = transformAuthRouteToMenu(routes);
|
|
|
|
this.searchMenus = transformAuthRoutesToSearchMenus(routes);
|
|
|
|
|
|
|
|
const vueRoutes = transformAuthRoutesToVueRoutes(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-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-05-09 23:51:19 +08:00
|
|
|
this.handleAuthRoutes(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-05-09 23:51:19 +08:00
|
|
|
this.handleAuthRoutes(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
|
|
|
});
|