routerUtil.js 4.24 KB
Newer Older
水落(YangLei)'s avatar
水落(YangLei) committed
1 2
import { mergeI18nFromRoutes } from '@/utils/i18nUtil';
import deepMerge from 'deepmerge';
wb-ct393452's avatar
wb-ct393452 committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

//应用配置
let appOptions = {
    router: undefined,
    i18n: undefined,
    store: undefined,
};

/**
 * 设置应用配置
 * @param options
 */
function setAppOptions(options) {
    const { router, store, i18n } = options;
    appOptions.router = router;
    appOptions.store = store;
    appOptions.i18n = i18n;
}

/**
 * 根据 路由配置 和 路由组件注册 解析路由
 * @param routesConfig 路由配置
 * @param routerMap 本地路由组件注册配置
 */
function parseRoutes(routesConfig, routerMap) {
    let routes = [];
水落(YangLei)'s avatar
水落(YangLei) committed
29
    routesConfig.forEach(item => {
wb-ct393452's avatar
wb-ct393452 committed
30 31 32
        // 获取注册在 routerMap 中的 router,初始化 routeCfg
        let router = undefined,
            routeCfg = {};
水落(YangLei)'s avatar
水落(YangLei) committed
33
        if (typeof item === 'string') {
wb-ct393452's avatar
wb-ct393452 committed
34 35
            router = routerMap[item];
            routeCfg = { path: (router && router.path) || item, router: item };
水落(YangLei)'s avatar
水落(YangLei) committed
36
        } else if (typeof item === 'object') {
wb-ct393452's avatar
wb-ct393452 committed
37 38 39 40 41
            router = routerMap[item.router];
            routeCfg = item;
        }
        if (!router) {
            console.warn(`can't find register for router ${routeCfg.router}, please register it in advance.`);
水落(YangLei)'s avatar
水落(YangLei) committed
42
            router = typeof item === 'string' ? { path: item, name: item } : item;
wb-ct393452's avatar
wb-ct393452 committed
43 44 45 46 47 48 49 50 51
        }
        // 从 router 和 routeCfg 解析路由
        const route = {
            path: routeCfg.path || router.path || routeCfg.router,
            name: routeCfg.name || router.name,
            component: router.component,
            redirect: routeCfg.redirect || router.redirect,
            meta: {
                authority:
水落(YangLei)'s avatar
水落(YangLei) committed
52 53 54 55 56
                    routeCfg.authority ||
                    router.authority ||
                    routeCfg.meta?.authority ||
                    router.meta?.authority ||
                    '*',
wb-ct393452's avatar
wb-ct393452 committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
                icon: routeCfg.icon || router.icon || routeCfg.meta?.icon || router.meta?.icon,
                page: routeCfg.page || router.page || routeCfg.meta?.page || router.meta?.page,
                link: routeCfg.link || router.link || routeCfg.meta?.link || router.meta?.link,
            },
        };
        if (routeCfg.invisible || router.invisible) {
            route.meta.invisible = true;
        }
        if (routeCfg.children && routeCfg.children.length > 0) {
            route.children = parseRoutes(routeCfg.children, routerMap);
        }
        routes.push(route);
    });
    return routes;
}

/**
 * 深度合并路由
 * @param target {Route[]}
 * @param source {Route[]}
 * @returns {Route[]}
 */
function deepMergeRoutes(target, source) {
    // 映射路由数组
水落(YangLei)'s avatar
水落(YangLei) committed
81
    const mapRoutes = routes => {
wb-ct393452's avatar
wb-ct393452 committed
82
        const routesMap = {};
水落(YangLei)'s avatar
水落(YangLei) committed
83
        routes.forEach(item => {
wb-ct393452's avatar
wb-ct393452 committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97
            routesMap[item.path] = {
                ...item,
                children: item.children ? mapRoutes(item.children) : undefined,
            };
        });
        return routesMap;
    };
    const tarMap = mapRoutes(target);
    const srcMap = mapRoutes(source);

    // 合并路由
    const merge = deepMerge(tarMap, srcMap);

    // 转换为 routes 数组
水落(YangLei)'s avatar
水落(YangLei) committed
98 99
    const parseRoutesMap = routesMap => {
        return Object.values(routesMap).map(item => {
wb-ct393452's avatar
wb-ct393452 committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            if (item.children) {
                item.children = parseRoutesMap(item.children);
            } else {
                delete item.children;
            }
            return item;
        });
    };
    return parseRoutesMap(merge);
}

/**
 * 从路由 path 解析 i18n key
 * @param path
 * @returns {*}
 */
function getI18nKey(path) {
水落(YangLei)'s avatar
水落(YangLei) committed
117 118 119
    const keys = path.split('/').filter(item => !item.startsWith(':') && item != '');
    keys.push('name');
    return keys.join('.');
wb-ct393452's avatar
wb-ct393452 committed
120 121 122 123 124 125 126 127 128 129
}

/**
 * 加载导航守卫
 * @param guards
 * @param options
 */
function loadGuards(guards, options) {
    const { beforeEach, afterEach } = guards;
    const { router } = options;
水落(YangLei)'s avatar
水落(YangLei) committed
130 131
    beforeEach.forEach(guard => {
        if (guard && typeof guard === 'function') {
wb-ct393452's avatar
wb-ct393452 committed
132 133 134
            router.beforeEach((to, from, next) => guard(to, from, next, options));
        }
    });
水落(YangLei)'s avatar
水落(YangLei) committed
135 136
    afterEach.forEach(guard => {
        if (guard && typeof guard === 'function') {
wb-ct393452's avatar
wb-ct393452 committed
137 138 139 140 141
            router.afterEach((to, from) => guard(to, from, options));
        }
    });
}

水落(YangLei)'s avatar
水落(YangLei) committed
142
export { parseRoutes, getI18nKey, loadGuards, deepMergeRoutes, setAppOptions };