智慧教务系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

187 lines
4.8 KiB

import axiosQuiet from './axiosQuiet.js'
/**
* 简化版字典工具类 - 用于调试和测试
*/
class DictUtilSimple {
constructor() {
this.cacheKey = 'dict_cache_simple'
this.cacheExpire = 30 * 60 * 1000 // 30分钟过期
}
/**
* 批量获取字典数据
* @param {Array} keys 字典key数组
* @param {Boolean} useCache 是否使用缓存
* @returns {Promise<Object>} 字典数据对象
*/
async getBatchDict(keys = [], useCache = true) {
console.log('getBatchDict 调用参数:', { keys, useCache })
if (!Array.isArray(keys) || keys.length === 0) {
console.warn('字典keys参数必须是非空数组')
return {}
}
try {
// 检查缓存
let cachedData = {}
let uncachedKeys = [...keys]
if (useCache) {
const cache = this.getCache()
uncachedKeys = keys.filter(key => {
if (cache[key] && this.isCacheValid(cache[key])) {
cachedData[key] = cache[key].data
return false
}
return true
})
}
console.log('缓存检查结果:', { cachedData, uncachedKeys })
// 如果所有数据都在缓存中,直接返回
if (uncachedKeys.length === 0) {
console.log('所有数据来自缓存')
return cachedData
}
// 请求未缓存的数据
console.log('开始请求未缓存的数据:', uncachedKeys)
const response = await axiosQuiet.get('/dict/batch', {
keys: uncachedKeys.join(',')
})
console.log('API响应:', response)
if (response && response.code === 1) {
const newData = response.data || {}
console.log('获取到新数据:', newData)
// 更新缓存
if (useCache) {
this.updateCache(newData)
}
// 合并缓存数据和新数据
const result = { ...cachedData, ...newData }
console.log('最终结果:', result)
return result
} else {
console.warn('批量获取字典失败:', response?.msg || '未知错误')
return cachedData // 返回已缓存的数据
}
} catch (error) {
console.error('批量获取字典异常:', error)
return {}
}
}
/**
* 获取单个字典数据
* @param {String} key 字典key
* @param {Boolean} useCache 是否使用缓存
* @returns {Promise<Array>} 字典数据数组
*/
async getDict(key, useCache = true) {
console.log('getDict 调用参数:', { key, useCache })
if (!key) {
console.warn('字典key不能为空')
return []
}
try {
const result = await this.getBatchDict([key], useCache)
return result[key] || []
} catch (error) {
console.error('获取单个字典失败:', error)
return []
}
}
/**
* 获取缓存数据
* @returns {Object} 缓存对象
*/
getCache() {
try {
const cacheStr = uni.getStorageSync(this.cacheKey)
const cache = cacheStr ? JSON.parse(cacheStr) : {}
console.log('获取缓存:', cache)
return cache
} catch (error) {
console.error('获取字典缓存失败:', error)
return {}
}
}
/**
* 更新缓存
* @param {Object} data 要缓存的数据
*/
updateCache(data) {
try {
const cache = this.getCache()
const timestamp = Date.now()
// 更新缓存数据
Object.keys(data).forEach(key => {
cache[key] = {
data: data[key],
timestamp: timestamp
}
})
uni.setStorageSync(this.cacheKey, JSON.stringify(cache))
console.log('缓存已更新:', cache)
} catch (error) {
console.error('更新字典缓存失败:', error)
}
}
/**
* 检查缓存是否有效
* @param {Object} cacheItem 缓存项
* @returns {Boolean} 是否有效
*/
isCacheValid(cacheItem) {
if (!cacheItem || !cacheItem.timestamp) {
return false
}
const isValid = (Date.now() - cacheItem.timestamp) < this.cacheExpire
console.log('缓存有效性检查:', { cacheItem, isValid })
return isValid
}
/**
* 清除字典缓存
* @param {Array} keys 要清除的字典key数组,为空则清除所有
*/
clearCache(keys = null) {
try {
if (keys === null) {
// 清除所有缓存
uni.removeStorageSync(this.cacheKey)
console.log('已清除所有字典缓存')
} else if (Array.isArray(keys)) {
// 清除指定keys的缓存
const cache = this.getCache()
keys.forEach(key => {
delete cache[key]
})
uni.setStorageSync(this.cacheKey, JSON.stringify(cache))
console.log('已清除指定字典缓存:', keys)
}
} catch (error) {
console.error('清除字典缓存失败:', error)
}
}
}
// 创建单例实例
const dictUtilSimple = new DictUtilSimple()
export default dictUtilSimple