2021-10-21 11:59:55 +08:00
|
|
|
import type { Component } from 'vue';
|
2021-11-28 12:17:48 +08:00
|
|
|
import type { Router, RouteRecordRaw, RouteMeta } from 'vue-router';
|
2021-11-29 20:34:56 +08:00
|
|
|
import type { ImportedRouteModules, LoginModuleType } from '@/interface';
|
2021-11-28 12:17:48 +08:00
|
|
|
|
|
|
|
interface SingleRouteConfig {
|
|
|
|
/** 路由 */
|
|
|
|
route: RouteRecordRaw;
|
|
|
|
/** 路由容器 */
|
|
|
|
container: Component;
|
|
|
|
/** 路由容器的描述 */
|
|
|
|
containerMeta: RouteMeta;
|
|
|
|
/** 404路由的名称 */
|
|
|
|
notFoundName: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 设置单个路由 */
|
|
|
|
export function setSingleRoute(config: SingleRouteConfig) {
|
|
|
|
const { route, container, containerMeta, notFoundName } = config;
|
|
|
|
const routeItem: RouteRecordRaw = {
|
|
|
|
name: `${route.name as string}_`,
|
|
|
|
path: `${route.path}_`,
|
|
|
|
component: container,
|
|
|
|
redirect: { name: notFoundName },
|
|
|
|
meta: {
|
|
|
|
notAsMenu: true,
|
|
|
|
...containerMeta,
|
|
|
|
title: `${containerMeta.title}-container`
|
|
|
|
},
|
|
|
|
children: [route]
|
|
|
|
};
|
|
|
|
|
|
|
|
return routeItem;
|
|
|
|
}
|
2021-10-21 11:59:55 +08:00
|
|
|
|
2021-11-21 18:44:18 +08:00
|
|
|
/** 处理导入的路由模块 */
|
|
|
|
export function transformRouteModules(routeModules: ImportedRouteModules) {
|
|
|
|
const modules = Object.keys(routeModules).map(key => {
|
|
|
|
return routeModules[key].default;
|
|
|
|
});
|
2021-11-28 12:17:48 +08:00
|
|
|
const constantRoutes: RouteRecordRaw[] = modules.sort(
|
|
|
|
(next, pre) => Number(next.meta?.order) - Number(pre.meta?.order)
|
|
|
|
);
|
2021-11-21 18:44:18 +08:00
|
|
|
return constantRoutes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取路由的首页
|
|
|
|
* @param routes - 路由
|
|
|
|
* @param routeHomeName - 路由首页名称
|
|
|
|
*/
|
2021-11-28 12:17:48 +08:00
|
|
|
export function getRouteHome(routes: RouteRecordRaw[], routeHomeName: string) {
|
|
|
|
let routeHome: RouteRecordRaw;
|
|
|
|
function hasChildren(route: RouteRecordRaw) {
|
2021-11-21 18:44:18 +08:00
|
|
|
return Boolean(route.children && route.children.length);
|
|
|
|
}
|
2021-11-28 12:17:48 +08:00
|
|
|
function getRouteHomeByRoute(route: RouteRecordRaw) {
|
2021-11-21 18:44:18 +08:00
|
|
|
if (routeHome) return;
|
|
|
|
const hasChild = hasChildren(route);
|
|
|
|
if (!hasChild) {
|
|
|
|
if (route.name === routeHomeName) {
|
|
|
|
routeHome = route;
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-28 12:17:48 +08:00
|
|
|
getRouteHomeByRoutes(route.children as RouteRecordRaw[]);
|
2021-11-21 18:44:18 +08:00
|
|
|
}
|
2021-10-21 11:59:55 +08:00
|
|
|
}
|
2021-11-28 12:17:48 +08:00
|
|
|
function getRouteHomeByRoutes(_routes: RouteRecordRaw[]) {
|
2021-11-21 18:44:18 +08:00
|
|
|
_routes.some(item => {
|
2021-11-28 12:17:48 +08:00
|
|
|
getRouteHomeByRoute(item as RouteRecordRaw);
|
2021-11-21 18:44:18 +08:00
|
|
|
return routeHome !== undefined;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
getRouteHomeByRoutes(routes);
|
|
|
|
return routeHome!;
|
2021-10-21 11:59:55 +08:00
|
|
|
}
|
|
|
|
|
2021-11-28 12:17:48 +08:00
|
|
|
/**
|
|
|
|
* 将多层级路由转换成二级路由
|
|
|
|
* @param routes - 路由
|
|
|
|
*/
|
|
|
|
export function transformMultiDegreeRoutes(routes: RouteRecordRaw[]) {
|
|
|
|
function hasComponent(route: RouteRecordRaw) {
|
|
|
|
return Boolean(route.component);
|
|
|
|
}
|
|
|
|
function hasChildren(route: RouteRecordRaw) {
|
|
|
|
return Boolean(route.children && route.children.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
function upDimension(route: RouteRecordRaw): RouteRecordRaw[] {
|
|
|
|
if (hasChildren(route)) {
|
|
|
|
const updateRoute = { ...route };
|
|
|
|
if (!hasComponent(route)) {
|
|
|
|
return updateRoute.children!;
|
|
|
|
}
|
|
|
|
updateRoute.children = updateRoute.children?.map(item => upDimension(item)).flat();
|
|
|
|
return [updateRoute];
|
|
|
|
}
|
|
|
|
return [route];
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes.map(item => upDimension(item)).flat();
|
|
|
|
}
|
|
|
|
|
2021-10-21 11:59:55 +08:00
|
|
|
/** 获取登录后的重定向地址 */
|
2021-11-21 18:44:18 +08:00
|
|
|
export function getLoginRedirectUrl(router: Router) {
|
2021-11-19 01:33:36 +08:00
|
|
|
const path = router.currentRoute.value.fullPath as string;
|
|
|
|
const redirectUrl = path === '/' ? undefined : path;
|
2021-10-21 11:59:55 +08:00
|
|
|
return redirectUrl;
|
|
|
|
}
|
2021-11-29 20:34:56 +08:00
|
|
|
|
|
|
|
/** 获取登录模块的正则字符串 */
|
|
|
|
export function getLoginModuleRegExp() {
|
|
|
|
const modules: LoginModuleType[] = ['pwd-login', 'code-login', 'register', 'reset-pwd', 'bind-wechat'];
|
|
|
|
return modules.join('|');
|
|
|
|
}
|