import {treeFind, slicePath} from '@ywfe/utils'; import { match } from 'path-to-regexp'; import { Route, YLocation } from '../router.interface'; import {matchParams} from '../utils'; export const getRoute = (pathname: string, routes: Route, moduleData: any): YLocation => { const res = treeFind({key: 'url', value: pathname}, 'children', routes); let result; if(!res.length) { const matchFn = match('/:moduleName/:moduleId/:menu?'); const matchRes = matchFn(pathname); const parentPath = slicePath(pathname); if(matchRes) { result = routes[parentPath]; } } else { result = res[0] } if(result) { const parentPath = result.grandPath ? result.grandPath : result.parentPath; const parent = routes[parentPath]; result.parentTitle = parent?.title; } if(!result){ const params = matchParams(pathname, '/:moduleName/:moduleId/:menu?'); result = { path: params.menu ? params.menu.replace(/_/g, '/') : pathname, url: pathname, ...params, parentTitle: moduleData ? moduleData.moduleName : '遥望云' } } return result; }