Files
cunkebao_v3/Store_vue/api/utils/auth.js
2025-04-15 17:08:52 +08:00

48 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* token验证与跳转工具函数
*/
// 检查token是否存在
export const hasToken = () => {
return !!uni.getStorageSync('token');
};
// 检查token是否过期
export const isTokenExpired = () => {
const expiredTime = uni.getStorageSync('token_expired');
if (!expiredTime) return true;
// 将当前时间转换为秒级时间戳确保与expiredTime单位一致
const currentTimeInSeconds = Math.floor(Date.now() / 1000);
return currentTimeInSeconds >= expiredTime;
};
// 检查是否有有效token
export const hasValidToken = () => {
return hasToken() && !isTokenExpired();
};
// 清除token信息
export const clearToken = () => {
uni.removeStorageSync('token');
uni.removeStorageSync('member');
uni.removeStorageSync('token_expired');
};
// 跳转到登录页面
export const redirectToLogin = () => {
const currentPage = getCurrentPages().pop();
if (currentPage && currentPage.route !== 'pages/login/index') {
uni.reLaunch({
url: '/pages/login/index'
});
}
};
// 跳转到聊天页面
export const redirectToChat = () => {
uni.reLaunch({
url: '/pages/chat/index'
});
};