|
@@ -1,28 +1,22 @@
|
|
|
import { ref, toRefs } from "vue";
|
|
import { ref, toRefs } from "vue";
|
|
|
import { useDictStore } from "@/store/modules/dict";
|
|
import { useDictStore } from "@/store/modules/dict";
|
|
|
-import { getDictDataByType } from "@/api/system/dict"; // 根据key获取字典接口
|
|
|
|
|
import { isString } from "@pureadmin/utils";
|
|
import { isString } from "@pureadmin/utils";
|
|
|
|
|
|
|
|
export function useDict(...args: string[]) {
|
|
export function useDict(...args: string[]) {
|
|
|
- const res = ref({});
|
|
|
|
|
|
|
+ const res = ref<Record<string, any[]>>({});
|
|
|
const dictStore = useDictStore();
|
|
const dictStore = useDictStore();
|
|
|
|
|
|
|
|
args.forEach(dictType => {
|
|
args.forEach(dictType => {
|
|
|
res.value[dictType] = [];
|
|
res.value[dictType] = [];
|
|
|
const dicts = dictStore.getDict(dictType);
|
|
const dicts = dictStore.getDict(dictType);
|
|
|
|
|
|
|
|
- if (dicts) {
|
|
|
|
|
|
|
+ if (dicts && dicts.length) {
|
|
|
res.value[dictType] = dicts;
|
|
res.value[dictType] = dicts;
|
|
|
} else {
|
|
} else {
|
|
|
- getDictDataByType(dictType)
|
|
|
|
|
- .then(resp => {
|
|
|
|
|
- const dictData = resp.data.map(p => ({
|
|
|
|
|
- label: p.dictLabel,
|
|
|
|
|
- value: p.dictValue,
|
|
|
|
|
- class: p.listClass
|
|
|
|
|
- }));
|
|
|
|
|
|
|
+ dictStore
|
|
|
|
|
+ .fetchDictByType(dictType)
|
|
|
|
|
+ .then(dictData => {
|
|
|
res.value[dictType] = dictData;
|
|
res.value[dictType] = dictData;
|
|
|
- dictStore.setDict(dictType, dictData);
|
|
|
|
|
})
|
|
})
|
|
|
.catch(error => {
|
|
.catch(error => {
|
|
|
console.error(`获取字典${dictType}失败:`, error);
|
|
console.error(`获取字典${dictType}失败:`, error);
|
|
@@ -34,9 +28,7 @@ export function useDict(...args: string[]) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export function useDictValue<T>(dictType: T, searchValue: string) {
|
|
export function useDictValue<T>(dictType: T, searchValue: string) {
|
|
|
- if (!isString(searchValue)) {
|
|
|
|
|
- searchValue = String(searchValue);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (!isString(searchValue)) searchValue = String(searchValue);
|
|
|
const dictStore = useDictStore();
|
|
const dictStore = useDictStore();
|
|
|
const dicts = dictStore.getDict(dictType as string) || [];
|
|
const dicts = dictStore.getDict(dictType as string) || [];
|
|
|
const item = dicts.find((d: any) => d.value === searchValue);
|
|
const item = dicts.find((d: any) => d.value === searchValue);
|
|
@@ -44,11 +36,9 @@ export function useDictValue<T>(dictType: T, searchValue: string) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export function useDictClass<T>(dictType: T, searchValue: string) {
|
|
export function useDictClass<T>(dictType: T, searchValue: string) {
|
|
|
- if (!isString(searchValue)) {
|
|
|
|
|
- searchValue = String(searchValue);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (!isString(searchValue)) searchValue = String(searchValue);
|
|
|
const dictStore = useDictStore();
|
|
const dictStore = useDictStore();
|
|
|
const dicts = dictStore.getDict(dictType as string) || [];
|
|
const dicts = dictStore.getDict(dictType as string) || [];
|
|
|
const item = dicts.find((d: any) => d.value === searchValue);
|
|
const item = dicts.find((d: any) => d.value === searchValue);
|
|
|
return item ? item.class : null;
|
|
return item ? item.class : null;
|
|
|
-}
|
|
|
|
|
|
|
+}
|