|
@@ -1,153 +1,173 @@
|
|
|
-import type { Route } from '#/global'
|
|
|
|
|
-import type { RouteRecordRaw } from 'vue-router'
|
|
|
|
|
-import { systemRoutes } from '@/router/routes'
|
|
|
|
|
-import { resolveRoutePath } from '@/utils'
|
|
|
|
|
-import useUserStore from '@/store/modules/user'
|
|
|
|
|
-import { cloneDeep } from 'lodash-es'
|
|
|
|
|
-import useSettingsStore from './settings'
|
|
|
|
|
-import api from '@/api'
|
|
|
|
|
-import { ElMessage } from 'element-plus'
|
|
|
|
|
|
|
+import type { Route } from "#/global";
|
|
|
|
|
+import type { RouteRecordRaw } from "vue-router";
|
|
|
|
|
+import { systemRoutes } from "@/router/routes";
|
|
|
|
|
+import { resolveRoutePath } from "@/utils";
|
|
|
|
|
+import useUserStore from "@/store/modules/user";
|
|
|
|
|
+import { cloneDeep } from "lodash-es";
|
|
|
|
|
+import useSettingsStore from "./settings";
|
|
|
|
|
+import api from "@/api";
|
|
|
|
|
+import { ElMessage } from "element-plus";
|
|
|
const useRouteStore = defineStore(
|
|
const useRouteStore = defineStore(
|
|
|
// 唯一ID
|
|
// 唯一ID
|
|
|
- 'route',
|
|
|
|
|
|
|
+ "route",
|
|
|
() => {
|
|
() => {
|
|
|
- const settingsStore = useSettingsStore()
|
|
|
|
|
|
|
+ const settingsStore = useSettingsStore();
|
|
|
|
|
|
|
|
- const isGenerate = ref(false)
|
|
|
|
|
- const routesRaw = ref<Route.recordMainRaw[]>([])
|
|
|
|
|
- const filesystemRoutesRaw = ref<RouteRecordRaw[]>([])
|
|
|
|
|
- const currentRemoveRoutes = ref<(() => void)[]>([])
|
|
|
|
|
|
|
+ const isGenerate = ref(false);
|
|
|
|
|
+ const routesRaw = ref<Route.recordMainRaw[]>([]);
|
|
|
|
|
+ const filesystemRoutesRaw = ref<RouteRecordRaw[]>([]);
|
|
|
|
|
+ const currentRemoveRoutes = ref<(() => void)[]>([]);
|
|
|
|
|
|
|
|
// 将多层嵌套路由处理成两层,保留顶层和最子层路由,中间层级将被拍平
|
|
// 将多层嵌套路由处理成两层,保留顶层和最子层路由,中间层级将被拍平
|
|
|
function flatAsyncRoutes<T extends RouteRecordRaw>(route: T): T {
|
|
function flatAsyncRoutes<T extends RouteRecordRaw>(route: T): T {
|
|
|
if (route.children) {
|
|
if (route.children) {
|
|
|
- route.children = flatAsyncRoutesRecursive(route.children, [{
|
|
|
|
|
- path: route.path,
|
|
|
|
|
- title: route.meta?.title,
|
|
|
|
|
- icon: route.meta?.icon,
|
|
|
|
|
- hide: !route.meta?.breadcrumb && route.meta?.breadcrumb === false,
|
|
|
|
|
- }], route.path)
|
|
|
|
|
|
|
+ route.children = flatAsyncRoutesRecursive(
|
|
|
|
|
+ route.children,
|
|
|
|
|
+ [
|
|
|
|
|
+ {
|
|
|
|
|
+ path: route.path,
|
|
|
|
|
+ title: route.meta?.title,
|
|
|
|
|
+ icon: route.meta?.icon,
|
|
|
|
|
+ hide: !route.meta?.breadcrumb && route.meta?.breadcrumb === false,
|
|
|
|
|
+ },
|
|
|
|
|
+ ],
|
|
|
|
|
+ route.path
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
- return route
|
|
|
|
|
|
|
+ return route;
|
|
|
}
|
|
}
|
|
|
- function flatAsyncRoutesRecursive(routes: RouteRecordRaw[], breadcrumb: Route.breadcrumb[] = [], baseUrl = ''): RouteRecordRaw[] {
|
|
|
|
|
- const res: RouteRecordRaw[] = []
|
|
|
|
|
|
|
+ function flatAsyncRoutesRecursive(
|
|
|
|
|
+ routes: RouteRecordRaw[],
|
|
|
|
|
+ breadcrumb: Route.breadcrumb[] = [],
|
|
|
|
|
+ baseUrl = ""
|
|
|
|
|
+ ): RouteRecordRaw[] {
|
|
|
|
|
+ const res: RouteRecordRaw[] = [];
|
|
|
routes.forEach((route) => {
|
|
routes.forEach((route) => {
|
|
|
if (route.children) {
|
|
if (route.children) {
|
|
|
- const childrenBaseUrl = resolveRoutePath(baseUrl, route.path)
|
|
|
|
|
- const tmpBreadcrumb = cloneDeep(breadcrumb)
|
|
|
|
|
|
|
+ const childrenBaseUrl = resolveRoutePath(baseUrl, route.path);
|
|
|
|
|
+ const tmpBreadcrumb = cloneDeep(breadcrumb);
|
|
|
tmpBreadcrumb.push({
|
|
tmpBreadcrumb.push({
|
|
|
path: childrenBaseUrl,
|
|
path: childrenBaseUrl,
|
|
|
title: route.meta?.title,
|
|
title: route.meta?.title,
|
|
|
icon: route.meta?.icon,
|
|
icon: route.meta?.icon,
|
|
|
hide: !route.meta?.breadcrumb && route.meta?.breadcrumb === false,
|
|
hide: !route.meta?.breadcrumb && route.meta?.breadcrumb === false,
|
|
|
- })
|
|
|
|
|
- const tmpRoute = cloneDeep(route)
|
|
|
|
|
- tmpRoute.path = childrenBaseUrl
|
|
|
|
|
|
|
+ });
|
|
|
|
|
+ const tmpRoute = cloneDeep(route);
|
|
|
|
|
+ tmpRoute.path = childrenBaseUrl;
|
|
|
if (!tmpRoute.meta) {
|
|
if (!tmpRoute.meta) {
|
|
|
- tmpRoute.meta = {}
|
|
|
|
|
|
|
+ tmpRoute.meta = {};
|
|
|
}
|
|
}
|
|
|
- tmpRoute.meta.breadcrumbNeste = tmpBreadcrumb
|
|
|
|
|
- delete tmpRoute.children
|
|
|
|
|
- res.push(tmpRoute)
|
|
|
|
|
- const childrenRoutes = flatAsyncRoutesRecursive(route.children, tmpBreadcrumb, childrenBaseUrl)
|
|
|
|
|
|
|
+ tmpRoute.meta.breadcrumbNeste = tmpBreadcrumb;
|
|
|
|
|
+ delete tmpRoute.children;
|
|
|
|
|
+ res.push(tmpRoute);
|
|
|
|
|
+ const childrenRoutes = flatAsyncRoutesRecursive(
|
|
|
|
|
+ route.children,
|
|
|
|
|
+ tmpBreadcrumb,
|
|
|
|
|
+ childrenBaseUrl
|
|
|
|
|
+ );
|
|
|
childrenRoutes.forEach((item) => {
|
|
childrenRoutes.forEach((item) => {
|
|
|
// 如果 path 一样则覆盖,因为子路由的 path 可能设置为空,导致和父路由一样,直接注册会提示路由重复
|
|
// 如果 path 一样则覆盖,因为子路由的 path 可能设置为空,导致和父路由一样,直接注册会提示路由重复
|
|
|
- if (res.some(v => v.path === item.path)) {
|
|
|
|
|
|
|
+ if (res.some((v) => v.path === item.path)) {
|
|
|
res.forEach((v, i) => {
|
|
res.forEach((v, i) => {
|
|
|
if (v.path === item.path) {
|
|
if (v.path === item.path) {
|
|
|
- res[i] = item
|
|
|
|
|
|
|
+ res[i] = item;
|
|
|
}
|
|
}
|
|
|
- })
|
|
|
|
|
|
|
+ });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ res.push(item);
|
|
|
}
|
|
}
|
|
|
- else {
|
|
|
|
|
- res.push(item)
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- const tmpRoute = cloneDeep(route)
|
|
|
|
|
- tmpRoute.path = resolveRoutePath(baseUrl, tmpRoute.path)
|
|
|
|
|
|
|
+ });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ const tmpRoute = cloneDeep(route);
|
|
|
|
|
+ tmpRoute.path = resolveRoutePath(baseUrl, tmpRoute.path);
|
|
|
// 处理面包屑导航
|
|
// 处理面包屑导航
|
|
|
- const tmpBreadcrumb = cloneDeep(breadcrumb)
|
|
|
|
|
|
|
+ const tmpBreadcrumb = cloneDeep(breadcrumb);
|
|
|
tmpBreadcrumb.push({
|
|
tmpBreadcrumb.push({
|
|
|
path: tmpRoute.path,
|
|
path: tmpRoute.path,
|
|
|
title: tmpRoute.meta?.title,
|
|
title: tmpRoute.meta?.title,
|
|
|
icon: tmpRoute.meta?.icon,
|
|
icon: tmpRoute.meta?.icon,
|
|
|
- hide: !tmpRoute.meta?.breadcrumb && tmpRoute.meta?.breadcrumb === false,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ hide:
|
|
|
|
|
+ !tmpRoute.meta?.breadcrumb && tmpRoute.meta?.breadcrumb === false,
|
|
|
|
|
+ });
|
|
|
if (!tmpRoute.meta) {
|
|
if (!tmpRoute.meta) {
|
|
|
- tmpRoute.meta = {}
|
|
|
|
|
|
|
+ tmpRoute.meta = {};
|
|
|
}
|
|
}
|
|
|
- tmpRoute.meta.breadcrumbNeste = tmpBreadcrumb
|
|
|
|
|
- res.push(tmpRoute)
|
|
|
|
|
|
|
+ tmpRoute.meta.breadcrumbNeste = tmpBreadcrumb;
|
|
|
|
|
+ res.push(tmpRoute);
|
|
|
}
|
|
}
|
|
|
- })
|
|
|
|
|
- return res
|
|
|
|
|
|
|
+ });
|
|
|
|
|
+ return res;
|
|
|
}
|
|
}
|
|
|
// 扁平化路由(将三级及以上路由数据拍平成二级)
|
|
// 扁平化路由(将三级及以上路由数据拍平成二级)
|
|
|
const flatRoutes = computed(() => {
|
|
const flatRoutes = computed(() => {
|
|
|
- const returnRoutes: RouteRecordRaw[] = []
|
|
|
|
|
- if (settingsStore.settings.app.routeBaseOn !== 'filesystem') {
|
|
|
|
|
|
|
+ const returnRoutes: RouteRecordRaw[] = [];
|
|
|
|
|
+ if (settingsStore.settings.app.routeBaseOn !== "filesystem") {
|
|
|
if (routesRaw.value) {
|
|
if (routesRaw.value) {
|
|
|
routesRaw.value.forEach((item) => {
|
|
routesRaw.value.forEach((item) => {
|
|
|
- const tmpRoutes = cloneDeep(item.children) as RouteRecordRaw[]
|
|
|
|
|
|
|
+ const tmpRoutes = cloneDeep(item.children) as RouteRecordRaw[];
|
|
|
tmpRoutes.map((v) => {
|
|
tmpRoutes.map((v) => {
|
|
|
if (!v.meta) {
|
|
if (!v.meta) {
|
|
|
- v.meta = {}
|
|
|
|
|
|
|
+ v.meta = {};
|
|
|
}
|
|
}
|
|
|
- v.meta.auth = item.meta?.auth ?? v.meta?.auth
|
|
|
|
|
- return v
|
|
|
|
|
- })
|
|
|
|
|
- returnRoutes.push(...tmpRoutes)
|
|
|
|
|
- })
|
|
|
|
|
- returnRoutes.forEach(item => flatAsyncRoutes(item))
|
|
|
|
|
|
|
+ v.meta.auth = item.meta?.auth ?? v.meta?.auth;
|
|
|
|
|
+ return v;
|
|
|
|
|
+ });
|
|
|
|
|
+ returnRoutes.push(...tmpRoutes);
|
|
|
|
|
+ });
|
|
|
|
|
+ returnRoutes.forEach((item) => flatAsyncRoutes(item));
|
|
|
}
|
|
}
|
|
|
|
|
+ } else {
|
|
|
|
|
+ returnRoutes.push(
|
|
|
|
|
+ ...(cloneDeep(filesystemRoutesRaw.value) as RouteRecordRaw[])
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
- else {
|
|
|
|
|
- returnRoutes.push(...cloneDeep(filesystemRoutesRaw.value) as RouteRecordRaw[])
|
|
|
|
|
- }
|
|
|
|
|
- return returnRoutes
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ return returnRoutes;
|
|
|
|
|
+ });
|
|
|
const flatSystemRoutes = computed(() => {
|
|
const flatSystemRoutes = computed(() => {
|
|
|
- const routes = [...systemRoutes]
|
|
|
|
|
- routes.forEach(item => flatAsyncRoutes(item))
|
|
|
|
|
- return routes
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ const routes = [...systemRoutes];
|
|
|
|
|
+ routes.forEach((item) => flatAsyncRoutes(item));
|
|
|
|
|
+ return routes;
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
// TODO 将设置 meta.sidebar 的属性转换成 meta.menu ,过渡处理,未来将被弃用
|
|
// TODO 将设置 meta.sidebar 的属性转换成 meta.menu ,过渡处理,未来将被弃用
|
|
|
- let isUsedDeprecatedAttribute = false
|
|
|
|
|
- function converDeprecatedAttribute<T extends Route.recordMainRaw[]>(routes: T): T {
|
|
|
|
|
|
|
+ let isUsedDeprecatedAttribute = false;
|
|
|
|
|
+ function converDeprecatedAttribute<T extends Route.recordMainRaw[]>(
|
|
|
|
|
+ routes: T
|
|
|
|
|
+ ): T {
|
|
|
routes.forEach((route) => {
|
|
routes.forEach((route) => {
|
|
|
- route.children = converDeprecatedAttributeRecursive(route.children)
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ route.children = converDeprecatedAttributeRecursive(route.children);
|
|
|
|
|
+ });
|
|
|
if (isUsedDeprecatedAttribute) {
|
|
if (isUsedDeprecatedAttribute) {
|
|
|
// turbo-console-disable-next-line
|
|
// turbo-console-disable-next-line
|
|
|
- console.warn('[Fantastic-admin] 路由配置中的 "sidebar" 属性即将被弃用, 请尽快替换为 "menu" 属性')
|
|
|
|
|
|
|
+ console.warn(
|
|
|
|
|
+ '[Fantastic-admin] 路由配置中的 "sidebar" 属性即将被弃用, 请尽快替换为 "menu" 属性'
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
- return routes
|
|
|
|
|
|
|
+ return routes;
|
|
|
}
|
|
}
|
|
|
function converDeprecatedAttributeRecursive(routes: RouteRecordRaw[]) {
|
|
function converDeprecatedAttributeRecursive(routes: RouteRecordRaw[]) {
|
|
|
if (routes) {
|
|
if (routes) {
|
|
|
routes.forEach((route) => {
|
|
routes.forEach((route) => {
|
|
|
- if (typeof route.meta?.sidebar === 'boolean') {
|
|
|
|
|
- isUsedDeprecatedAttribute = true
|
|
|
|
|
- route.meta.menu = route.meta.sidebar
|
|
|
|
|
- delete route.meta.sidebar
|
|
|
|
|
|
|
+ if (typeof route.meta?.sidebar === "boolean") {
|
|
|
|
|
+ isUsedDeprecatedAttribute = true;
|
|
|
|
|
+ route.meta.menu = route.meta.sidebar;
|
|
|
|
|
+ delete route.meta.sidebar;
|
|
|
}
|
|
}
|
|
|
if (route.children) {
|
|
if (route.children) {
|
|
|
- converDeprecatedAttributeRecursive(route.children)
|
|
|
|
|
|
|
+ converDeprecatedAttributeRecursive(route.children);
|
|
|
}
|
|
}
|
|
|
- })
|
|
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
- return routes
|
|
|
|
|
|
|
+ return routes;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 生成路由(前端生成)
|
|
// 生成路由(前端生成)
|
|
|
function generateRoutesAtFront(asyncRoutes: Route.recordMainRaw[]) {
|
|
function generateRoutesAtFront(asyncRoutes: Route.recordMainRaw[]) {
|
|
|
// 设置 routes 数据
|
|
// 设置 routes 数据
|
|
|
- routesRaw.value = converDeprecatedAttribute(cloneDeep(asyncRoutes) as any)
|
|
|
|
|
- isGenerate.value = true
|
|
|
|
|
|
|
+ routesRaw.value = converDeprecatedAttribute(
|
|
|
|
|
+ cloneDeep(asyncRoutes) as any
|
|
|
|
|
+ );
|
|
|
|
|
+ isGenerate.value = true;
|
|
|
}
|
|
}
|
|
|
// 格式化后端路由数据
|
|
// 格式化后端路由数据
|
|
|
// function formatBackRoutes(routes: any, views = import.meta.glob('../../views/**/*.vue')): Route.recordMainRaw[] {
|
|
// function formatBackRoutes(routes: any, views = import.meta.glob('../../views/**/*.vue')): Route.recordMainRaw[] {
|
|
@@ -171,86 +191,106 @@ const useRouteStore = defineStore(
|
|
|
// })
|
|
// })
|
|
|
// }
|
|
// }
|
|
|
// 格式化后端路由数据
|
|
// 格式化后端路由数据
|
|
|
-function formatBackRoutes(routes: any, views = import.meta.glob('../../views/**/*.vue')): Route.recordMainRaw[] {
|
|
|
|
|
- // 需要从外层移动到meta对象的字段列表
|
|
|
|
|
- const metaFields = ['title', 'icon', 'auth', 'menu', 'breadcrumb', 'activeMenu', 'cache', 'noCache', 'link']// 从外层移动到meta对象的字段列表
|
|
|
|
|
- return routes.filter((route: any) => route.type != 2).map((route: any) => {
|
|
|
|
|
- // 处理组件引用
|
|
|
|
|
- switch (route.component) {
|
|
|
|
|
- case 'Layout':
|
|
|
|
|
- route.component = () => import('@/layouts/index.vue')
|
|
|
|
|
- break
|
|
|
|
|
- default:
|
|
|
|
|
- if (route.component) {
|
|
|
|
|
- route.component = views[`../../views${route.component}.vue`]
|
|
|
|
|
- if(route.keepAlive==='1') route.cache=true
|
|
|
|
|
- if (route.menu ==0) {
|
|
|
|
|
- route.menu = false;
|
|
|
|
|
|
|
+ function formatBackRoutes(
|
|
|
|
|
+ routes: any,
|
|
|
|
|
+ views = import.meta.glob("../../views/**/*.vue")
|
|
|
|
|
+ ): Route.recordMainRaw[] {
|
|
|
|
|
+ // 需要从外层移动到meta对象的字段列表
|
|
|
|
|
+ const metaFields = [
|
|
|
|
|
+ "title",
|
|
|
|
|
+ "icon",
|
|
|
|
|
+ "auth",
|
|
|
|
|
+ "menu",
|
|
|
|
|
+ "breadcrumb",
|
|
|
|
|
+ "activeMenu",
|
|
|
|
|
+ "cache",
|
|
|
|
|
+ "noCache",
|
|
|
|
|
+ "link",
|
|
|
|
|
+ ]; // 从外层移动到meta对象的字段列表
|
|
|
|
|
+ return routes
|
|
|
|
|
+ .filter((route: any) => route.type != 2)
|
|
|
|
|
+ .map((route: any) => {
|
|
|
|
|
+ // 处理组件引用
|
|
|
|
|
+ switch (route.component) {
|
|
|
|
|
+ case "Layout":
|
|
|
|
|
+ route.component = () => import("@/layouts/index.vue");
|
|
|
|
|
+ break;
|
|
|
|
|
+ default:
|
|
|
|
|
+ if (route.component) {
|
|
|
|
|
+ route.component = views[`../../views${route.component}.vue`];
|
|
|
|
|
+ if (route.keepAlive === "1") route.cache = true;
|
|
|
|
|
+ if (route.menu == 0) {
|
|
|
|
|
+ route.menu = false;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ delete route.component;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 处理元数据格式转换:将指定字段从外层移动到meta对象中
|
|
|
|
|
+ const hasMetaFields = metaFields.some(
|
|
|
|
|
+ (field) => route[field] !== undefined
|
|
|
|
|
+ );
|
|
|
|
|
+ if (hasMetaFields) {
|
|
|
|
|
+ // 确保meta对象存在
|
|
|
|
|
+ if (!route.meta) {
|
|
|
|
|
+ route.meta = {};
|
|
|
|
|
+ }
|
|
|
|
|
+ // 批量移动字段
|
|
|
|
|
+ metaFields.forEach((field) => {
|
|
|
|
|
+ if (route[field] !== undefined) {
|
|
|
|
|
+ route.meta[field] = route[field];
|
|
|
|
|
+ delete route[field];
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- delete route.component
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- // 处理元数据格式转换:将指定字段从外层移动到meta对象中
|
|
|
|
|
- const hasMetaFields = metaFields.some(field => route[field] !== undefined)
|
|
|
|
|
- if (hasMetaFields) {
|
|
|
|
|
- // 确保meta对象存在
|
|
|
|
|
- if (!route.meta) {
|
|
|
|
|
- route.meta = {}
|
|
|
|
|
- }
|
|
|
|
|
- // 批量移动字段
|
|
|
|
|
- metaFields.forEach(field => {
|
|
|
|
|
- if (route[field] !== undefined) {
|
|
|
|
|
- route.meta[field] = route[field]
|
|
|
|
|
- delete route[field]
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- // 递归处理子路由
|
|
|
|
|
- if (route.children) {
|
|
|
|
|
- route.children = formatBackRoutes(route.children, views)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 递归处理子路由
|
|
|
|
|
+ if (route.children) {
|
|
|
|
|
+ route.children = formatBackRoutes(route.children, views);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return route
|
|
|
|
|
- })
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ return route;
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
// 生成路由(后端获取)
|
|
// 生成路由(后端获取)
|
|
|
async function generateRoutesAtBack() {
|
|
async function generateRoutesAtBack() {
|
|
|
- await api.menuApi.getRoleMenuTree().then((res) => {
|
|
|
|
|
- if(res.data.length > 0) {
|
|
|
|
|
- // 设置 routes 数据
|
|
|
|
|
- routesRaw.value = converDeprecatedAttribute(formatBackRoutes(res.data) as any)
|
|
|
|
|
- isGenerate.value = true
|
|
|
|
|
- }else{
|
|
|
|
|
- // 退出登录
|
|
|
|
|
- useUserStore().logout()
|
|
|
|
|
- // 路由获取失败
|
|
|
|
|
- ElMessage.error('路由获取失败')
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- }).catch(() => {})
|
|
|
|
|
|
|
+ await api.menuApi
|
|
|
|
|
+ .getRoleMenuTree()
|
|
|
|
|
+ .then((res) => {
|
|
|
|
|
+ if (res.data.length > 0) {
|
|
|
|
|
+ // 设置 routes 数据
|
|
|
|
|
+ routesRaw.value = converDeprecatedAttribute(
|
|
|
|
|
+ formatBackRoutes(res.data) as any
|
|
|
|
|
+ );
|
|
|
|
|
+ isGenerate.value = true;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 退出登录
|
|
|
|
|
+ useUserStore().logout();
|
|
|
|
|
+ // 路由获取失败
|
|
|
|
|
+ ElMessage.error("路由获取失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch(() => {});
|
|
|
}
|
|
}
|
|
|
// 生成路由(文件系统生成)
|
|
// 生成路由(文件系统生成)
|
|
|
function generateRoutesAtFilesystem(asyncRoutes: RouteRecordRaw[]) {
|
|
function generateRoutesAtFilesystem(asyncRoutes: RouteRecordRaw[]) {
|
|
|
// 设置 routes 数据
|
|
// 设置 routes 数据
|
|
|
- filesystemRoutesRaw.value = cloneDeep(asyncRoutes) as any
|
|
|
|
|
- isGenerate.value = true
|
|
|
|
|
|
|
+ filesystemRoutesRaw.value = cloneDeep(asyncRoutes) as any;
|
|
|
|
|
+ isGenerate.value = true;
|
|
|
}
|
|
}
|
|
|
// 记录 accessRoutes 路由,用于登出时删除路由
|
|
// 记录 accessRoutes 路由,用于登出时删除路由
|
|
|
function setCurrentRemoveRoutes(routes: (() => void)[]) {
|
|
function setCurrentRemoveRoutes(routes: (() => void)[]) {
|
|
|
- currentRemoveRoutes.value = routes
|
|
|
|
|
|
|
+ currentRemoveRoutes.value = routes;
|
|
|
}
|
|
}
|
|
|
// 清空动态路由
|
|
// 清空动态路由
|
|
|
function removeRoutes() {
|
|
function removeRoutes() {
|
|
|
- isGenerate.value = false
|
|
|
|
|
- routesRaw.value = []
|
|
|
|
|
- filesystemRoutesRaw.value = []
|
|
|
|
|
|
|
+ isGenerate.value = false;
|
|
|
|
|
+ routesRaw.value = [];
|
|
|
|
|
+ filesystemRoutesRaw.value = [];
|
|
|
currentRemoveRoutes.value.forEach((removeRoute) => {
|
|
currentRemoveRoutes.value.forEach((removeRoute) => {
|
|
|
- removeRoute()
|
|
|
|
|
- })
|
|
|
|
|
- currentRemoveRoutes.value = []
|
|
|
|
|
|
|
+ removeRoute();
|
|
|
|
|
+ });
|
|
|
|
|
+ currentRemoveRoutes.value = [];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
return {
|
|
@@ -264,8 +304,8 @@ function formatBackRoutes(routes: any, views = import.meta.glob('../../views/**/
|
|
|
generateRoutesAtFilesystem,
|
|
generateRoutesAtFilesystem,
|
|
|
setCurrentRemoveRoutes,
|
|
setCurrentRemoveRoutes,
|
|
|
removeRoutes,
|
|
removeRoutes,
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
-)
|
|
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+);
|
|
|
|
|
|
|
|
-export default useRouteStore
|
|
|
|
|
|
|
+export default useRouteStore;
|