2019-03-27 19:12:12 +08:00
|
|
|
|
/* eslint-disable */
|
|
|
|
|
|
|
|
|
|
// localStorage 操作
|
|
|
|
|
|
|
|
|
|
const cacheKeys = {
|
|
|
|
|
accessTokenKey: 'accessToken',
|
|
|
|
|
refreshTokenKey: 'refreshToken',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
|
/// 设置 loginToken,分为 accessToken 和 refreshToken
|
|
|
|
|
|
|
|
|
|
export function setLoginToken(accessToken, refreshToken) {
|
|
|
|
|
setLocalStorage(cacheKeys.accessTokenKey, accessToken);
|
|
|
|
|
setLocalStorage(cacheKeys.refreshTokenKey, refreshToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getLoginToken() {
|
|
|
|
|
const res = {};
|
|
|
|
|
res[cacheKeys.accessTokenKey] = getLocalStorage(cacheKeys.accessTokenKey);
|
|
|
|
|
res[cacheKeys.refreshTokenKey] = getLocalStorage(cacheKeys.refreshTokenKey);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-28 19:19:04 +08:00
|
|
|
|
export function clearLoginToken() {
|
|
|
|
|
removeLocalStorage(cacheKeys.accessTokenKey);
|
|
|
|
|
removeLocalStorage(cacheKeys.refreshTokenKey);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 19:12:12 +08:00
|
|
|
|
export function getAccessToken() {
|
|
|
|
|
return getLocalStorage(cacheKeys.accessTokenKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
|
/// 设置 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-03-27 19:12:12 +08:00
|
|
|
|
}
|