import { pathIsMatch, pathToPageName } from 'urlUtils'; let authTypes; let routes = []; let vueRoutes = []; export interface Option { routes authTypes? } export const init = (option:Option) => { authTypes = option.authTypes; } // == 加载路由配置 export const load = (routesGroup) => { for (let routeModule of routesGroup) { if(Array.isArray(routeModule)){ for (let route of routeModule) { const path = route.url; const name = pathToPageName(path); const component = route.page; routes.push(route); vueRoutes.push({path, name, component}); } } } } // == 根据url获取路由配置 export const get = (url, type?) => { if(typeof url === 'object') return {}; if(routes.length === 0) { console.warn('路由配置不存在,请先初始化!'); return {}; } const isVueType = type === 'vue'; const route = (isVueType ? vueRoutes : routes).find((route) => { const pattern = isVueType ? route.path : route.url; return pathIsMatch(url, pattern); }); return route; } /** * 获取全部配置(routes、vueRoutes) * @param type vue */ export const getAll = (type?) => { return type === 'vue' ? vueRoutes : routes; } // == 判断是否是无授权页面 export const isNone = (path) => { const route = get(path); const auth = authTypes[route.auth] || -1; return auth == authTypes.none; } // == 判断是否是公共页面 export const isPub = (path) => { const route = get(path); const auth = authTypes[route.auth]; return auth == authTypes.pub; }