mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-01-24 10:33:50 +08:00
feat: add permissionCacheType setting
This commit is contained in:
parent
6e03e05032
commit
26b6109ca0
@ -18,3 +18,8 @@ export const BASE_LOCAL_CACHE_KEY = 'LOCAL__CACHE__KEY__';
|
|||||||
|
|
||||||
// base global session key
|
// base global session key
|
||||||
export const BASE_SESSION_CACHE_KEY = 'SESSION__CACHE__KEY__';
|
export const BASE_SESSION_CACHE_KEY = 'SESSION__CACHE__KEY__';
|
||||||
|
|
||||||
|
export enum CacheTypeEnum {
|
||||||
|
SESSION,
|
||||||
|
LOCAL,
|
||||||
|
}
|
||||||
|
@ -23,7 +23,7 @@ export function useTimeoutFn(handle: Fn<any>, wait: number) {
|
|||||||
export function useTimeoutRef(wait: number) {
|
export function useTimeoutRef(wait: number) {
|
||||||
const readyRef = ref(false);
|
const readyRef = ref(false);
|
||||||
|
|
||||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
let timer: TimeoutHandle;
|
||||||
function stop(): void {
|
function stop(): void {
|
||||||
readyRef.value = false;
|
readyRef.value = false;
|
||||||
timer && window.clearTimeout(timer);
|
timer && window.clearTimeout(timer);
|
||||||
|
@ -7,7 +7,7 @@ import { appStore } from '/@/store/modules/app';
|
|||||||
export function useTransitionSetting() {
|
export function useTransitionSetting() {
|
||||||
const getTransitionSetting = computed(() => appStore.getProjectConfig.transitionSetting);
|
const getTransitionSetting = computed(() => appStore.getProjectConfig.transitionSetting);
|
||||||
|
|
||||||
const getEnableTransition = computed(() => unref(getTransitionSetting).enable);
|
const getEnableTransition = computed(() => unref(getTransitionSetting)?.enable);
|
||||||
|
|
||||||
const getOpenNProgress = computed(() => unref(getTransitionSetting)?.openNProgress);
|
const getOpenNProgress = computed(() => unref(getTransitionSetting)?.openNProgress);
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import { appStore } from '/@/store/modules/app';
|
|||||||
import { userStore } from '/@/store/modules/user';
|
import { userStore } from '/@/store/modules/user';
|
||||||
|
|
||||||
export function useLockPage() {
|
export function useLockPage() {
|
||||||
let timeId: ReturnType<typeof setTimeout>;
|
let timeId: TimeoutHandle;
|
||||||
|
|
||||||
function clear(): void {
|
function clear(): void {
|
||||||
window.clearTimeout(timeId);
|
window.clearTimeout(timeId);
|
||||||
|
@ -40,12 +40,9 @@
|
|||||||
height: 12px;
|
height: 12px;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
color: inherit;
|
color: inherit;
|
||||||
visibility: hidden;
|
|
||||||
transition: none;
|
transition: none;
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
visibility: visible;
|
|
||||||
|
|
||||||
svg {
|
svg {
|
||||||
width: 0.8em;
|
width: 0.8em;
|
||||||
}
|
}
|
||||||
@ -72,10 +69,6 @@
|
|||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.ant-tabs-close-x {
|
|
||||||
visibility: visible;
|
|
||||||
}
|
|
||||||
|
|
||||||
svg {
|
svg {
|
||||||
width: 0.7em;
|
width: 0.7em;
|
||||||
fill: @white;
|
fill: @white;
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
|
import './index.less';
|
||||||
|
|
||||||
import type { TabContentProps } from './tab.data';
|
import type { TabContentProps } from './tab.data';
|
||||||
import type { TabItem } from '/@/store/modules/tab';
|
import type { TabItem } from '/@/store/modules/tab';
|
||||||
import type { AppRouteRecordRaw } from '/@/router/types';
|
import type { AppRouteRecordRaw } from '/@/router/types';
|
||||||
|
|
||||||
import { defineComponent, watch, computed, unref, toRaw } from 'vue';
|
import { defineComponent, watch, computed, unref } from 'vue';
|
||||||
import { useRouter } from 'vue-router';
|
import { useRouter } from 'vue-router';
|
||||||
import router from '/@/router';
|
|
||||||
|
|
||||||
import { Tabs } from 'ant-design-vue';
|
import { Tabs } from 'ant-design-vue';
|
||||||
import TabContent from './TabContent';
|
import TabContent from './TabContent';
|
||||||
@ -18,12 +19,12 @@ import { userStore } from '/@/store/modules/user';
|
|||||||
|
|
||||||
import { closeTab } from './useTabDropdown';
|
import { closeTab } from './useTabDropdown';
|
||||||
import { useTabs } from '/@/hooks/web/useTabs';
|
import { useTabs } from '/@/hooks/web/useTabs';
|
||||||
|
import { initAffixTabs } from './useAffixTabs';
|
||||||
|
|
||||||
import './index.less';
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
name: 'MultiTabs',
|
name: 'MultipleTabs',
|
||||||
setup() {
|
setup() {
|
||||||
let isAddAffix = false;
|
initAffixTabs();
|
||||||
|
|
||||||
const go = useGo();
|
const go = useGo();
|
||||||
|
|
||||||
@ -36,11 +37,6 @@ export default defineComponent({
|
|||||||
watch(
|
watch(
|
||||||
() => tabStore.getLastChangeRouteState,
|
() => tabStore.getLastChangeRouteState,
|
||||||
() => {
|
() => {
|
||||||
if (!isAddAffix) {
|
|
||||||
addAffixTabs();
|
|
||||||
isAddAffix = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const lastChangeRoute = unref(tabStore.getLastChangeRouteState);
|
const lastChangeRoute = unref(tabStore.getLastChangeRouteState);
|
||||||
|
|
||||||
if (!lastChangeRoute || !userStore.getTokenState) return;
|
if (!lastChangeRoute || !userStore.getTokenState) return;
|
||||||
@ -56,39 +52,15 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
|
||||||
* @description: 过滤所有固定路由
|
|
||||||
*/
|
|
||||||
function filterAffixTabs(routes: AppRouteRecordRaw[]) {
|
|
||||||
const tabs: TabItem[] = [];
|
|
||||||
routes &&
|
|
||||||
routes.forEach((route) => {
|
|
||||||
if (route.meta && route.meta.affix) {
|
|
||||||
tabs.push(toRaw(route) as TabItem);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return tabs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @description: 设置固定tabs
|
|
||||||
*/
|
|
||||||
function addAffixTabs(): void {
|
|
||||||
const affixTabs = filterAffixTabs((router.getRoutes() as unknown) as AppRouteRecordRaw[]);
|
|
||||||
for (const tab of affixTabs) {
|
|
||||||
tabStore.commitAddTab(tab);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// tab切换
|
// tab切换
|
||||||
function handleChange(activeKey: any) {
|
function handleChange(activeKey: any) {
|
||||||
activeKeyRef.value = activeKey;
|
activeKeyRef.value = activeKey;
|
||||||
go(activeKey, false);
|
go(activeKey, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 关闭当前ab
|
// 关闭当前tab
|
||||||
function handleEdit(targetKey: string) {
|
function handleEdit(targetKey: string) {
|
||||||
// 新增操作隐藏,目前只使用删除操作
|
// Added operation to hide, currently only use delete operation
|
||||||
const index = unref(getTabsState).findIndex(
|
const index = unref(getTabsState).findIndex(
|
||||||
(item) => (item.fullPath || item.path) === targetKey
|
(item) => (item.fullPath || item.path) === targetKey
|
||||||
);
|
);
|
||||||
@ -107,12 +79,13 @@ export default defineComponent({
|
|||||||
</span>
|
</span>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderTabs() {
|
function renderTabs() {
|
||||||
return unref(getTabsState).map((item: TabItem) => {
|
return unref(getTabsState).map((item: TabItem) => {
|
||||||
const key = item.query ? item.fullPath : item.path;
|
const key = item.query ? item.fullPath : item.path;
|
||||||
|
const closable = !(item && item.meta && item.meta.affix);
|
||||||
return (
|
return (
|
||||||
<Tabs.TabPane key={key} closable={!(item && item.meta && item.meta.affix)}>
|
<Tabs.TabPane key={key} closable={closable}>
|
||||||
{{
|
{{
|
||||||
tab: () => <TabContent tabItem={item} />,
|
tab: () => <TabContent tabItem={item} />,
|
||||||
}}
|
}}
|
||||||
|
35
src/layouts/default/multitabs/useAffixTabs.ts
Normal file
35
src/layouts/default/multitabs/useAffixTabs.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { toRaw } from 'vue';
|
||||||
|
import router from '/@/router';
|
||||||
|
import { AppRouteRecordRaw } from '/@/router/types';
|
||||||
|
import { TabItem, tabStore } from '/@/store/modules/tab';
|
||||||
|
|
||||||
|
export function initAffixTabs() {
|
||||||
|
/**
|
||||||
|
* @description: Filter all fixed routes
|
||||||
|
*/
|
||||||
|
function filterAffixTabs(routes: AppRouteRecordRaw[]) {
|
||||||
|
const tabs: TabItem[] = [];
|
||||||
|
routes &&
|
||||||
|
routes.forEach((route) => {
|
||||||
|
if (route.meta && route.meta.affix) {
|
||||||
|
tabs.push(toRaw(route) as TabItem);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return tabs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: Set fixed tabs
|
||||||
|
*/
|
||||||
|
function addAffixTabs(): void {
|
||||||
|
const affixTabs = filterAffixTabs((router.getRoutes() as unknown) as AppRouteRecordRaw[]);
|
||||||
|
for (const tab of affixTabs) {
|
||||||
|
tabStore.commitAddTab(tab);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let isAddAffix = false;
|
||||||
|
if (!isAddAffix) {
|
||||||
|
addAffixTabs();
|
||||||
|
isAddAffix = true;
|
||||||
|
}
|
||||||
|
}
|
@ -203,6 +203,7 @@ export function closeTab(closedTab: TabItem | AppRouteRecordRaw) {
|
|||||||
const getTabsState = computed(() => {
|
const getTabsState = computed(() => {
|
||||||
return tabStore.getTabsState;
|
return tabStore.getTabsState;
|
||||||
});
|
});
|
||||||
|
|
||||||
const { path } = unref(currentRoute);
|
const { path } = unref(currentRoute);
|
||||||
if (path !== closedTab.path) {
|
if (path !== closedTab.path) {
|
||||||
// 关闭的不是激活tab
|
// 关闭的不是激活tab
|
||||||
|
@ -14,9 +14,9 @@ import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel';
|
|||||||
|
|
||||||
import { tabStore } from '/@/store/modules/tab';
|
import { tabStore } from '/@/store/modules/tab';
|
||||||
|
|
||||||
|
const { closeMessageOnSwitch, removeAllHttpPending } = useProjectSetting();
|
||||||
const globSetting = useGlobSetting();
|
const globSetting = useGlobSetting();
|
||||||
export function createGuard(router: Router) {
|
export function createGuard(router: Router) {
|
||||||
const { closeMessageOnSwitch, removeAllHttpPending } = useProjectSetting();
|
|
||||||
let axiosCanceler: AxiosCanceler | null;
|
let axiosCanceler: AxiosCanceler | null;
|
||||||
if (removeAllHttpPending) {
|
if (removeAllHttpPending) {
|
||||||
axiosCanceler = new AxiosCanceler();
|
axiosCanceler = new AxiosCanceler();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import type { ProjectConfig } from '/@/types/config';
|
import type { ProjectConfig } from '/@/types/config';
|
||||||
|
|
||||||
import { MenuTypeEnum, MenuModeEnum, TriggerEnum } from '/@/enums/menuEnum';
|
import { MenuTypeEnum, MenuModeEnum, TriggerEnum } from '/@/enums/menuEnum';
|
||||||
|
import { CacheTypeEnum } from '/@/enums/cacheEnum';
|
||||||
import { ContentEnum, PermissionModeEnum, ThemeEnum, RouterTransitionEnum } from '/@/enums/appEnum';
|
import { ContentEnum, PermissionModeEnum, ThemeEnum, RouterTransitionEnum } from '/@/enums/appEnum';
|
||||||
import { primaryColor } from '../../build/config/lessModifyVars';
|
import { primaryColor } from '../../build/config/lessModifyVars';
|
||||||
import { isProdMode } from '/@/utils/env';
|
import { isProdMode } from '/@/utils/env';
|
||||||
@ -13,6 +14,9 @@ const setting: ProjectConfig = {
|
|||||||
// Permission mode
|
// Permission mode
|
||||||
permissionMode: PermissionModeEnum.ROLE,
|
permissionMode: PermissionModeEnum.ROLE,
|
||||||
|
|
||||||
|
// Permission-related cache is stored in sessionStorage or localStorage
|
||||||
|
permissionCacheType: CacheTypeEnum.LOCAL,
|
||||||
|
|
||||||
// color
|
// color
|
||||||
// TODO Theme color
|
// TODO Theme color
|
||||||
themeColor: primaryColor,
|
themeColor: primaryColor,
|
||||||
@ -130,7 +134,7 @@ const setting: ProjectConfig = {
|
|||||||
openPageLoading: true,
|
openPageLoading: true,
|
||||||
|
|
||||||
// Whether to open the top progress bar
|
// Whether to open the top progress bar
|
||||||
openNProgress: true,
|
openNProgress: false,
|
||||||
},
|
},
|
||||||
|
|
||||||
// Whether to enable KeepAlive cache is best to close during development, otherwise the cache needs to be cleared every time
|
// Whether to enable KeepAlive cache is best to close during development, otherwise the cache needs to be cleared every time
|
||||||
|
@ -20,6 +20,7 @@ import {
|
|||||||
} from '/@/setup/theme';
|
} from '/@/setup/theme';
|
||||||
|
|
||||||
import { appStore } from '/@/store/modules/app';
|
import { appStore } from '/@/store/modules/app';
|
||||||
|
import { deepMerge } from '../utils/index';
|
||||||
|
|
||||||
// Used to share global app instances
|
// Used to share global app instances
|
||||||
let app: App;
|
let app: App;
|
||||||
@ -50,16 +51,15 @@ export function useThemeMode(mode: ThemeModeEnum) {
|
|||||||
// Initial project configuration
|
// Initial project configuration
|
||||||
export function initAppConfigStore() {
|
export function initAppConfigStore() {
|
||||||
let projCfg: ProjectConfig = getLocal(PROJ_CFG_KEY) as ProjectConfig;
|
let projCfg: ProjectConfig = getLocal(PROJ_CFG_KEY) as ProjectConfig;
|
||||||
if (!projCfg) {
|
projCfg = deepMerge(projectSetting, projCfg || {});
|
||||||
projCfg = projectSetting;
|
|
||||||
}
|
try {
|
||||||
const {
|
const {
|
||||||
colorWeak,
|
colorWeak,
|
||||||
grayMode,
|
grayMode,
|
||||||
headerSetting: { bgColor: headerBgColor },
|
headerSetting: { bgColor: headerBgColor } = {},
|
||||||
menuSetting: { bgColor },
|
menuSetting: { bgColor } = {},
|
||||||
} = projCfg;
|
} = projCfg;
|
||||||
try {
|
|
||||||
// if (
|
// if (
|
||||||
// themeColor !== primaryColor &&
|
// themeColor !== primaryColor &&
|
||||||
// themeColor &&
|
// themeColor &&
|
||||||
|
@ -26,7 +26,7 @@ export interface LockInfo {
|
|||||||
isLock: boolean;
|
isLock: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
let timeId: ReturnType<typeof setTimeout>;
|
let timeId: TimeoutHandle;
|
||||||
const NAME = 'app';
|
const NAME = 'app';
|
||||||
hotModuleUnregisterModule(NAME);
|
hotModuleUnregisterModule(NAME);
|
||||||
@Module({ dynamic: true, namespaced: true, store, name: NAME })
|
@Module({ dynamic: true, namespaced: true, store, name: NAME })
|
||||||
|
@ -11,7 +11,7 @@ import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';
|
|||||||
|
|
||||||
import { PageEnum } from '/@/enums/pageEnum';
|
import { PageEnum } from '/@/enums/pageEnum';
|
||||||
import { RoleEnum } from '/@/enums/roleEnum';
|
import { RoleEnum } from '/@/enums/roleEnum';
|
||||||
import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
|
import { CacheTypeEnum, ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
|
||||||
|
|
||||||
import { useMessage } from '/@/hooks/web/useMessage';
|
import { useMessage } from '/@/hooks/web/useMessage';
|
||||||
|
|
||||||
@ -19,13 +19,29 @@ import router from '/@/router';
|
|||||||
|
|
||||||
import { loginApi, getUserInfoById } from '/@/api/sys/user';
|
import { loginApi, getUserInfoById } from '/@/api/sys/user';
|
||||||
|
|
||||||
import { setLocal, getLocal } from '/@/utils/helper/persistent';
|
import { setLocal, getLocal, getSession, setSession } from '/@/utils/helper/persistent';
|
||||||
// import { FULL_PAGE_NOT_FOUND_ROUTE } from '/@/router/constant';
|
import { useProjectSetting } from '/@/hooks/setting';
|
||||||
|
|
||||||
export type UserInfo = Omit<GetUserInfoByUserIdModel, 'roles'>;
|
export type UserInfo = Omit<GetUserInfoByUserIdModel, 'roles'>;
|
||||||
|
|
||||||
const NAME = 'user';
|
const NAME = 'user';
|
||||||
hotModuleUnregisterModule(NAME);
|
hotModuleUnregisterModule(NAME);
|
||||||
|
|
||||||
|
const { permissionCacheType } = useProjectSetting();
|
||||||
|
|
||||||
|
function getCache<T>(key: string) {
|
||||||
|
const fn = permissionCacheType === CacheTypeEnum.LOCAL ? getLocal : getSession;
|
||||||
|
return fn(key) as T;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setCache(USER_INFO_KEY: string, info: any) {
|
||||||
|
if (!info) return;
|
||||||
|
// const fn = permissionCacheType === CacheTypeEnum.LOCAL ? setLocal : setSession;
|
||||||
|
setLocal(USER_INFO_KEY, info, true);
|
||||||
|
// TODO
|
||||||
|
setSession(USER_INFO_KEY, info, true);
|
||||||
|
}
|
||||||
|
|
||||||
@Module({ namespaced: true, name: NAME, dynamic: true, store })
|
@Module({ namespaced: true, name: NAME, dynamic: true, store })
|
||||||
class User extends VuexModule {
|
class User extends VuexModule {
|
||||||
// user info
|
// user info
|
||||||
@ -38,15 +54,15 @@ class User extends VuexModule {
|
|||||||
private roleListState: RoleEnum[] = [];
|
private roleListState: RoleEnum[] = [];
|
||||||
|
|
||||||
get getUserInfoState(): UserInfo {
|
get getUserInfoState(): UserInfo {
|
||||||
return this.userInfoState || (getLocal(USER_INFO_KEY) as UserInfo) || {};
|
return this.userInfoState || getCache<UserInfo>(USER_INFO_KEY) || {};
|
||||||
}
|
}
|
||||||
|
|
||||||
get getTokenState(): string {
|
get getTokenState(): string {
|
||||||
return this.tokenState || (getLocal(TOKEN_KEY) as string);
|
return this.tokenState || getCache<string>(TOKEN_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
get getRoleListState(): RoleEnum[] {
|
get getRoleListState(): RoleEnum[] {
|
||||||
return this.roleListState.length > 0 ? this.roleListState : (getLocal(ROLES_KEY) as RoleEnum[]);
|
return this.roleListState.length > 0 ? this.roleListState : getCache<RoleEnum[]>(ROLES_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Mutation
|
@Mutation
|
||||||
@ -59,25 +75,19 @@ class User extends VuexModule {
|
|||||||
@Mutation
|
@Mutation
|
||||||
commitUserInfoState(info: UserInfo): void {
|
commitUserInfoState(info: UserInfo): void {
|
||||||
this.userInfoState = info;
|
this.userInfoState = info;
|
||||||
if (info) {
|
setCache(USER_INFO_KEY, info);
|
||||||
setLocal(USER_INFO_KEY, info, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Mutation
|
@Mutation
|
||||||
commitRoleListState(roleList: RoleEnum[]): void {
|
commitRoleListState(roleList: RoleEnum[]): void {
|
||||||
this.roleListState = roleList;
|
this.roleListState = roleList;
|
||||||
if (roleList) {
|
setCache(ROLES_KEY, roleList);
|
||||||
setLocal(ROLES_KEY, roleList, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Mutation
|
@Mutation
|
||||||
commitTokenState(info: string): void {
|
commitTokenState(info: string): void {
|
||||||
this.tokenState = info;
|
this.tokenState = info;
|
||||||
if (info) {
|
setCache(TOKEN_KEY, info);
|
||||||
setLocal(TOKEN_KEY, info, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
4
src/types/config.d.ts
vendored
4
src/types/config.d.ts
vendored
@ -1,6 +1,6 @@
|
|||||||
// 左侧菜单, 顶部菜单
|
|
||||||
import { MenuTypeEnum, MenuModeEnum, TriggerEnum } from '/@/enums/menuEnum';
|
import { MenuTypeEnum, MenuModeEnum, TriggerEnum } from '/@/enums/menuEnum';
|
||||||
import { ContentEnum, PermissionModeEnum, ThemeEnum, RouterTransitionEnum } from '/@/enums/appEnum';
|
import { ContentEnum, PermissionModeEnum, ThemeEnum, RouterTransitionEnum } from '/@/enums/appEnum';
|
||||||
|
import { CacheTypeEnum } from '/@/enums/cacheEnum';
|
||||||
import type { LocaleType } from '/@/locales/types';
|
import type { LocaleType } from '/@/locales/types';
|
||||||
|
|
||||||
export interface MenuSetting {
|
export interface MenuSetting {
|
||||||
@ -76,6 +76,8 @@ export interface TransitionSetting {
|
|||||||
export interface ProjectConfig {
|
export interface ProjectConfig {
|
||||||
locale: LocaleSetting;
|
locale: LocaleSetting;
|
||||||
|
|
||||||
|
permissionCacheType: CacheTypeEnum;
|
||||||
|
|
||||||
// 是否显示配置按钮
|
// 是否显示配置按钮
|
||||||
showSettingButton: boolean;
|
showSettingButton: boolean;
|
||||||
// 权限模式
|
// 权限模式
|
||||||
|
@ -29,8 +29,12 @@ function initCache() {
|
|||||||
initCache();
|
initCache();
|
||||||
|
|
||||||
export function setLocal(key: string, value: any, immediate = false) {
|
export function setLocal(key: string, value: any, immediate = false) {
|
||||||
cacheStore.local[BASE_LOCAL_CACHE_KEY] = cacheStore.local[BASE_LOCAL_CACHE_KEY] || {};
|
const local = ls.get(BASE_LOCAL_CACHE_KEY)?.[BASE_LOCAL_CACHE_KEY] || {};
|
||||||
|
|
||||||
|
cacheStore.local[BASE_LOCAL_CACHE_KEY] =
|
||||||
|
{ ...local, ...cacheStore.local[BASE_LOCAL_CACHE_KEY] } || {};
|
||||||
cacheStore.local[BASE_LOCAL_CACHE_KEY][key] = value;
|
cacheStore.local[BASE_LOCAL_CACHE_KEY][key] = value;
|
||||||
|
|
||||||
if (immediate) {
|
if (immediate) {
|
||||||
ls.set(BASE_LOCAL_CACHE_KEY, cacheStore.local);
|
ls.set(BASE_LOCAL_CACHE_KEY, cacheStore.local);
|
||||||
}
|
}
|
||||||
@ -50,16 +54,21 @@ export function removeLocal(key: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clearLocal() {
|
export function clearLocal(immediate = false) {
|
||||||
cacheStore.local = {};
|
cacheStore.local = {};
|
||||||
|
immediate && ls.remove(BASE_LOCAL_CACHE_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setSession(key: string, value: any, immediate = false) {
|
export function setSession(key: string, value: any, immediate = false) {
|
||||||
cacheStore.session[BASE_SESSION_CACHE_KEY] = cacheStore.session[BASE_SESSION_CACHE_KEY] || {};
|
const session = ss.get(BASE_SESSION_CACHE_KEY)?.[BASE_SESSION_CACHE_KEY] || {};
|
||||||
|
|
||||||
|
cacheStore.session[BASE_SESSION_CACHE_KEY] =
|
||||||
|
{ ...session, ...cacheStore.session[BASE_SESSION_CACHE_KEY] } || {};
|
||||||
|
|
||||||
cacheStore.session[BASE_SESSION_CACHE_KEY][key] = value;
|
cacheStore.session[BASE_SESSION_CACHE_KEY][key] = value;
|
||||||
|
|
||||||
if (immediate) {
|
if (immediate) {
|
||||||
const cache = cacheStore.session;
|
ss.set(BASE_SESSION_CACHE_KEY, cacheStore.session);
|
||||||
ss.set(BASE_SESSION_CACHE_KEY, cache);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,8 +86,9 @@ export function getSession<T>(key: string): T | null {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clearSession() {
|
export function clearSession(immediate = false) {
|
||||||
cacheStore.session = {};
|
cacheStore.session = {};
|
||||||
|
immediate && ss.remove(BASE_SESSION_CACHE_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clearAll() {
|
export function clearAll() {
|
||||||
@ -86,14 +96,17 @@ export function clearAll() {
|
|||||||
clearSession();
|
clearSession();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function persistentCache() {
|
||||||
|
const localCache = cacheStore.local;
|
||||||
|
const sessionCache = cacheStore.session;
|
||||||
|
ls.set(BASE_LOCAL_CACHE_KEY, localCache);
|
||||||
|
ss.set(BASE_SESSION_CACHE_KEY, sessionCache);
|
||||||
|
}
|
||||||
|
|
||||||
(() => {
|
(() => {
|
||||||
// /** Write to local before closing window */
|
// /** Write to local before closing window */
|
||||||
window.addEventListener('beforeunload', () => {
|
window.addEventListener('beforeunload', () => {
|
||||||
const localCache = cacheStore.local;
|
persistentCache();
|
||||||
const sessionCache = cacheStore.session;
|
|
||||||
|
|
||||||
ls.set(BASE_LOCAL_CACHE_KEY, localCache);
|
|
||||||
ss.set(BASE_SESSION_CACHE_KEY, sessionCache);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function storageChange(e: any) {
|
function storageChange(e: any) {
|
||||||
|
Loading…
Reference in New Issue
Block a user