57 lines
1.3 KiB
JavaScript
57 lines
1.3 KiB
JavaScript
// utils/auth.js - 登录状态管理(不依赖store模块)
|
|
|
|
// 获取登录状态
|
|
const isLoggedIn = () => {
|
|
return wx.getStorageSync('isLoggedIn') || false;
|
|
};
|
|
|
|
// 获取用户信息
|
|
const getUserInfo = () => {
|
|
return wx.getStorageSync('userInfo') || null;
|
|
};
|
|
|
|
// 获取token
|
|
const getToken = () => {
|
|
return wx.getStorageSync('token') || null;
|
|
};
|
|
|
|
// 获取userId
|
|
const getUserId = () => {
|
|
return wx.getStorageSync('userId') || null;
|
|
};
|
|
|
|
// 是否为活动管理员(可创建/编辑/删除活动)
|
|
const getIsAdmin = () => {
|
|
return wx.getStorageSync('isAdmin') || false;
|
|
};
|
|
|
|
// 保存登录状态
|
|
const saveLoginState = (token, userId, userInfo) => {
|
|
wx.setStorageSync('token', token);
|
|
wx.setStorageSync('userId', userId);
|
|
wx.setStorageSync('isLoggedIn', true);
|
|
wx.setStorageSync('isAdmin', !!(userInfo && userInfo.isAdmin));
|
|
if (userInfo) {
|
|
wx.setStorageSync('userInfo', userInfo);
|
|
}
|
|
};
|
|
|
|
// 清除登录状态
|
|
const clearLoginState = () => {
|
|
wx.removeStorageSync('token');
|
|
wx.removeStorageSync('userId');
|
|
wx.removeStorageSync('isLoggedIn');
|
|
wx.removeStorageSync('isAdmin');
|
|
wx.removeStorageSync('userInfo');
|
|
};
|
|
|
|
module.exports = {
|
|
isLoggedIn,
|
|
getUserInfo,
|
|
getToken,
|
|
getUserId,
|
|
getIsAdmin,
|
|
saveLoginState,
|
|
clearLoginState,
|
|
};
|