OfficeSystem/store/dict.js
2025-11-26 15:13:25 +08:00

221 lines
6.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { defineStore } from 'pinia'
import { getDictDataList } from '@/api/common'
/**
* 字典数据 Store
* 用于管理数据字典,提供字典数据的存储和映射功能
*/
export const useDictStore = defineStore('dict', {
state: () => {
return {
// 所有字典数据列表
dictList: [],
// 按 dictType 分组的字典数据,格式:{ dictType: [dictItems] }
dictMap: {},
// 字典数据是否已加载
isLoaded: false,
// 加载时间戳,用于判断是否需要刷新
loadTime: null
}
},
getters: {
/**
* 根据字典类型获取字典列表
* @param {string} dictType 字典类型
* @returns {Array} 字典项列表
*/
getDictByType: (state) => (dictType) => {
return state.dictMap[dictType] || []
},
/**
* 根据字典类型和值获取字典标签
* @param {string} dictType 字典类型
* @param {string|number} dictValue 字典值
* @returns {string} 字典标签,如果未找到返回原值
*/
getDictLabel: (state) => (dictType, dictValue) => {
const dictItems = state.dictMap[dictType] || []
const item = dictItems.find(item => String(item.dictValue) === String(dictValue))
return item ? item.dictLabel : dictValue
},
/**
* 根据字典类型和值获取完整的字典项
* @param {string} dictType 字典类型
* @param {string|number} dictValue 字典值
* @returns {Object|null} 字典项对象,如果未找到返回 null
*/
getDictItem: (state) => (dictType, dictValue) => {
const dictItems = state.dictMap[dictType] || []
return dictItems.find(item => String(item.dictValue) === String(dictValue)) || null
},
/**
* 批量获取字典标签
* @param {string} dictType 字典类型
* @param {Array<string|number>} dictValues 字典值数组
* @returns {Array<string>} 字典标签数组
*/
getDictLabels: (state) => (dictType, dictValues) => {
if (!Array.isArray(dictValues)) {
return []
}
const dictItems = state.dictMap[dictType] || []
return dictValues.map(value => {
const item = dictItems.find(item => String(item.dictValue) === String(value))
return item ? item.dictLabel : value
})
}
},
actions: {
/**
* 加载字典数据
* @param {boolean} forceRefresh 是否强制刷新(默认 false
* @returns {Promise} 返回加载结果
*/
async loadDictData(forceRefresh = false) {
// 如果已加载且不强制刷新,直接返回
if (this.isLoaded && !forceRefresh) {
return Promise.resolve(this.dictList)
}
try {
const response = await getDictDataList()
// 根据响应数据结构处理
// 如果响应直接是数组,使用 response
// 如果响应有 rows 字段,使用 response.rows
const dictList = Array.isArray(response) ? response : (response.rows || [])
console.log('dictList',dictList)
// 过滤掉 status 不为 '0' 的字典项(停用的字典)
const activeDictList = dictList.filter(item => item.status === '0' || item.status === 0)
this.dictList = activeDictList
this.isLoaded = true
this.loadTime = Date.now()
// 按 dictType 分组
this.dictMap = {}
activeDictList.forEach(item => {
if (!this.dictMap[item.dictType]) {
this.dictMap[item.dictType] = []
}
this.dictMap[item.dictType].push(item)
})
// 按 dictSort 排序
Object.keys(this.dictMap).forEach(dictType => {
this.dictMap[dictType].sort((a, b) => {
const sortA = parseInt(a.dictSort) || 0
const sortB = parseInt(b.dictSort) || 0
return sortA - sortB
})
})
// 保存到本地存储
try {
uni.setStorageSync('dictData', {
dictList: this.dictList,
dictMap: this.dictMap,
loadTime: this.loadTime
})
} catch (error) {
console.warn('保存字典数据到本地存储失败:', error)
}
return this.dictList
} catch (error) {
console.error('加载字典数据失败:', error)
// 如果加载失败,尝试从本地存储恢复
try {
const cached = uni.getStorageSync('dictData')
if (cached && cached.dictList) {
this.dictList = cached.dictList
this.dictMap = cached.dictMap
this.loadTime = cached.loadTime
this.isLoaded = true
console.log('从本地存储恢复字典数据')
return this.dictList
}
} catch (e) {
console.error('从本地存储恢复字典数据失败:', e)
}
throw error
}
},
/**
* 加载特定类型的字典数据
* @param {string} dictType 字典类型
* @returns {Promise} 返回该类型的字典列表
*/
async loadDictByType(dictType) {
try {
const response = await getDictDataList({ dictType })
const dictList = Array.isArray(response) ? response : (response.rows || [])
const activeDictList = dictList.filter(item => item.status === '0' || item.status === 0)
// 更新该类型的字典数据
if (!this.dictMap[dictType]) {
this.dictMap[dictType] = []
}
this.dictMap[dictType] = activeDictList.sort((a, b) => {
const sortA = parseInt(a.dictSort) || 0
const sortB = parseInt(b.dictSort) || 0
return sortA - sortB
})
// 更新主列表
const existingIndex = this.dictList.findIndex(item => item.dictType === dictType)
if (existingIndex >= 0) {
// 移除旧的该类型数据
this.dictList = this.dictList.filter(item => item.dictType !== dictType)
}
this.dictList.push(...activeDictList)
return this.dictMap[dictType]
} catch (error) {
console.error(`加载字典类型 ${dictType} 失败:`, error)
throw error
}
},
/**
* 初始化字典数据(从本地存储恢复)
*/
initFromCache() {
try {
const cached = uni.getStorageSync('dictData')
if (cached && cached.dictList) {
this.dictList = cached.dictList
this.dictMap = cached.dictMap
this.loadTime = cached.loadTime
this.isLoaded = true
}
} catch (error) {
console.warn('从本地存储初始化字典数据失败:', error)
}
},
/**
* 清除字典数据
*/
clearDictData() {
this.dictList = []
this.dictMap = {}
this.isLoaded = false
this.loadTime = null
try {
uni.removeStorageSync('dictData')
} catch (error) {
console.warn('清除本地存储的字典数据失败:', error)
}
}
}
})