2024-11-05 11:23:38 +08:00
|
|
|
import type { Dict } from './../../api/interface/index';
|
2024-10-15 15:37:50 +08:00
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
|
import piniaPersistConfig from '@/stores/helper/persist'
|
|
|
|
|
import { DICT_STORE_KEY } from '@/stores/constant'
|
|
|
|
|
// 模拟数据
|
2024-11-05 11:23:38 +08:00
|
|
|
//import dictData from '@/api/system/dictData'
|
|
|
|
|
import { getDictList } from '@/api/user/login.ts'
|
2024-10-15 15:37:50 +08:00
|
|
|
|
2024-11-05 11:23:38 +08:00
|
|
|
// 初始值设为空数组
|
|
|
|
|
let dictData: Dict[] = [];
|
|
|
|
|
|
|
|
|
|
async function fetchDictData() {
|
|
|
|
|
try {
|
|
|
|
|
const response = await getDictList();
|
|
|
|
|
dictData = response.data as unknown as Dict[];
|
|
|
|
|
console.log('Fetched dictionary data:', dictData);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to fetch dictionary data:', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchDictData();
|
2024-11-04 18:43:24 +08:00
|
|
|
|
2024-10-15 15:37:50 +08:00
|
|
|
export const useDictStore = defineStore({
|
|
|
|
|
id: DICT_STORE_KEY,
|
|
|
|
|
state: () => ({
|
|
|
|
|
dictData,
|
|
|
|
|
}),
|
|
|
|
|
getters: {},
|
|
|
|
|
actions: {
|
|
|
|
|
// 获取字典数据数组,如果为空则返回空数组
|
|
|
|
|
getDictData(code: string) {
|
|
|
|
|
const dict = this.dictData.find(item => item.code === code )
|
2024-11-05 11:23:38 +08:00
|
|
|
return dict?.children || [];
|
2024-10-15 15:37:50 +08:00
|
|
|
},
|
|
|
|
|
// 初始化获取全部字典数据并缓存
|
2024-11-05 11:23:38 +08:00
|
|
|
async initDictData() {
|
|
|
|
|
await fetchDictData();
|
|
|
|
|
},
|
2024-10-15 15:37:50 +08:00
|
|
|
},
|
|
|
|
|
persist: piniaPersistConfig(DICT_STORE_KEY),
|
|
|
|
|
})
|