oa_front/src/store/modules/dict.ts

37 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-02-29 16:51:37 +08:00
import { ref } from 'vue';
import { defineStore } from 'pinia';
import Api from '@/api';
import { store } from '@/store';
2024-03-04 11:29:26 +08:00
const needCachedKey = [
'contract_type', // 合同类型
];
2024-02-29 16:51:37 +08:00
export const useDictStore = defineStore('dict', () => {
const dictTypes = ref<API.DictTypeDto[]>([]);
const getDictTypes = async () => {
2024-03-04 13:17:32 +08:00
dictTypes.value = await Api.systemDictType.dictTypeGetAll({
storeCodes: needCachedKey,
withItems: true,
});
2024-02-29 16:51:37 +08:00
};
2024-03-04 13:17:32 +08:00
getDictTypes();
2024-02-29 16:51:37 +08:00
const getDictItemsByCode = async (code: string): Promise<API.DictItemEntity[]> => {
try {
const dictType = dictTypes.value.find((item) => item.code === code);
if (dictType) {
return await Api.systemDictItem.dictItemGetAllByTypeId(dictType.id!);
}
return Promise.resolve([]);
} catch (error) {
return Promise.reject(error);
}
};
return { dictTypes, getDictTypes, getDictItemsByCode };
});
// 在组件setup函数外使用
export function useDictStoreWithOut() {
return useDictStore(store);
}