const TOKEN_KEY = 'token' const USER_KEY = 'userInfo' const LOGIN_TIME_KEY = 'login_time' // 后端 30 天过期,前端保险起见提前 30 分钟判定失效 const EXPIRE = 30 * 24 * 60 * 60 - 30 * 60 export function getToken() { const loginTime = uni.getStorageSync(LOGIN_TIME_KEY) const curr = parseInt(Date.now() / 1000) if (loginTime && curr - loginTime > EXPIRE) { clearAuth() return '' } return uni.getStorageSync(TOKEN_KEY) || '' } export function setToken(token) { uni.setStorageSync(TOKEN_KEY, token) uni.setStorageSync(LOGIN_TIME_KEY, parseInt(Date.now() / 1000)) } export function getUser() { return uni.getStorageSync(USER_KEY) || {} } export function setUser(user) { uni.setStorageSync(USER_KEY, user || {}) } export function clearAuth() { uni.removeStorageSync(TOKEN_KEY) uni.removeStorageSync(USER_KEY) uni.removeStorageSync(LOGIN_TIME_KEY) }