chore: 完善字典封装

This commit is contained in:
xlsea 2024-09-06 15:55:35 +08:00
parent c47de5f834
commit 226568b3af
3 changed files with 42 additions and 27 deletions

View File

@ -2,41 +2,55 @@ 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(dictType: string, immediate: boolean = true) {
const dictStore = useDictStore(); const dictStore = useDictStore();
async function getDictData(dictType: string) { const data = ref<Api.System.DictData[]>([]);
const record = ref<Record<string, string>>({});
const options = ref<CommonType.Option[]>([]);
async function getData() {
const dicts = dictStore.getDict(dictType); const dicts = dictStore.getDict(dictType);
if (dicts) { if (dicts) {
return dicts; data.value = dicts;
return;
} }
const { data, error } = await fetchGetDictDataByType(dictType); const { data: dictData, error } = await fetchGetDictDataByType(dictType);
if (error) return []; if (error) return;
dictStore.setDict(dictType, data); dictStore.setDict(dictType, dictData);
return data; data.value = dictData;
} }
function getDictRecord(dictType: string) { async function getRecord() {
const dictRecord = ref<{ [key: string]: string }>({}); if (!data.value.length) {
getDictData(dictType).then(dictData => { await getData();
dictData.forEach(dict => { }
dictRecord.value[dict.dictValue!] = dict.dictLabel!; data.value.forEach(dict => {
}); record.value[dict.dictValue!] = dict.dictLabel!;
}); });
return dictRecord;
} }
function getDictOptions(dictType: string) { async function getOptions() {
const dictOptions = ref<CommonType.Option[]>([]); if (!data.value.length) {
getDictData(dictType).then(dictData => { await getData();
dictOptions.value = dictData.map(dict => ({ label: dict.dictLabel!, value: dict.dictValue! })); }
options.value = data.value.map(dict => ({ label: dict.dictLabel!, value: dict.dictValue! }));
}
if (immediate) {
getData().then(() => {
getRecord();
getOptions();
}); });
return dictOptions;
} }
return { return {
getDictData, data,
getDictRecord, record,
getDictOptions options,
getData,
getRecord,
getOptions
}; };
} }

View File

@ -259,8 +259,7 @@ const btnColumns: DataTableColumns<Api.System.Menu> = [
} }
]; ];
const { getDictRecord } = useDict(); const { record: showHideRecord } = useDict('sys_show_hide');
const showHideRecord = getDictRecord('sys_show_hide');
</script> </script>
<template> <template>

View File

@ -40,9 +40,8 @@ const visible = defineModel<boolean>('visible', {
default: false default: false
}); });
const { getDictOptions } = useDict(); const { options: showHideOptions, getOptions: getShowHideOptions } = useDict('sys_show_hide', false);
const showHideOptions = getDictOptions('sys_show_hide'); const { options: enableStatusOptions, getOptions: getNormalDisableOptions } = useDict('sys_normal_disable', false);
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();
@ -221,6 +220,9 @@ watch(visible, () => {
if (visible.value) { if (visible.value) {
handleInitModel(); handleInitModel();
restoreValidation(); restoreValidation();
getShowHideOptions();
getShowHideOptions();
getNormalDisableOptions();
} }
}); });