ludu-cloud/mobile-web/src/utils/cache.js
YunaiV 366f7791fb 前端:增加注册登陆
前端:个人信息修改
2019-03-28 19:19:04 +08:00

59 lines
1.3 KiB
JavaScript
Raw 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.

/* 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;
}
export function clearLoginToken() {
removeLocalStorage(cacheKeys.accessTokenKey);
removeLocalStorage(cacheKeys.refreshTokenKey);
}
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}`);
}
}
function removeLocalStorage(key) {
try {
localStorage.removeItem(key);
} catch (e) {
throw new Error(`localStorage 设置错误! ${e}`);
}
}