diff --git a/utils/authRedirect.js b/utils/authRedirect.js index 6b98883..8f3e785 100644 --- a/utils/authRedirect.js +++ b/utils/authRedirect.js @@ -1,5 +1,6 @@ export function hasLogin() { - return !!uni.getStorageSync('id') + const token = uni.getStorageSync('hiver_token') + return !!(token && typeof token === 'string' && token.trim() && token !== 'null' && token !== 'undefined') } export function getCurrentPagePath() { @@ -15,13 +16,20 @@ export function getCurrentPagePath() { } export function requireLoginToCurrentPage() { + return requireLoginToPage(getCurrentPagePath(), { redirect: true }) +} + +export function requireLoginToPage(targetPath, options = {}) { if (hasLogin()) return true const redirectKey = 'login_redirect_' + Date.now() - const targetPath = getCurrentPagePath() - uni.setStorageSync(redirectKey, targetPath) - uni.setStorageSync('authPendingTargetPath', targetPath) - uni.redirectTo({ - url: '/package2/login/login?redirect=' + redirectKey - }) + const targetUrl = targetPath || getCurrentPagePath() + uni.setStorageSync(redirectKey, targetUrl) + uni.setStorageSync('authPendingTargetPath', targetUrl) + const loginUrl = '/package2/login/login?redirect=' + redirectKey + if (options.redirect) { + uni.redirectTo({ url: loginUrl }) + } else { + uni.navigateTo({ url: loginUrl }) + } return false }