2022-05-01 11:43:53 +08:00
|
|
|
|
import { RouteRecordRaw } from 'vue-router';
|
|
|
|
|
import { storeToRefs } from 'pinia';
|
|
|
|
|
import pinia from '/@/stores/index';
|
2022-04-18 19:14:38 +08:00
|
|
|
|
import { useUserInfo } from '/@/stores/userInfo';
|
|
|
|
|
import { useRequestOldRoutes } from '/@/stores/requestOldRoutes';
|
2021-06-19 17:49:42 +08:00
|
|
|
|
import { Session } from '/@/utils/storage';
|
|
|
|
|
import { NextLoading } from '/@/utils/loading';
|
2022-05-01 11:43:53 +08:00
|
|
|
|
import { dynamicRoutes, notFoundAndNoPower } from '/@/router/route';
|
|
|
|
|
import { formatTwoStageRoutes, formatFlatteningRoutes, router } from '/@/router/index';
|
|
|
|
|
import { useRoutesList } from '/@/stores/routesList';
|
|
|
|
|
import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
|
2022-02-21 23:52:59 +08:00
|
|
|
|
import { useMenuApi } from '/@/api/menu/index';
|
|
|
|
|
|
2022-05-01 11:43:53 +08:00
|
|
|
|
// 后端控制路由
|
|
|
|
|
|
2022-11-29 22:03:40 +08:00
|
|
|
|
// 引入 api 请求接口
|
|
|
|
|
const menuApi = useMenuApi();
|
|
|
|
|
|
2021-06-19 17:49:42 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取目录下的 .vue、.tsx 全部文件
|
|
|
|
|
* @method import.meta.glob
|
|
|
|
|
* @link 参考:https://cn.vitejs.dev/guide/features.html#json
|
|
|
|
|
*/
|
2022-11-29 22:03:40 +08:00
|
|
|
|
const layouModules: any = import.meta.glob('../layout/routerView/*.{vue,tsx}');
|
|
|
|
|
const viewsModules: any = import.meta.glob('../views/**/*.{vue,tsx}');
|
2021-06-22 22:03:59 +08:00
|
|
|
|
const dynamicViewsModules: Record<string, Function> = Object.assign({}, { ...layouModules }, { ...viewsModules });
|
2021-06-19 17:49:42 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 后端控制路由:初始化方法,防止刷新时路由丢失
|
2022-04-18 19:14:38 +08:00
|
|
|
|
* @method NextLoading 界面 loading 动画开始执行
|
|
|
|
|
* @method useUserInfo().setUserInfos() 触发初始化用户信息 pinia
|
2022-05-01 11:43:53 +08:00
|
|
|
|
* @method useRequestOldRoutes().setRequestOldRoutes() 存储接口原始路由(未处理component),根据需求选择使用
|
2021-06-19 17:49:42 +08:00
|
|
|
|
* @method setAddRoute 添加动态路由
|
2022-11-29 22:03:40 +08:00
|
|
|
|
* @method setFilterMenuAndCacheTagsViewRoutes 设置路由到 pinia routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
|
2021-06-19 17:49:42 +08:00
|
|
|
|
*/
|
|
|
|
|
export async function initBackEndControlRoutes() {
|
|
|
|
|
// 界面 loading 动画开始执行
|
|
|
|
|
if (window.nextLoading === undefined) NextLoading.start();
|
|
|
|
|
// 无 token 停止执行下一步
|
|
|
|
|
if (!Session.get('token')) return false;
|
2022-04-18 19:14:38 +08:00
|
|
|
|
// 触发初始化用户信息 pinia
|
2022-07-10 19:37:39 +08:00
|
|
|
|
// https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP
|
|
|
|
|
await useUserInfo().setUserInfos();
|
2021-06-19 17:49:42 +08:00
|
|
|
|
// 获取路由菜单数据
|
|
|
|
|
const res = await getBackEndControlRoutes();
|
2022-12-09 23:55:16 +08:00
|
|
|
|
// 无登录权限时,添加判断
|
|
|
|
|
// https://gitee.com/lyt-top/vue-next-admin/issues/I64HVO
|
|
|
|
|
if (res.data.length <= 0) return Promise.resolve(true);
|
2021-06-19 17:49:42 +08:00
|
|
|
|
// 存储接口原始路由(未处理component),根据需求选择使用
|
2022-05-01 11:43:53 +08:00
|
|
|
|
useRequestOldRoutes().setRequestOldRoutes(JSON.parse(JSON.stringify(res.data)));
|
2021-06-19 17:49:42 +08:00
|
|
|
|
// 处理路由(component),替换 dynamicRoutes(/@/router/route)第一个顶级 children 的路由
|
|
|
|
|
dynamicRoutes[0].children = await backEndComponent(res.data);
|
|
|
|
|
// 添加动态路由
|
|
|
|
|
await setAddRoute();
|
2022-11-29 22:03:40 +08:00
|
|
|
|
// 设置路由到 pinia routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
|
2022-05-01 11:43:53 +08:00
|
|
|
|
await setFilterMenuAndCacheTagsViewRoutes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-11-29 22:03:40 +08:00
|
|
|
|
* 设置路由到 pinia routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
|
2022-05-01 11:43:53 +08:00
|
|
|
|
* @description 用于左侧菜单、横向菜单的显示
|
|
|
|
|
* @description 用于 tagsView、菜单搜索中:未过滤隐藏的(isHide)
|
|
|
|
|
*/
|
2022-12-09 23:55:16 +08:00
|
|
|
|
export async function setFilterMenuAndCacheTagsViewRoutes() {
|
2022-05-01 11:43:53 +08:00
|
|
|
|
const storesRoutesList = useRoutesList(pinia);
|
|
|
|
|
storesRoutesList.setRoutesList(dynamicRoutes[0].children as any);
|
|
|
|
|
setCacheTagsViewRoutes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 缓存多级嵌套数组处理后的一维数组
|
|
|
|
|
* @description 用于 tagsView、菜单搜索中:未过滤隐藏的(isHide)
|
|
|
|
|
*/
|
|
|
|
|
export function setCacheTagsViewRoutes() {
|
|
|
|
|
const storesTagsView = useTagsViewRoutes(pinia);
|
|
|
|
|
storesTagsView.setTagsViewRoutes(formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes))[0].children);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-05-05 20:00:52 +08:00
|
|
|
|
* 处理路由格式及添加捕获所有路由或 404 Not found 路由
|
2022-05-01 11:43:53 +08:00
|
|
|
|
* @description 替换 dynamicRoutes(/@/router/route)第一个顶级 children 的路由
|
|
|
|
|
* @returns 返回替换后的路由数组
|
|
|
|
|
*/
|
|
|
|
|
export function setFilterRouteEnd() {
|
|
|
|
|
let filterRouteEnd: any = formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes));
|
2022-11-16 15:34:23 +08:00
|
|
|
|
// notFoundAndNoPower 防止 404、401 不在 layout 布局中,不设置的话,404、401 界面将全屏显示
|
|
|
|
|
// 关联问题 No match found for location with path 'xxx'
|
2022-05-01 11:43:53 +08:00
|
|
|
|
filterRouteEnd[0].children = [...filterRouteEnd[0].children, ...notFoundAndNoPower];
|
|
|
|
|
return filterRouteEnd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加动态路由
|
|
|
|
|
* @method router.addRoute
|
|
|
|
|
* @description 此处循环为 dynamicRoutes(/@/router/route)第一个顶级 children 的路由一维数组,非多级嵌套
|
|
|
|
|
* @link 参考:https://next.router.vuejs.org/zh/api/#addroute
|
|
|
|
|
*/
|
|
|
|
|
export async function setAddRoute() {
|
|
|
|
|
await setFilterRouteEnd().forEach((route: RouteRecordRaw) => {
|
|
|
|
|
router.addRoute(route);
|
|
|
|
|
});
|
2021-06-19 17:49:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 请求后端路由菜单接口
|
|
|
|
|
* @description isRequestRoutes 为 true,则开启后端控制路由
|
|
|
|
|
* @returns 返回后端路由菜单数据
|
|
|
|
|
*/
|
|
|
|
|
export function getBackEndControlRoutes() {
|
|
|
|
|
// 模拟 admin 与 test
|
2022-05-01 11:43:53 +08:00
|
|
|
|
const stores = useUserInfo(pinia);
|
|
|
|
|
const { userInfos } = storeToRefs(stores);
|
|
|
|
|
const auth = userInfos.value.roles[0];
|
2021-06-19 17:49:42 +08:00
|
|
|
|
// 管理员 admin
|
2022-11-29 22:03:40 +08:00
|
|
|
|
if (auth === 'admin') return menuApi.getAdminMenu();
|
2021-06-19 17:49:42 +08:00
|
|
|
|
// 其它用户 test
|
2022-11-29 22:03:40 +08:00
|
|
|
|
else return menuApi.getTestMenu();
|
2021-06-19 17:49:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重新请求后端路由菜单接口
|
|
|
|
|
* @description 用于菜单管理界面刷新菜单(未进行测试)
|
|
|
|
|
* @description 路径:/src/views/system/menu/component/addMenu.vue
|
|
|
|
|
*/
|
2022-12-09 23:55:16 +08:00
|
|
|
|
export async function setBackEndControlRefreshRoutes() {
|
|
|
|
|
await getBackEndControlRoutes();
|
2021-06-19 17:49:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 后端路由 component 转换
|
|
|
|
|
* @param routes 后端返回的路由表数组
|
|
|
|
|
* @returns 返回处理成函数后的 component
|
|
|
|
|
*/
|
|
|
|
|
export function backEndComponent(routes: any) {
|
|
|
|
|
if (!routes) return;
|
|
|
|
|
return routes.map((item: any) => {
|
|
|
|
|
if (item.component) item.component = dynamicImport(dynamicViewsModules, item.component as string);
|
|
|
|
|
item.children && backEndComponent(item.children);
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 后端路由 component 转换函数
|
|
|
|
|
* @param dynamicViewsModules 获取目录下的 .vue、.tsx 全部文件
|
|
|
|
|
* @param component 当前要处理项 component
|
|
|
|
|
* @returns 返回处理成函数后的 component
|
|
|
|
|
*/
|
|
|
|
|
export function dynamicImport(dynamicViewsModules: Record<string, Function>, component: string) {
|
|
|
|
|
const keys = Object.keys(dynamicViewsModules);
|
|
|
|
|
const matchKeys = keys.filter((key) => {
|
2021-06-22 22:03:59 +08:00
|
|
|
|
const k = key.replace(/..\/views|../, '');
|
2021-06-19 17:49:42 +08:00
|
|
|
|
return k.startsWith(`${component}`) || k.startsWith(`/${component}`);
|
|
|
|
|
});
|
|
|
|
|
if (matchKeys?.length === 1) {
|
|
|
|
|
const matchKey = matchKeys[0];
|
|
|
|
|
return dynamicViewsModules[matchKey];
|
|
|
|
|
}
|
|
|
|
|
if (matchKeys?.length > 1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|