35 lines
998 B
TypeScript
35 lines
998 B
TypeScript
import { ref } from 'vue';
|
|
import { defineStore } from 'pinia';
|
|
import Api from '@/api';
|
|
import { store } from '@/store';
|
|
// interface DictState {
|
|
// /** 需要缓存的路由组件名称列表 */
|
|
// list: API.DictItemDto[];
|
|
// }
|
|
|
|
export const useDictStore = defineStore('dict', () => {
|
|
const dictTypes = ref<API.DictTypeDto[]>([]);
|
|
const getDictTypes = async () => {
|
|
dictTypes.value = await Api.systemDictType.dictTypeGetAll();
|
|
};
|
|
|
|
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);
|
|
}
|