56 lines
1.3 KiB
JavaScript
56 lines
1.3 KiB
JavaScript
import { getCache, getLocalStorage, setLocalStorage, removeCache, removeLocalStorage } from './cacheService';
|
|
import { getUserInfo } from '@/common/server/user';
|
|
import { navigateTo, navigateToAccountLogin } from './router';
|
|
export const isAccountLogin = () => {
|
|
const token = getCache('token');
|
|
if (token) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 获取用户信息
|
|
* @returns {Promise} 用户信息
|
|
*/
|
|
export const getAccountInfo = async () => {
|
|
return new Promise(async (resolve, reject) => {
|
|
const user = getLocalStorage('user');
|
|
if (user) {
|
|
resolve(user);
|
|
return;
|
|
}
|
|
if (isAccountLogin()) {
|
|
const user_res = await getUserInfo();
|
|
setLocalStorage("user", user_res, 60);
|
|
resolve(user_res);
|
|
} else {
|
|
resolve(null);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 清除用户信息
|
|
*/
|
|
export const clearAccountInfo = async () => {
|
|
removeLocalStorage('user');
|
|
}
|
|
|
|
/**
|
|
* 判断用户是否登录,如果用户未登录,会跳转到登录页面
|
|
* @returns {Boolean} 是否登录
|
|
*/
|
|
export const IsUserLogin = () => {
|
|
if (!isAccountLogin()) {
|
|
navigateToAccountLogin();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
export const logout = () => {
|
|
removeCache('token');
|
|
removeCache('userInfo');
|
|
removeLocalStorage('user');
|
|
} |