refactor: 将字典从store 移动到@vben/stores
parent
cf8745d844
commit
48ec3e15ab
|
|
@ -3,14 +3,14 @@ import type { Router } from 'vue-router';
|
||||||
import { LOGIN_PATH } from '@vben/constants';
|
import { LOGIN_PATH } from '@vben/constants';
|
||||||
import { $t } from '@vben/locales';
|
import { $t } from '@vben/locales';
|
||||||
import { preferences } from '@vben/preferences';
|
import { preferences } from '@vben/preferences';
|
||||||
import { useAccessStore, useUserStore } from '@vben/stores';
|
import { useAccessStore, useDictStore, useUserStore } from '@vben/stores';
|
||||||
import { startProgress, stopProgress } from '@vben/utils';
|
import { startProgress, stopProgress } from '@vben/utils';
|
||||||
|
|
||||||
import { message } from 'ant-design-vue';
|
import { message } from 'ant-design-vue';
|
||||||
|
|
||||||
import { getSimpleDictDataList } from '#/api/system/dict/data';
|
import { getSimpleDictDataList } from '#/api/system/dict/data';
|
||||||
import { accessRoutes, coreRouteNames } from '#/router/routes';
|
import { accessRoutes, coreRouteNames } from '#/router/routes';
|
||||||
import { useAuthStore, useDictStore } from '#/store';
|
import { useAuthStore } from '#/store';
|
||||||
|
|
||||||
import { generateAccess } from './access';
|
import { generateAccess } from './access';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1 @@
|
||||||
export * from './auth';
|
export * from './auth';
|
||||||
export * from './dict';
|
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,11 @@
|
||||||
// TODO @芋艿:后续再优化
|
// TODO @芋艿:后续再优化
|
||||||
// TODO @芋艿:可以共享么?
|
// TODO @芋艿:可以共享么?
|
||||||
|
|
||||||
import type { DictItem } from '#/store';
|
import type { DictItem } from '@vben/stores';
|
||||||
|
|
||||||
|
import { useDictStore } from '@vben/stores';
|
||||||
import { isObject } from '@vben/utils';
|
import { isObject } from '@vben/utils';
|
||||||
|
|
||||||
import { useDictStore } from '#/store';
|
|
||||||
|
|
||||||
// TODO @dhb52:top-level 调用 导致:"getActivePinia()" was called but there was no active Pinia
|
// TODO @dhb52:top-level 调用 导致:"getActivePinia()" was called but there was no active Pinia
|
||||||
// 先临时移入到方法中
|
// 先临时移入到方法中
|
||||||
// const dictStore = useDictStore();
|
// const dictStore = useDictStore();
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,14 @@ import type { Router } from 'vue-router';
|
||||||
import { LOGIN_PATH } from '@vben/constants';
|
import { LOGIN_PATH } from '@vben/constants';
|
||||||
import { $t } from '@vben/locales';
|
import { $t } from '@vben/locales';
|
||||||
import { preferences } from '@vben/preferences';
|
import { preferences } from '@vben/preferences';
|
||||||
import { useAccessStore, useUserStore } from '@vben/stores';
|
import { useAccessStore, useDictStore, useUserStore } from '@vben/stores';
|
||||||
import { startProgress, stopProgress } from '@vben/utils';
|
import { startProgress, stopProgress } from '@vben/utils';
|
||||||
|
|
||||||
import { ElMessage } from 'element-plus';
|
import { ElMessage } from 'element-plus';
|
||||||
|
|
||||||
import { getSimpleDictDataList } from '#/api/system/dict/data';
|
import { getSimpleDictDataList } from '#/api/system/dict/data';
|
||||||
import { accessRoutes, coreRouteNames } from '#/router/routes';
|
import { accessRoutes, coreRouteNames } from '#/router/routes';
|
||||||
import { useAuthStore, useDictStore } from '#/store';
|
import { useAuthStore } from '#/store';
|
||||||
|
|
||||||
import { generateAccess } from './access';
|
import { generateAccess } from './access';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,74 +0,0 @@
|
||||||
import { acceptHMRUpdate, defineStore } from 'pinia';
|
|
||||||
|
|
||||||
export interface DictItem {
|
|
||||||
colorType?: string;
|
|
||||||
cssClass?: string;
|
|
||||||
label: string;
|
|
||||||
value: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Dict = Record<string, DictItem[]>;
|
|
||||||
|
|
||||||
interface DictState {
|
|
||||||
dictCache: Dict;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO @xingyu:@芋艿:可以共享么?
|
|
||||||
export const useDictStore = defineStore('dict', {
|
|
||||||
actions: {
|
|
||||||
getDictData(dictType: string, value: any) {
|
|
||||||
const dict = this.dictCache[dictType];
|
|
||||||
if (!dict) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return (
|
|
||||||
dict.find((d) => d.value === value || d.value === value.toString()) ??
|
|
||||||
undefined
|
|
||||||
);
|
|
||||||
},
|
|
||||||
getDictOptions(dictType: string) {
|
|
||||||
const dictOptions = this.dictCache[dictType];
|
|
||||||
if (!dictOptions) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
return dictOptions;
|
|
||||||
},
|
|
||||||
setDictCache(dicts: Dict) {
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
|
||||||
persist: {
|
|
||||||
// 持久化
|
|
||||||
pick: ['dictCache'],
|
|
||||||
},
|
|
||||||
state: (): DictState => ({
|
|
||||||
dictCache: {},
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
// 解决热更新问题
|
|
||||||
const hot = import.meta.hot;
|
|
||||||
if (hot) {
|
|
||||||
hot.accept(acceptHMRUpdate(useDictStore, hot));
|
|
||||||
}
|
|
||||||
|
|
@ -1,2 +1 @@
|
||||||
export * from './auth';
|
export * from './auth';
|
||||||
export * from './dict';
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ export const copyValueToTarget = (target: any, source: any) => {
|
||||||
Object.keys(newObj).forEach((key) => {
|
Object.keys(newObj).forEach((key) => {
|
||||||
// 如果不是target中的属性则删除
|
// 如果不是target中的属性则删除
|
||||||
if (!Object.keys(target).includes(key)) {
|
if (!Object.keys(target).includes(key)) {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
||||||
delete newObj[key];
|
delete newObj[key];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
// TODO @芋艿:后续再优化
|
// TODO @芋艿:后续再优化
|
||||||
// TODO @芋艿:可以共享么?
|
// TODO @芋艿:可以共享么?
|
||||||
|
|
||||||
|
import { useDictStore } from '@vben/stores';
|
||||||
import { isObject } from '@vben/utils';
|
import { isObject } from '@vben/utils';
|
||||||
|
|
||||||
import { useDictStore } from '#/store';
|
|
||||||
|
|
||||||
// TODO @dhb52:top-level 调用 导致:"getActivePinia()" was called but there was no active Pinia
|
// TODO @dhb52:top-level 调用 导致:"getActivePinia()" was called but there was no active Pinia
|
||||||
// 先临时移入到方法中
|
// 先临时移入到方法中
|
||||||
// const dictStore = useDictStore();
|
// const dictStore = useDictStore();
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,7 @@ interface DictState {
|
||||||
dictCache: Dict;
|
dictCache: Dict;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO @芋艿:可以共享么?
|
export const useDictStore = defineStore('core-dict', {
|
||||||
export const useDictStore = defineStore('dict', {
|
|
||||||
actions: {
|
actions: {
|
||||||
getDictData(dictType: string, value: any) {
|
getDictData(dictType: string, value: any) {
|
||||||
const dict = this.dictCache[dictType];
|
const dict = this.dictCache[dictType];
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
export * from './access';
|
export * from './access';
|
||||||
|
export * from './dict';
|
||||||
export * from './tabbar';
|
export * from './tabbar';
|
||||||
export * from './user';
|
export * from './user';
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue