57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
export type PathSet = 'asset' | 'energy';
|
|
|
|
interface BrowserLocation {
|
|
pathname: string;
|
|
search: string;
|
|
hash: string;
|
|
}
|
|
const ROOT_PATHS = new Set(['/asset', '/energy', '/ele/import', '/admin/feedback']);
|
|
|
|
const LEGACY_PATHS: Record<string, { path: string; hash?: string }> = {
|
|
'/': { path: '/asset' },
|
|
'/vehicle': { path: '/asset', hash: 'assets' },
|
|
'/assets': { path: '/asset', hash: 'assets' },
|
|
'/mileage': { path: '/asset', hash: 'mileage' },
|
|
'/scheduling': { path: '/asset', hash: 'scheduling' },
|
|
};
|
|
|
|
/** 返回旧地址应替换成的规范地址;主路径返回 null。 */
|
|
export function resolveCanonicalUrl(location: BrowserLocation): string | null {
|
|
if (ROOT_PATHS.has(location.pathname)) return null;
|
|
|
|
const target = LEGACY_PATHS[location.pathname] ?? { path: '/asset' };
|
|
const finalHash = target.hash ? `#${target.hash}` : location.hash;
|
|
return `${target.path}${location.search}${finalHash}`;
|
|
}
|
|
|
|
export function normalizeBrowserPath(): void {
|
|
if (typeof window === 'undefined') return;
|
|
const target = resolveCanonicalUrl(window.location);
|
|
if (target) window.history.replaceState(null, '', target);
|
|
}
|
|
|
|
export function getPathSet(pathname: string): PathSet {
|
|
return pathname === '/energy' ? 'energy' : 'asset';
|
|
}
|
|
|
|
export function getRouteKey(pathname: string, hash: string): string {
|
|
if (pathname === '/ele/import' || hash === '#/ele/import' || hash === '#ele/import') {
|
|
return 'ele/import';
|
|
}
|
|
if (
|
|
pathname === '/admin/feedback'
|
|
|| hash === '#/admin/feedback'
|
|
|| hash === '#admin/feedback'
|
|
) {
|
|
return 'admin/feedback';
|
|
}
|
|
return '';
|
|
}
|
|
|
|
export function readBrowserRoute(): { routeKey: string; pathSet: PathSet } {
|
|
return {
|
|
routeKey: getRouteKey(window.location.pathname, window.location.hash),
|
|
pathSet: getPathSet(window.location.pathname),
|
|
};
|
|
}
|