108 lines
3.4 KiB
TypeScript
108 lines
3.4 KiB
TypeScript
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
|
|
// banner 字典(单独管理,保持原始值)
|
|
bannerDict: {
|
|
bannerListImage: string
|
|
bannerDetailImage: string
|
|
bannerListIndex: string
|
|
} | null
|
|
fetchBannerDictionary: () => Promise<void>
|
|
}
|
|
|
|
// 创建字典Store
|
|
export const useDictionaryStore = create<DictionaryState>()((set, get) => ({
|
|
// 初始状态
|
|
dictionaryData: {},
|
|
isLoading: false,
|
|
error: null,
|
|
bannerDict: null,
|
|
|
|
// 获取字典数据
|
|
fetchDictionary: async () => {
|
|
set({ isLoading: true, error: null })
|
|
|
|
try {
|
|
const keys = 'publishing_requirements,court_type,court_surface,supplementary_information,game_play,fabu_tip';
|
|
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)
|
|
}
|
|
},
|
|
|
|
// 获取 Banner 字典(启动时或手动调用)
|
|
fetchBannerDictionary: async () => {
|
|
try {
|
|
const keys = 'bannerListImage,bannerDetailImage,bannerListIndex';
|
|
const response = await commonApi.getDictionaryManyKey(keys)
|
|
if (response.code === 0 && response.data) {
|
|
const data = response.data || {};
|
|
set({
|
|
bannerDict: {
|
|
bannerListImage: data.bannerListImage || '',
|
|
bannerDetailImage: data.bannerDetailImage || '',
|
|
bannerListIndex: (data.bannerListIndex ?? '').toString(),
|
|
}
|
|
})
|
|
}
|
|
} catch (error) {
|
|
// 保持静默,避免影响启动流程
|
|
console.error('获取 Banner 字典失败:', 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
|
|
})) |