oa_front/src/store/modules/dict.ts

32 lines
876 B
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 13:33:45 +08:00
import { DictEnum } from '@/enums/dictEnum';
2024-03-04 11:29:26 +08:00
const needCachedKey = [
2024-03-04 14:17:40 +08:00
DictEnum.ContractType,
2024-03-08 10:38:53 +08:00
DictEnum.Unit,
DictEnum.Vehicle
2024-03-04 11:29:26 +08:00
];
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();
const getDictItemsByCode = (code: DictEnum): API.DictItemEntity[] => {
2024-03-04 13:33:45 +08:00
return dictTypes.value.find((item) => item.code === code)?.dictItems || [];
2024-02-29 16:51:37 +08:00
};
return { dictTypes, getDictTypes, getDictItemsByCode };
});
// 在组件setup函数外使用
export function useDictStoreWithOut() {
return useDictStore(store);
}