refactor: ♻️ 字典调整按需加载,api、store和枚举文件命名优化
This commit is contained in:
@@ -8,10 +8,10 @@ export function setupStore(app: App<Element>) {
|
||||
app.use(store);
|
||||
}
|
||||
|
||||
export * from "./modules/app";
|
||||
export * from "./modules/permission";
|
||||
export * from "./modules/settings";
|
||||
export * from "./modules/tags-view";
|
||||
export * from "./modules/user";
|
||||
export * from "./modules/dict";
|
||||
export * from "./modules/app.store";
|
||||
export * from "./modules/permission.store";
|
||||
export * from "./modules/settings.store";
|
||||
export * from "./modules/tags-view.store";
|
||||
export * from "./modules/user.store";
|
||||
export * from "./modules/dict.store";
|
||||
export { store };
|
||||
|
||||
@@ -4,8 +4,8 @@ import defaultSettings from "@/settings";
|
||||
import zhCn from "element-plus/es/locale/lang/zh-cn";
|
||||
import en from "element-plus/es/locale/lang/en";
|
||||
import { store } from "@/store";
|
||||
import { DeviceEnum } from "@/enums/DeviceEnum";
|
||||
import { SidebarStatusEnum } from "@/enums/SidebarStatusEnum";
|
||||
import { DeviceEnum } from "@/enums/settings/device.enum";
|
||||
import { SidebarStatus } from "@/enums/settings/layout.enum";
|
||||
|
||||
export const useAppStore = defineStore("app", () => {
|
||||
// 设备类型
|
||||
@@ -15,9 +15,9 @@ export const useAppStore = defineStore("app", () => {
|
||||
// 语言
|
||||
const language = useStorage("language", defaultSettings.language);
|
||||
// 侧边栏状态
|
||||
const sidebarStatus = useStorage("sidebarStatus", SidebarStatusEnum.CLOSED);
|
||||
const sidebarStatus = useStorage("sidebarStatus", SidebarStatus.CLOSED);
|
||||
const sidebar = reactive({
|
||||
opened: sidebarStatus.value === SidebarStatusEnum.OPENED,
|
||||
opened: sidebarStatus.value === SidebarStatus.OPENED,
|
||||
withoutAnimation: false,
|
||||
});
|
||||
|
||||
@@ -38,19 +38,19 @@ export const useAppStore = defineStore("app", () => {
|
||||
// 切换侧边栏
|
||||
function toggleSidebar() {
|
||||
sidebar.opened = !sidebar.opened;
|
||||
sidebarStatus.value = sidebar.opened ? SidebarStatusEnum.OPENED : SidebarStatusEnum.CLOSED;
|
||||
sidebarStatus.value = sidebar.opened ? SidebarStatus.OPENED : SidebarStatus.CLOSED;
|
||||
}
|
||||
|
||||
// 关闭侧边栏
|
||||
function closeSideBar() {
|
||||
sidebar.opened = false;
|
||||
sidebarStatus.value = SidebarStatusEnum.CLOSED;
|
||||
sidebarStatus.value = SidebarStatus.CLOSED;
|
||||
}
|
||||
|
||||
// 打开侧边栏
|
||||
function openSideBar() {
|
||||
sidebar.opened = true;
|
||||
sidebarStatus.value = SidebarStatusEnum.OPENED;
|
||||
sidebarStatus.value = SidebarStatus.OPENED;
|
||||
}
|
||||
|
||||
// 切换设备
|
||||
55
src/store/modules/dict.store.ts
Normal file
55
src/store/modules/dict.store.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { store } from "@/store";
|
||||
import DictAPI, { type DictItemOption } from "@/api/system/dict.api";
|
||||
|
||||
export const useDictStore = defineStore("dict", () => {
|
||||
// 字典数据缓存
|
||||
const dictCache = useStorage<Record<string, DictItemOption[]>>("dict_cache", {});
|
||||
// 请求队列(防止重复请求)
|
||||
const requestQueue: Record<string, Promise<void>> = {};
|
||||
/**
|
||||
* 缓存字典数据
|
||||
* @param dictCode 字典编码
|
||||
* @param data 字典项列表
|
||||
*/
|
||||
const cacheDictItems = (dictCode: string, data: DictItemOption[]) => {
|
||||
dictCache.value[dictCode] = data;
|
||||
};
|
||||
/**
|
||||
* 加载字典数据(如果缓存中没有则请求)
|
||||
* @param dictCode 字典编码
|
||||
*/
|
||||
const loadDictItems = async (dictCode: string) => {
|
||||
if (dictCache.value[dictCode]) return;
|
||||
// 防止重复请求
|
||||
if (!requestQueue[dictCode]) {
|
||||
requestQueue[dictCode] = DictAPI.getDictItems(dictCode).then((data) => {
|
||||
cacheDictItems(dictCode, data);
|
||||
Reflect.deleteProperty(requestQueue, dictCode);
|
||||
});
|
||||
}
|
||||
await requestQueue[dictCode];
|
||||
};
|
||||
/**
|
||||
* 获取字典项列表
|
||||
* @param dictCode 字典编码
|
||||
* @returns 字典项列表
|
||||
*/
|
||||
const getDictItems = (dictCode: string): DictItemOption[] => {
|
||||
return dictCache.value[dictCode] || [];
|
||||
};
|
||||
/**
|
||||
* 清空字典缓存
|
||||
*/
|
||||
const clearDictCache = () => {
|
||||
dictCache.value = {};
|
||||
};
|
||||
return {
|
||||
loadDictItems,
|
||||
getDictItems,
|
||||
clearDictCache,
|
||||
};
|
||||
});
|
||||
|
||||
export function useDictStoreHook() {
|
||||
return useDictStore(store);
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
import { store } from "@/store";
|
||||
import DictionaryAPI, { type DictVO, type DictData } from "@/api/system/dict";
|
||||
|
||||
export const useDictStore = defineStore("dict", () => {
|
||||
const dictionary = useStorage<Record<string, DictData[]>>("dictionary", {});
|
||||
|
||||
const setDictionary = (dict: DictVO) => {
|
||||
dictionary.value[dict.dictCode] = dict.dictDataList;
|
||||
};
|
||||
|
||||
const loadDictionaries = async () => {
|
||||
const dictList = await DictionaryAPI.getList();
|
||||
dictList.forEach(setDictionary);
|
||||
};
|
||||
|
||||
const getDictionary = (dictCode: string): DictData[] => {
|
||||
return dictionary.value[dictCode] || [];
|
||||
};
|
||||
|
||||
const clearDictionaryCache = () => {
|
||||
dictionary.value = {};
|
||||
};
|
||||
|
||||
const updateDictionaryCache = async () => {
|
||||
clearDictionaryCache(); // 先清除旧缓存
|
||||
await loadDictionaries(); // 重新加载最新字典数据
|
||||
};
|
||||
|
||||
return {
|
||||
dictionary,
|
||||
setDictionary,
|
||||
loadDictionaries,
|
||||
getDictionary,
|
||||
clearDictionaryCache,
|
||||
updateDictionaryCache,
|
||||
};
|
||||
});
|
||||
|
||||
export function useDictStoreHook() {
|
||||
return useDictStore(store);
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import { constantRoutes } from "@/router";
|
||||
import { store } from "@/store";
|
||||
import router from "@/router";
|
||||
|
||||
import MenuAPI, { type RouteVO } from "@/api/system/menu";
|
||||
import MenuAPI, { type RouteVO } from "@/api/system/menu.api";
|
||||
const modules = import.meta.glob("../../views/**/**.vue");
|
||||
const Layout = () => import("@/layout/index.vue");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import defaultSettings from "@/settings";
|
||||
import { SidebarColorEnum, ThemeEnum } from "@/enums/ThemeEnum";
|
||||
import { LayoutEnum } from "@/enums/LayoutEnum";
|
||||
import { SidebarColor, ThemeMode } from "@/enums/settings/theme.enum";
|
||||
import { LayoutMode } from "@/enums/settings/layout.enum";
|
||||
import { generateThemeColors, applyTheme, toggleDarkMode, toggleSidebarColor } from "@/utils/theme";
|
||||
|
||||
type SettingsValue = boolean | string;
|
||||
@@ -18,7 +18,7 @@ export const useSettingsStore = defineStore("setting", () => {
|
||||
defaultSettings.sidebarColorScheme
|
||||
);
|
||||
// 布局
|
||||
const layout = useStorage<LayoutEnum>("layout", defaultSettings.layout as LayoutEnum);
|
||||
const layout = useStorage<LayoutMode>("layout", defaultSettings.layout as LayoutMode);
|
||||
// 水印
|
||||
const watermarkEnabled = useStorage<boolean>(
|
||||
"watermarkEnabled",
|
||||
@@ -33,7 +33,7 @@ export const useSettingsStore = defineStore("setting", () => {
|
||||
watch(
|
||||
[theme, themeColor],
|
||||
([newTheme, newThemeColor]) => {
|
||||
toggleDarkMode(newTheme === ThemeEnum.DARK);
|
||||
toggleDarkMode(newTheme === ThemeMode.DARK);
|
||||
const colors = generateThemeColors(newThemeColor);
|
||||
applyTheme(colors);
|
||||
},
|
||||
@@ -44,7 +44,7 @@ export const useSettingsStore = defineStore("setting", () => {
|
||||
watch(
|
||||
[sidebarColorScheme],
|
||||
([newSidebarColorScheme]) => {
|
||||
toggleSidebarColor(newSidebarColorScheme === SidebarColorEnum.CLASSIC_BLUE);
|
||||
toggleSidebarColor(newSidebarColorScheme === SidebarColor.CLASSIC_BLUE);
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
@@ -75,7 +75,7 @@ export const useSettingsStore = defineStore("setting", () => {
|
||||
themeColor.value = color;
|
||||
}
|
||||
|
||||
function changeLayout(val: LayoutEnum) {
|
||||
function changeLayout(val: LayoutMode) {
|
||||
layout.value = val;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { store } from "@/store";
|
||||
import { usePermissionStoreHook } from "@/store/modules/permission";
|
||||
import { useDictStoreHook } from "@/store/modules/dict";
|
||||
import { usePermissionStoreHook } from "@/store/modules/permission.store";
|
||||
import { useDictStoreHook } from "@/store/modules/dict.store";
|
||||
|
||||
import AuthAPI, { type LoginFormData } from "@/api/auth";
|
||||
import UserAPI, { type UserInfo } from "@/api/system/user";
|
||||
import AuthAPI, { type LoginFormData } from "@/api/auth.api";
|
||||
import UserAPI, { type UserInfo } from "@/api/system/user.api";
|
||||
|
||||
import { setAccessToken, setRefreshToken, getRefreshToken, clearToken } from "@/utils/auth";
|
||||
|
||||
@@ -60,7 +60,7 @@ export const useUserStore = defineStore("user", () => {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
AuthAPI.logout()
|
||||
.then(() => {
|
||||
clearUserData();
|
||||
clearSessionAndCache();
|
||||
resolve();
|
||||
})
|
||||
.catch((error) => {
|
||||
@@ -90,15 +90,13 @@ export const useUserStore = defineStore("user", () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理用户数据
|
||||
*
|
||||
* @returns
|
||||
* 清除用户会话和缓存
|
||||
*/
|
||||
function clearUserData() {
|
||||
function clearSessionAndCache() {
|
||||
return new Promise<void>((resolve) => {
|
||||
clearToken();
|
||||
usePermissionStoreHook().resetRouter();
|
||||
useDictStoreHook().clearDictionaryCache();
|
||||
useDictStoreHook().clearDictCache();
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
@@ -108,7 +106,7 @@ export const useUserStore = defineStore("user", () => {
|
||||
getUserInfo,
|
||||
login,
|
||||
logout,
|
||||
clearUserData,
|
||||
clearSessionAndCache,
|
||||
refreshToken,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user