From 25d6340245f5afed5d83860e228f85a40d6525cb Mon Sep 17 00:00:00 2001 From: zzhuan <1260310633@qq.com> Date: Thu, 23 Apr 2026 17:11:05 +0800 Subject: [PATCH] loginRedirect.ts --- src/utils/loginRedirect.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/utils/loginRedirect.ts diff --git a/src/utils/loginRedirect.ts b/src/utils/loginRedirect.ts new file mode 100644 index 0000000..4c43ff1 --- /dev/null +++ b/src/utils/loginRedirect.ts @@ -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()); +}