mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-11-11 03:03:42 +08:00
refactor(projects): new storage system [新的本地数据存储系统]
This commit is contained in:
@@ -1 +0,0 @@
|
||||
export * from './user';
|
||||
@@ -1,60 +0,0 @@
|
||||
import { EnumStorageKey } from '@/enum';
|
||||
import { getLocal, removeLocal, setLocal } from '../storage';
|
||||
|
||||
/** 设置token */
|
||||
export function setToken(token: string) {
|
||||
setLocal(EnumStorageKey.token, token);
|
||||
}
|
||||
|
||||
/** 获取token */
|
||||
export function getToken() {
|
||||
return getLocal<string>(EnumStorageKey.token) || '';
|
||||
}
|
||||
|
||||
/** 去除token */
|
||||
export function removeToken() {
|
||||
removeLocal(EnumStorageKey.token);
|
||||
}
|
||||
|
||||
/** 获取refresh token */
|
||||
export function getRefreshToken() {
|
||||
return getLocal<string>(EnumStorageKey['refresh-token']) || '';
|
||||
}
|
||||
|
||||
/** 设置refresh token */
|
||||
export function setRefreshToken(token: string) {
|
||||
setLocal(EnumStorageKey['refresh-token'], token);
|
||||
}
|
||||
|
||||
/** 去除refresh token */
|
||||
export function removeRefreshToken() {
|
||||
removeLocal(EnumStorageKey['refresh-token']);
|
||||
}
|
||||
|
||||
/** 获取用户信息 */
|
||||
export function getUserInfo() {
|
||||
const emptyInfo: Auth.UserInfo = {
|
||||
userId: '',
|
||||
userName: '',
|
||||
userRole: 'user'
|
||||
};
|
||||
const userInfo: Auth.UserInfo = getLocal<Auth.UserInfo>(EnumStorageKey['user-info']) || emptyInfo;
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
/** 设置用户信息 */
|
||||
export function setUserInfo(userInfo: Auth.UserInfo) {
|
||||
setLocal(EnumStorageKey['user-info'], userInfo);
|
||||
}
|
||||
|
||||
/** 去除用户信息 */
|
||||
export function removeUserInfo() {
|
||||
removeLocal(EnumStorageKey['user-info']);
|
||||
}
|
||||
|
||||
/** 去除用户相关缓存 */
|
||||
export function clearAuthStorage() {
|
||||
removeToken();
|
||||
removeRefreshToken();
|
||||
removeUserInfo();
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
export * from './typeof';
|
||||
export * from './color';
|
||||
export * from './number';
|
||||
export * from './object';
|
||||
export * from './pattern';
|
||||
export * from './theme';
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
/** 设置对象数据 */
|
||||
export function objectAssign<T extends Record<string, any>>(target: T, source: Partial<T>) {
|
||||
Object.assign(target, source);
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { EnumStorageKey } from '@/enum';
|
||||
|
||||
/**
|
||||
* 缓存主题颜色
|
||||
* @param color
|
||||
*/
|
||||
export function setThemeColor(color: string) {
|
||||
window.localStorage.setItem(EnumStorageKey['theme-color'], color);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存的主题颜色
|
||||
*/
|
||||
export function getThemeColor() {
|
||||
return window.localStorage.getItem(EnumStorageKey['theme-color']);
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
export * from './common';
|
||||
export * from './storage';
|
||||
export * from './service';
|
||||
export * from './auth';
|
||||
export * from './router';
|
||||
export * from './form';
|
||||
|
||||
@@ -1,45 +1,57 @@
|
||||
import { decrypto, encrypto } from '../crypto';
|
||||
|
||||
interface StorageData {
|
||||
value: unknown;
|
||||
interface StorageData<T> {
|
||||
value: T;
|
||||
expire: number | null;
|
||||
}
|
||||
|
||||
/** 默认缓存期限为7天 */
|
||||
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;
|
||||
function createLocalStorage<T extends StorageInterface.Local = StorageInterface.Local>() {
|
||||
/** 默认缓存期限为7天 */
|
||||
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;
|
||||
|
||||
export function setLocal(key: string, value: unknown, expire: number | null = DEFAULT_CACHE_TIME) {
|
||||
const storageData: StorageData = { value, expire: expire !== null ? new Date().getTime() + expire * 1000 : null };
|
||||
const json = encrypto(storageData);
|
||||
window.localStorage.setItem(key, json);
|
||||
}
|
||||
function set<K extends keyof T>(key: K, value: T[K], expire: number | null = DEFAULT_CACHE_TIME) {
|
||||
const storageData: StorageData<T[K]> = {
|
||||
value,
|
||||
expire: expire !== null ? new Date().getTime() + expire * 1000 : null
|
||||
};
|
||||
const json = encrypto(storageData);
|
||||
window.localStorage.setItem(key as string, json);
|
||||
}
|
||||
|
||||
export function getLocal<T>(key: string) {
|
||||
const json = window.localStorage.getItem(key);
|
||||
if (json) {
|
||||
let storageData: StorageData | null = null;
|
||||
try {
|
||||
storageData = decrypto(json);
|
||||
} catch {
|
||||
// 防止解析失败
|
||||
}
|
||||
if (storageData) {
|
||||
const { value, expire } = storageData;
|
||||
// 在有效期内直接返回
|
||||
if (expire === null || expire >= Date.now()) {
|
||||
return value as T;
|
||||
function get<K extends keyof T>(key: K) {
|
||||
const json = window.localStorage.getItem(key as string);
|
||||
if (json) {
|
||||
let storageData: StorageData<T[K]> | null = null;
|
||||
try {
|
||||
storageData = decrypto(json);
|
||||
} catch {
|
||||
// 防止解析失败
|
||||
}
|
||||
if (storageData) {
|
||||
const { value, expire } = storageData;
|
||||
// 在有效期内直接返回
|
||||
if (expire === null || expire >= Date.now()) {
|
||||
return value as T[K];
|
||||
}
|
||||
}
|
||||
remove(key);
|
||||
return null;
|
||||
}
|
||||
removeLocal(key);
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
|
||||
function remove(key: keyof T) {
|
||||
window.localStorage.removeItem(key as string);
|
||||
}
|
||||
function clear() {
|
||||
window.localStorage.clear();
|
||||
}
|
||||
|
||||
return {
|
||||
set,
|
||||
get,
|
||||
remove,
|
||||
clear
|
||||
};
|
||||
}
|
||||
|
||||
export function removeLocal(key: string) {
|
||||
window.localStorage.removeItem(key);
|
||||
}
|
||||
|
||||
export function clearLocal() {
|
||||
window.localStorage.clear();
|
||||
}
|
||||
export const localStg = createLocalStorage();
|
||||
|
||||
@@ -25,3 +25,37 @@ export function removeSession(key: string) {
|
||||
export function clearSession() {
|
||||
window.sessionStorage.clear();
|
||||
}
|
||||
|
||||
function createSessionStorage<T extends StorageInterface.Session = StorageInterface.Session>() {
|
||||
function set<K extends keyof T>(key: K, value: T[K]) {
|
||||
const json = encrypto(value);
|
||||
sessionStorage.setItem(key as string, json);
|
||||
}
|
||||
function get<K extends keyof T>(key: K) {
|
||||
const json = sessionStorage.getItem(key as string);
|
||||
let data: T[K] | null = null;
|
||||
if (json) {
|
||||
try {
|
||||
data = decrypto(json);
|
||||
} catch {
|
||||
// 防止解析失败
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
function remove(key: keyof T) {
|
||||
window.sessionStorage.removeItem(key as string);
|
||||
}
|
||||
function clear() {
|
||||
window.sessionStorage.clear();
|
||||
}
|
||||
|
||||
return {
|
||||
set,
|
||||
get,
|
||||
remove,
|
||||
clear
|
||||
};
|
||||
}
|
||||
|
||||
export const sessionStg = createSessionStorage();
|
||||
|
||||
Reference in New Issue
Block a user