export function hasLogin() { const token = uni.getStorageSync('hiver_token') return !!(token && typeof token === 'string' && token.trim() && token !== 'null' && token !== 'undefined') } 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() { return requireLoginToPage(getCurrentPagePath(), { redirect: true }) } export function requireLoginToPage(targetPath, options = {}) { if (hasLogin()) return true const redirectKey = 'login_redirect_' + Date.now() 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 }