Files
dukang/packages/weixin-sdk/src/app-path.ts
T
2026-08-04 21:38:49 +08:00

26 lines
978 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// <reference types="vite/client" />
function normalizeBase(base?: string): string {
if (!base || base === '/') return '';
return base.replace(/\/$/, '');
}
/** React Router basename(来自 Vite `base` */
export function getRouterBasename(baseUrl?: string): string | undefined {
const base = normalizeBase(baseUrl ?? import.meta.env?.BASE_URL);
return base || undefined;
}
/** 拼接带 Vite base 前缀的应用内路径(用于 window.location 跳转) */
export function toAppPath(path: string, baseUrl?: string): string {
const base = normalizeBase(baseUrl ?? import.meta.env?.BASE_URL);
const normalized = path.startsWith('/') ? path : `/${path}`;
return `${base}${normalized}`;
}
export function isOnAppPath(path: string, baseUrl?: string): boolean {
if (typeof window === 'undefined') return false;
const target = toAppPath(path, baseUrl);
return window.location.pathname === target || window.location.pathname.startsWith(`${target}/`);
}