You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.3 KiB

3 months ago
export function hasLogin() {
2 weeks ago
const token = uni.getStorageSync('hiver_token')
return !!(token && typeof token === 'string' && token.trim() && token !== 'null' && token !== 'undefined')
3 months ago
}
export function getCurrentPagePath() {
const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : []
const page = pages[pages.length - 1]
if (!page || !page.route) return '/pages/index/index'
const options = page.options || {}
const query = Object.keys(options)
.filter(key => options[key] !== undefined && options[key] !== null && options[key] !== '')
.map(key => key + '=' + encodeURIComponent(String(options[key])))
.join('&')
return '/' + page.route + (query ? '?' + query : '')
}
export function requireLoginToCurrentPage() {
2 weeks ago
return requireLoginToPage(getCurrentPagePath(), { redirect: true })
}
export function requireLoginToPage(targetPath, options = {}) {
3 months ago
if (hasLogin()) return true
const redirectKey = 'login_redirect_' + Date.now()
2 weeks ago
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 })
}
3 months ago
return false
}