83 lines
2.7 KiB
Plaintext
83 lines
2.7 KiB
Plaintext
export type AuthRedirectOptions = {
|
||
loginPath?: string
|
||
registerPath?: string
|
||
fallbackUrl?: string
|
||
}
|
||
|
||
function normalizePath(path: string): string {
|
||
if (!path) return ''
|
||
return path.startsWith('/') ? path : `/${path}`
|
||
}
|
||
|
||
function safeEncode(url: string): string {
|
||
try {
|
||
return encodeURIComponent(url)
|
||
} catch (_) {
|
||
return url
|
||
}
|
||
}
|
||
|
||
export function getCurrentPageUrlWithQuery(): string {
|
||
const pages = getCurrentPages() as any[]
|
||
const currentPage = pages.length > 0 ? pages[pages.length - 1] : null
|
||
if (!currentPage) return ''
|
||
|
||
const route = currentPage.route as string | null
|
||
const options = (currentPage.options ?? {}) as Record<string, any>
|
||
if (!route) return ''
|
||
|
||
const path = normalizePath(route)
|
||
const keys = Object.keys(options)
|
||
if (keys.length === 0) return path
|
||
|
||
const query = keys
|
||
.filter((k) => options[k] != null)
|
||
.map((k) => `${k}=${encodeURIComponent(String(options[k]))}`)
|
||
.join('&')
|
||
return query.length > 0 ? `${path}?${query}` : path
|
||
}
|
||
|
||
export function toLoginWithRedirect(redirectUrl?: string, opts?: AuthRedirectOptions) {
|
||
const loginPath = opts?.loginPath ?? '/pages/user/login'
|
||
const registerPath = opts?.registerPath ?? '/pages/user/register'
|
||
const fallback = opts?.fallbackUrl ?? '/pages/mall/consumer/index'
|
||
|
||
let target = (redirectUrl != null && redirectUrl.length > 0)
|
||
? redirectUrl
|
||
: (getCurrentPageUrlWithQuery() || fallback)
|
||
|
||
// 避免 redirect 无限嵌套:如果目标本身就是 login/register,则回落到 fallback
|
||
const normalizedLogin = normalizePath(loginPath)
|
||
const normalizedRegister = normalizePath(registerPath)
|
||
const normalizedTarget = normalizePath(target.split('?')[0] ?? '')
|
||
if (normalizedTarget === normalizedLogin || normalizedTarget === normalizedRegister) {
|
||
target = fallback
|
||
}
|
||
|
||
uni.navigateTo({
|
||
url: `${loginPath}?redirect=${safeEncode(target)}`
|
||
})
|
||
}
|
||
|
||
export function toRegisterWithRedirect(redirectUrl?: string, opts?: AuthRedirectOptions) {
|
||
const loginPath = opts?.loginPath ?? '/pages/user/login'
|
||
const registerPath = opts?.registerPath ?? '/pages/user/register'
|
||
const fallback = opts?.fallbackUrl ?? '/pages/mall/consumer/index'
|
||
|
||
let target = (redirectUrl != null && redirectUrl.length > 0)
|
||
? redirectUrl
|
||
: (getCurrentPageUrlWithQuery() || fallback)
|
||
|
||
// 避免 redirect 无限嵌套:如果目标本身就是 login/register,则回落到 fallback
|
||
const normalizedLogin = normalizePath(loginPath)
|
||
const normalizedRegister = normalizePath(registerPath)
|
||
const normalizedTarget = normalizePath(target.split('?')[0] ?? '')
|
||
if (normalizedTarget === normalizedLogin || normalizedTarget === normalizedRegister) {
|
||
target = fallback
|
||
}
|
||
|
||
uni.navigateTo({
|
||
url: `${registerPath}?redirect=${safeEncode(target)}`
|
||
})
|
||
}
|