增加获取场馆、字典
This commit is contained in:
79
src/store/dictionaryStore.ts
Normal file
79
src/store/dictionaryStore.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { create } from 'zustand'
|
||||
import commonApi from '../services/commonApi'
|
||||
|
||||
// 字典数据接口
|
||||
export interface DictionaryData {
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
// 字典Store状态接口
|
||||
interface DictionaryState {
|
||||
dictionaryData: DictionaryData
|
||||
isLoading: boolean
|
||||
error: string | null
|
||||
fetchDictionary: () => Promise<void>
|
||||
getDictionaryValue: (key: string, defaultValue?: any) => any
|
||||
clearDictionary: () => void
|
||||
}
|
||||
|
||||
// 创建字典Store
|
||||
export const useDictionaryStore = create<DictionaryState>()((set, get) => ({
|
||||
// 初始状态
|
||||
dictionaryData: {},
|
||||
isLoading: false,
|
||||
error: null,
|
||||
|
||||
// 获取字典数据
|
||||
fetchDictionary: async () => {
|
||||
set({ isLoading: true, error: null })
|
||||
|
||||
try {
|
||||
const keys = 'publishing_requirements,court_type,court_surface,supplementary_information,game_play';
|
||||
const response = await commonApi.getDictionaryManyKey(keys)
|
||||
|
||||
if (response.code === 0 && response.data) {
|
||||
const dictionaryData = {};
|
||||
keys.split(',').forEach(key => {
|
||||
const list = response.data[key];
|
||||
const listData = list.split('|');
|
||||
dictionaryData[key] = listData;
|
||||
})
|
||||
set({
|
||||
dictionaryData: dictionaryData || {},
|
||||
isLoading: false
|
||||
})
|
||||
console.log('字典数据获取成功:', response.data)
|
||||
} else {
|
||||
throw new Error(response.message || '获取字典数据失败')
|
||||
}
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : '获取字典数据失败'
|
||||
set({
|
||||
error: errorMessage,
|
||||
isLoading: false
|
||||
})
|
||||
console.error('获取字典数据失败:', error)
|
||||
}
|
||||
},
|
||||
|
||||
// 获取字典值
|
||||
getDictionaryValue: (key: string, defaultValue?: any) => {
|
||||
const { dictionaryData } = get()
|
||||
return dictionaryData[key] !== undefined ? dictionaryData[key] : defaultValue
|
||||
},
|
||||
|
||||
// 清空字典数据
|
||||
clearDictionary: () => {
|
||||
set({ dictionaryData: {}, error: null })
|
||||
}
|
||||
}))
|
||||
|
||||
// 导出hooks
|
||||
export const useDictionaryData = () => useDictionaryStore((state) => state.dictionaryData)
|
||||
export const useDictionaryLoading = () => useDictionaryStore((state) => state.isLoading)
|
||||
export const useDictionaryError = () => useDictionaryStore((state) => state.error)
|
||||
export const useDictionaryActions = () => useDictionaryStore((state) => ({
|
||||
fetchDictionary: state.fetchDictionary,
|
||||
getDictionaryValue: state.getDictionaryValue,
|
||||
clearDictionary: state.clearDictionary
|
||||
}))
|
||||
Reference in New Issue
Block a user