chore: 重构字典封装(还有问题)
This commit is contained in:
parent
f2912f64fc
commit
43df950806
@ -1,64 +1,42 @@
|
|||||||
|
import { ref } from 'vue';
|
||||||
import { fetchGetDictDataByType } from '@/service/api';
|
import { fetchGetDictDataByType } from '@/service/api';
|
||||||
import { useDictStore } from '@/store/modules/dict';
|
import { useDictStore } from '@/store/modules/dict';
|
||||||
|
|
||||||
export function useDict() {
|
export function useDict() {
|
||||||
const dictStore = useDictStore();
|
const dictStore = useDictStore();
|
||||||
|
|
||||||
async function getDictData(...args: string[]) {
|
async function getDictData(dictType: string) {
|
||||||
const dictData: { [key: string]: Api.System.DictData[] } = {};
|
const dicts = dictStore.getDict(dictType);
|
||||||
const promises = args.map(async dictType => {
|
if (dicts) {
|
||||||
dictData[dictType] = [];
|
return dicts;
|
||||||
const dicts = dictStore.getDict(dictType);
|
}
|
||||||
if (dicts) {
|
const { data, error } = await fetchGetDictDataByType(dictType);
|
||||||
dictData[dictType] = dicts;
|
if (error) return [];
|
||||||
return;
|
dictStore.setDict(dictType, data);
|
||||||
}
|
return data;
|
||||||
const { data, error } = await fetchGetDictDataByType(dictType);
|
|
||||||
if (error) return;
|
|
||||||
dictData[dictType] = data;
|
|
||||||
dictStore.setDict(dictType, data);
|
|
||||||
});
|
|
||||||
await Promise.all(promises);
|
|
||||||
return dictData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getDictRecord(...args: string[]) {
|
function getDictRecord(dictType: string) {
|
||||||
const dictRecord: { [key: string]: { [key: string]: string } } = {};
|
const dictRecord = ref<{ [key: string]: string }>({});
|
||||||
const dictData = await getDictData(...args);
|
getDictData(dictType).then(dictData => {
|
||||||
Object.keys(dictData).forEach(dictType => {
|
dictData.forEach(dict => {
|
||||||
const data: { [key: string]: string } = {};
|
dictRecord.value[dict.dictValue!] = dict.dictLabel!;
|
||||||
Object.keys(dictData);
|
|
||||||
dictData[dictType].forEach(dict => {
|
|
||||||
data[dict.dictValue!] = dict.dictLabel!;
|
|
||||||
});
|
});
|
||||||
dictRecord[dictType] = data;
|
|
||||||
});
|
});
|
||||||
return dictRecord;
|
return dictRecord;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getDictOptions(...args: string[]) {
|
function getDictOptions(dictType: string) {
|
||||||
const dictOptions: { [key: string]: CommonType.Option[] } = {};
|
const dictOptions = ref<CommonType.Option[]>([]);
|
||||||
const dictData = await getDictData(...args);
|
getDictData(dictType).then(dictData => {
|
||||||
Object.keys(dictData).forEach(dictType => {
|
dictOptions.value = dictData.map(dict => ({ label: dict.dictLabel!, value: dict.dictValue! }));
|
||||||
dictOptions[dictType] = dictData[dictType].map(dict => ({ label: dict.dictLabel!, value: dict.dictValue! }));
|
|
||||||
});
|
});
|
||||||
return dictOptions;
|
return dictOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function transformDictByCode(type: string, code: string) {
|
|
||||||
const dictData = await getDictData(type);
|
|
||||||
return transformDictByOptions(code, dictData[type]);
|
|
||||||
}
|
|
||||||
|
|
||||||
function transformDictByOptions(code: string, options: Api.System.DictData[]) {
|
|
||||||
return options.find(item => item.dictValue === code)?.dictLabel;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getDictData,
|
getDictData,
|
||||||
getDictRecord,
|
getDictRecord,
|
||||||
getDictOptions,
|
getDictOptions
|
||||||
transformDictByCode,
|
|
||||||
transformDictByOptions
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -259,17 +259,8 @@ const btnColumns: DataTableColumns<Api.System.Menu> = [
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
const enableStatusRecord = ref<CommonType.Record>({});
|
const { getDictRecord } = useDict();
|
||||||
const showHideRecord = ref<CommonType.Record>({});
|
const showHideRecord = getDictRecord('sys_show_hide');
|
||||||
|
|
||||||
async function initDictData() {
|
|
||||||
const { getDictRecord } = useDict();
|
|
||||||
const { sys_show_hide, sys_normal_disable } = await getDictRecord('sys_show_hide', 'sys_normal_disable');
|
|
||||||
enableStatusRecord.value = sys_normal_disable;
|
|
||||||
showHideRecord.value = sys_show_hide;
|
|
||||||
}
|
|
||||||
|
|
||||||
initDictData();
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
@ -40,6 +40,10 @@ const visible = defineModel<boolean>('visible', {
|
|||||||
default: false
|
default: false
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { getDictOptions } = useDict();
|
||||||
|
const showHideOptions = getDictOptions('sys_show_hide');
|
||||||
|
const enableStatusOptions = getDictOptions('sys_normal_disable');
|
||||||
|
|
||||||
const iconType = ref<Api.System.IconType>('1');
|
const iconType = ref<Api.System.IconType>('1');
|
||||||
const { formRef, validate, restoreValidation } = useNaiveForm();
|
const { formRef, validate, restoreValidation } = useNaiveForm();
|
||||||
const { defaultRequiredRule } = useFormRules();
|
const { defaultRequiredRule } = useFormRules();
|
||||||
@ -217,7 +221,6 @@ watch(visible, () => {
|
|||||||
if (visible.value) {
|
if (visible.value) {
|
||||||
handleInitModel();
|
handleInitModel();
|
||||||
restoreValidation();
|
restoreValidation();
|
||||||
initDictData();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -251,16 +254,6 @@ const FormTipComponent = defineComponent({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const enableStatusOptions = ref<CommonType.Option[]>([]);
|
|
||||||
const showHideOptions = ref<CommonType.Option[]>([]);
|
|
||||||
|
|
||||||
async function initDictData() {
|
|
||||||
const { getDictOptions } = useDict();
|
|
||||||
const { sys_show_hide, sys_normal_disable } = await getDictOptions('sys_show_hide', 'sys_normal_disable');
|
|
||||||
enableStatusOptions.value = sys_normal_disable;
|
|
||||||
showHideOptions.value = sys_show_hide;
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
Loading…
Reference in New Issue
Block a user