2019-03-27 19:12:12 +08:00
|
|
|
|
/* eslint-disable */
|
|
|
|
|
|
|
|
|
|
// localStorage 操作
|
|
|
|
|
|
|
|
|
|
const cacheKeys = {
|
2019-04-22 19:11:23 +08:00
|
|
|
|
ACCESS_TOKEN: 'accessToken',
|
|
|
|
|
REFRESH_TOKEN: 'refreshToken',
|
2019-03-27 19:12:12 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
|
/// 设置 loginToken,分为 accessToken 和 refreshToken
|
|
|
|
|
|
2019-04-22 19:11:23 +08:00
|
|
|
|
export function checkLogin() {
|
|
|
|
|
let accessToken = getAccessToken();
|
|
|
|
|
return accessToken && accessToken.length > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 19:12:12 +08:00
|
|
|
|
export function setLoginToken(accessToken, refreshToken) {
|
2019-04-22 19:11:23 +08:00
|
|
|
|
setLocalStorage(cacheKeys.ACCESS_TOKEN, accessToken);
|
|
|
|
|
setLocalStorage(cacheKeys.REFRESH_TOKEN, refreshToken);
|
2019-03-27 19:12:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getLoginToken() {
|
|
|
|
|
const res = {};
|
2019-04-22 19:11:23 +08:00
|
|
|
|
res[cacheKeys.ACCESS_TOKEN] = getLocalStorage(cacheKeys.ACCESS_TOKEN);
|
|
|
|
|
res[cacheKeys.REFRESH_TOKEN] = getLocalStorage(cacheKeys.REFRESH_TOKEN);
|
2019-03-27 19:12:12 +08:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-28 19:19:04 +08:00
|
|
|
|
export function clearLoginToken() {
|
2019-04-22 19:11:23 +08:00
|
|
|
|
removeLocalStorage(cacheKeys.ACCESS_TOKEN);
|
|
|
|
|
removeLocalStorage(cacheKeys.REFRESH_TOKEN);
|
2019-03-28 19:19:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 19:12:12 +08:00
|
|
|
|
export function getAccessToken() {
|
2019-04-22 19:11:23 +08:00
|
|
|
|
return getLocalStorage(cacheKeys.ACCESS_TOKEN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getRefreshToken() {
|
|
|
|
|
return getLocalStorage(cacheKeys.REFRESH_TOKEN);
|
2019-03-27 19:12:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
|
/// 设置 localStorage 公共方法
|
|
|
|
|
|
|
|
|
|
function setLocalStorage(key, value) {
|
|
|
|
|
try {
|
|
|
|
|
localStorage.setItem(key, value);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw new Error(`localStorage 设置错误! ${e}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getLocalStorage(key) {
|
|
|
|
|
try {
|
|
|
|
|
return localStorage.getItem(key);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw new Error(`localStorage 获取错误! ${e}`);
|
|
|
|
|
}
|
2019-03-28 19:19:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function removeLocalStorage(key) {
|
|
|
|
|
try {
|
|
|
|
|
localStorage.removeItem(key);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw new Error(`localStorage 设置错误! ${e}`);
|
|
|
|
|
}
|
2019-04-22 19:11:23 +08:00
|
|
|
|
}
|