|
@@ -0,0 +1,97 @@
|
|
|
+import { NavigationGuardNext, RouteLocationNormalized } from "vue-router";
|
|
|
+
|
|
|
+import router from "./index";
|
|
|
+import store from "@/store";
|
|
|
+
|
|
|
+import Layout from "@/layout/index.vue";
|
|
|
+
|
|
|
+import { RouteConfig } from "@/types/route";
|
|
|
+import { ActionType } from "@/store/modules/app/actions";
|
|
|
+
|
|
|
+const WHITE_URL_LIST: string[] = ["/login", "/forget"];
|
|
|
+
|
|
|
+/**
|
|
|
+ * 权限动态路由合并
|
|
|
+ * TODO 外部封装next函数有问题
|
|
|
+ * @param to
|
|
|
+ * @param next
|
|
|
+ */
|
|
|
+const generatorRoutes = async (
|
|
|
+ to: RouteLocationNormalized,
|
|
|
+ next: NavigationGuardNext
|
|
|
+) => {
|
|
|
+ const roles = store.getters["system/userRoles"];
|
|
|
+ if (roles.length) next();
|
|
|
+ else {
|
|
|
+ const userRoles: string[] = await store.dispatch("system/getUserRoles");
|
|
|
+ const webRoutes: RouteConfig[] = await store.dispatch(
|
|
|
+ "system/generatorRoutes",
|
|
|
+ userRoles
|
|
|
+ );
|
|
|
+ const AppRoute: RouteConfig = {
|
|
|
+ name: "home",
|
|
|
+ path: "/",
|
|
|
+ redirect: webRoutes[0].path,
|
|
|
+ component: Layout,
|
|
|
+ children: webRoutes,
|
|
|
+ };
|
|
|
+ router.addRoute(AppRoute);
|
|
|
+ next({ ...to, replace: true, name: to.name! });
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 未登录的情况下 路由的处理
|
|
|
+ * @param to
|
|
|
+ * @param next
|
|
|
+ */
|
|
|
+const redirectRoutes = (
|
|
|
+ to: RouteLocationNormalized,
|
|
|
+ next: NavigationGuardNext
|
|
|
+) => {
|
|
|
+ if (!!~WHITE_URL_LIST.indexOf(to.path)) next();
|
|
|
+ else
|
|
|
+ next({
|
|
|
+ path: "/login",
|
|
|
+ query: {
|
|
|
+ redirect: encodeURIComponent(to.path),
|
|
|
+ },
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+router.beforeEach(
|
|
|
+ async (
|
|
|
+ to: RouteLocationNormalized,
|
|
|
+ _: RouteLocationNormalized,
|
|
|
+ next: NavigationGuardNext
|
|
|
+ ) => {
|
|
|
+ if (to.path === "/login") next({ path: "/", replace: true });
|
|
|
+ else {
|
|
|
+ const roles = store.state.app.roles;
|
|
|
+ if (roles.length) next();
|
|
|
+ else {
|
|
|
+ const userRoles: string[] = await store.dispatch(
|
|
|
+ ActionType.GET_USER_ROLES
|
|
|
+ );
|
|
|
+ const webRoutes: RouteConfig[] = await store.dispatch(
|
|
|
+ ActionType.INIT_ROUTES,
|
|
|
+ userRoles
|
|
|
+ );
|
|
|
+ const AppRoute: RouteConfig = {
|
|
|
+ name: "home",
|
|
|
+ path: "",
|
|
|
+ redirect: webRoutes[0].path,
|
|
|
+ component: Layout,
|
|
|
+ children: webRoutes,
|
|
|
+ };
|
|
|
+ router.addRoute(AppRoute);
|
|
|
+ next({ ...to, replace: true, name: to.name! });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // redirectRoutes(to, next);
|
|
|
+ }
|
|
|
+);
|
|
|
+
|
|
|
+router.afterEach(() => {
|
|
|
+ // NProgress.done();
|
|
|
+});
|