更新完善字典相关功能

This commit is contained in:
2026-02-18 17:15:33 +08:00
parent 5450777bd7
commit 378b9bd71f
23 changed files with 1657 additions and 572 deletions
@@ -1,147 +0,0 @@
/**
* 数据字典缓存工具
* 用于缓存和管理数据字典数据
*/
import systemApi from '@/api/system'
// 缓存存储
const cacheStorage = new Map()
// 缓存过期时间(毫秒)默认1小时
const CACHE_EXPIRE_TIME = 3600 * 1000
/**
* 字典缓存管理类
*/
class DictionaryCacheManager {
/**
* 获取所有字典(带缓存)
*/
async getAll() {
const cacheKey = 'all'
const cached = this.get(cacheKey)
if (cached) {
return cached
}
const res = await systemApi.dictionaries.all.get()
if (res.code === 200) {
const data = res.data || []
this.set(cacheKey, data)
return data
}
return []
}
/**
* 根据编码获取字典项(带缓存)
*/
async getItemsByCode(code) {
const cacheKey = `items:${code}`
const cached = this.get(cacheKey)
if (cached) {
return cached
}
const res = await systemApi.public.dictionaries.code.get({ code })
if (res.code === 200) {
const data = res.data || []
this.set(cacheKey, data)
return data
}
return []
}
/**
* 根据编码获取字典(带缓存)
*/
async getByCode(code) {
const cacheKey = `code:${code}`
const cached = this.get(cacheKey)
if (cached) {
return cached
}
const all = await this.getAll()
const dictionary = all.find((item) => item.code === code)
if (dictionary) {
this.set(cacheKey, dictionary)
}
return dictionary
}
/**
* 根据编码获取字典项的标签
*/
async getLabelByCode(code, value) {
const items = await this.getItemsByCode(code)
const item = items.find((item) => item.value === value)
return item ? item.label : value
}
/**
* 清除所有缓存
*/
clear() {
cacheStorage.clear()
}
/**
* 清除指定缓存
*/
delete(key) {
cacheStorage.delete(key)
}
/**
* 清除字典相关缓存
*/
clearDictionary(dictionaryId) {
// 清除特定字典的缓存(暂时清除所有,因为前端不知道字典ID对应的code)
this.clear()
}
/**
* 获取缓存
*/
get(key) {
const item = cacheStorage.get(key)
if (!item) {
return null
}
// 检查是否过期
if (Date.now() > item.expires) {
cacheStorage.delete(key)
return null
}
return item.data
}
/**
* 设置缓存
*/
set(key, data) {
const item = {
data,
expires: Date.now() + CACHE_EXPIRE_TIME
}
cacheStorage.set(key, item)
}
}
// 创建单例实例
const dictionaryCache = new DictionaryCacheManager()
export default dictionaryCache