105 lines
3.4 KiB
TypeScript
105 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
|
||
}
|
||
|
||
// 创建字典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,supported_cities,show_home_qrcode,bannerListImage,bannerDetailImage,bannerListIndex';
|
||
const response = await commonApi.getDictionaryManyKey(keys)
|
||
|
||
if (response.code === 0 && response.data) {
|
||
const dictionaryData: DictionaryData = {};
|
||
keys.split(',').forEach(key => {
|
||
const raw = response.data[key];
|
||
// 单值配置:首页是否展示二维码(1/0 或 true/false)
|
||
if (key === 'show_home_qrcode') {
|
||
dictionaryData[key] = raw === '1' || raw === 1 || raw === true;
|
||
return;
|
||
}
|
||
// supported_cities 格式如 "上海市||北京市",用 || 分割
|
||
const listData = key === 'supported_cities'
|
||
? (raw ? String(raw).split('||').map((s) => s.trim()).filter(Boolean) : [])
|
||
: (raw ? String(raw).split('|') : []);
|
||
dictionaryData[key] = listData;
|
||
})
|
||
set({
|
||
dictionaryData: dictionaryData || {},
|
||
isLoading: false
|
||
})
|
||
|
||
set({
|
||
bannerDict: {
|
||
bannerListImage: response.data.bannerListImage || '',
|
||
bannerDetailImage: response.data.bannerDetailImage || '',
|
||
bannerListIndex: (response.data.bannerListIndex ?? '').toString(),
|
||
}
|
||
})
|
||
|
||
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.warn('获取字典数据失败:', 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
|
||
})) |