import { clearAuthSession, getAccessToken } from '@/utils/auth-storage' export const LOGIN_PAGE = '/pages/Login/user-login/index' let redirectingToLogin = false export function isLoggedIn() { return Boolean(getAccessToken()) } /** 未登录时跳转登录页,返回是否已登录 */ export function ensureLoggedIn(showToast = true) { if (isLoggedIn()) return true if (showToast) uni.showToast({ title: '请先登录', icon: 'none' }) redirectToLogin() return false } /** 登录态失效(如接口返回 401)时清除会话并跳转登录页 */ export function handleUnauthorized(message = '登录已过期,请重新登录') { if (redirectingToLogin) return redirectingToLogin = true clearAuthSession() uni.showToast({ title: message, icon: 'none' }) redirectToLogin() } function redirectToLogin() { uni.reLaunch({ url: LOGIN_PAGE, complete: () => { redirectingToLogin = false }, }) }