wangfukang 2 weeks ago
parent
commit
d14e5f0153
  1. 22
      utils/authRedirect.js

22
utils/authRedirect.js

@ -1,5 +1,6 @@
export function hasLogin() { 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() { export function getCurrentPagePath() {
@ -15,13 +16,20 @@ export function getCurrentPagePath() {
} }
export function requireLoginToCurrentPage() { export function requireLoginToCurrentPage() {
return requireLoginToPage(getCurrentPagePath(), { redirect: true })
}
export function requireLoginToPage(targetPath, options = {}) {
if (hasLogin()) return true if (hasLogin()) return true
const redirectKey = 'login_redirect_' + Date.now() const redirectKey = 'login_redirect_' + Date.now()
const targetPath = getCurrentPagePath() const targetUrl = targetPath || getCurrentPagePath()
uni.setStorageSync(redirectKey, targetPath) uni.setStorageSync(redirectKey, targetUrl)
uni.setStorageSync('authPendingTargetPath', targetPath) uni.setStorageSync('authPendingTargetPath', targetUrl)
uni.redirectTo({ const loginUrl = '/package2/login/login?redirect=' + redirectKey
url: '/package2/login/login?redirect=' + redirectKey if (options.redirect) {
}) uni.redirectTo({ url: loginUrl })
} else {
uni.navigateTo({ url: loginUrl })
}
return false return false
} }

Loading…
Cancel
Save