Merge branch 'dev-v5' of https://gitee.com/yudaocode/yudao-ui-admin-vben into dev-v5【788061ad】

pull/62/head
YunaiV 2025-03-25 22:51:51 +08:00
parent 4556a3e03b
commit 1d1e70ee66
3 changed files with 64 additions and 50 deletions

View File

@ -50,6 +50,7 @@ function createRequestClient(baseURL: string, options?: RequestClientOptions) {
*/ */
async function doRefreshToken() { async function doRefreshToken() {
const accessStore = useAccessStore(); const accessStore = useAccessStore();
debugger
const resp = await refreshTokenApi(); const resp = await refreshTokenApi();
const newToken = resp.data; const newToken = resp.data;
accessStore.setAccessToken(newToken); accessStore.setAccessToken(newToken);

View File

@ -6,7 +6,7 @@ import { useAccessStore, useUserStore } from '@vben/stores';
import { startProgress, stopProgress } from '@vben/utils'; import { startProgress, stopProgress } from '@vben/utils';
import { accessRoutes, coreRouteNames } from '#/router/routes'; import { accessRoutes, coreRouteNames } from '#/router/routes';
import { useAuthStore } from '#/store'; import { useAuthStore, useDictStore } from '#/store';
import { generateAccess } from './access'; import { generateAccess } from './access';
import { message } from 'ant-design-vue'; import { message } from 'ant-design-vue';
@ -51,6 +51,7 @@ function setupAccessGuard(router: Router) {
const accessStore = useAccessStore(); const accessStore = useAccessStore();
const userStore = useUserStore(); const userStore = useUserStore();
const authStore = useAuthStore(); const authStore = useAuthStore();
const dictStore = useDictStore();
// 基本路由,这些路由不需要进入权限拦截 // 基本路由,这些路由不需要进入权限拦截
if (coreRouteNames.includes(to.name as string)) { if (coreRouteNames.includes(to.name as string)) {
@ -92,6 +93,9 @@ function setupAccessGuard(router: Router) {
return true; return true;
} }
// 加载字典数据(不阻塞加载)
dictStore.setDictMap();
// 生成路由表 // 生成路由表
// 当前登录用户拥有的角色标识列表 // 当前登录用户拥有的角色标识列表
let userInfo = userStore.userInfo; let userInfo = userStore.userInfo;

View File

@ -1,69 +1,78 @@
// TODO @芋艿:【可优化】挪到 packages/stores/src/modules import { StorageManager } from '@vben/utils';
import { acceptHMRUpdate, defineStore } from 'pinia'; import { acceptHMRUpdate, defineStore } from 'pinia';
export interface DictItem { import { getSimpleDictDataList } from '#/api/system/dict/dict.data';
const DICT_STORAGE_KEY = 'DICT_STORAGE__';
interface DictValueType {
value: any;
label: string;
colorType?: string; colorType?: string;
cssClass?: string; cssClass?: string;
label: string;
value: string;
} }
export type Dict = Record<string, DictItem[]>; // interface DictTypeType {
// dictType: string;
// dictValue: DictValueType[];
// }
interface DictState { interface DictState {
dictCache: Dict; dictMap: Map<string, DictValueType[]>;
isSetDict: boolean;
} }
const storage = new StorageManager({
prefix: import.meta.env.VITE_APP_NAMESPACE,
storageType: 'sessionStorage',
});
export const useDictStore = defineStore('dict', { export const useDictStore = defineStore('dict', {
actions: { actions: {
getDictData(dictType: string, value: any) { async setDictMap() {
const dict = this.dictCache[dictType]; try {
if (!dict) { const dataRes = await getSimpleDictDataList();
return undefined;
} const dictDataMap = new Map<string, DictValueType[]>();
return (
dict.find((d) => d.value === value || d.value === value.toString()) ?? dataRes.forEach((item: any) => {
undefined let dictTypeArray = dictDataMap.get(item.dictType);
); if (!dictTypeArray) {
}, dictTypeArray = [];
getDictOptions(dictType: string) { }
const dictOptions = this.dictCache[dictType]; dictTypeArray.push({
if (!dictOptions) { value: item.value,
return []; label: item.label,
} colorType: item.colorType,
return dictOptions; cssClass: item.cssClass,
}, });
setDictCache(dicts: Dict) { dictDataMap.set(item.dictType, dictTypeArray);
this.dictCache = dicts;
},
setDictCacheByApi(
api: (params: Record<string, any>) => Promise<Record<string, any>[]>,
params: Record<string, any>,
labelField: string = 'label',
valueField: string = 'value',
) {
api(params).then((dicts) => {
const dictCacheData: Dict = {};
dicts.forEach((dict) => {
dictCacheData[dict.dictType] = dicts
.filter((d) => d.dictType === dict.dictType)
.map((d) => ({
colorType: d.colorType,
cssClass: d.cssClass,
label: d[labelField],
value: d[valueField],
}));
}); });
this.setDictCache(dictCacheData);
}); this.dictMap = dictDataMap;
this.isSetDict = true;
// 将字典数据存储到 sessionStorage 中
storage.setItem(DICT_STORAGE_KEY, dictDataMap, 60);
} catch (error) {
console.error('Failed to set dictionary values:', error);
}
}, },
}, },
persist: { getters: {
// 持久化 getDictMap: (state) => state.dictMap,
pick: ['dictCache'], getDictData: (state) => (dictType: string) => {
return state.dictMap.get(dictType);
},
getDictOptions: (state) => (dictType: string) => {
return state.dictMap.get(dictType);
},
}, },
persist: [{ pick: ['dictMap', 'isSetDict'] }],
state: (): DictState => ({ state: (): DictState => ({
dictCache: {}, dictMap: new Map<string, DictValueType[]>(),
isSetDict: false,
}), }),
}); });