loginRedirect.ts

This commit is contained in:
2026-04-23 17:11:05 +08:00
parent adf124ee70
commit 25d6340245

View File

@@ -0,0 +1,30 @@
import { PageEnum } from '@/enums/pageEnum';
function getAppBaseUrl() {
const baseUrl = import.meta.env.BASE_URL || '/';
return baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;
}
function getCurrentRouteFullPath() {
const hashPath = window.location.hash.replace(/^#/, '');
if (hashPath) return hashPath;
const { pathname, search } = window.location;
const appBase = getAppBaseUrl().replace(/\/$/, '');
const routePath = appBase && pathname.startsWith(appBase) ? pathname.slice(appBase.length) || '/' : pathname;
return `${routePath}${search}`;
}
export function getLoginHref(redirect = getCurrentRouteFullPath()) {
const appBase = getAppBaseUrl();
const redirectQuery =
redirect && !redirect.startsWith(PageEnum.BASE_LOGIN)
? `?redirect=${encodeURIComponent(redirect)}`
: '';
return `${appBase}#${PageEnum.BASE_LOGIN}${redirectQuery}`;
}
export function replaceToLogin() {
window.location.replace(getLoginHref());
}