import axiosQuiet from './axiosQuiet.js' /** * 字典工具类 - 支持批量获取和缓存 */ class DictUtil { constructor() { this.cacheKey = 'dict_cache' this.cacheExpire = 30 * 60 * 1000 // 30分钟过期 } /** * 批量获取字典数据 * @param {Array} keys 字典key数组 * @param {Boolean} useCache 是否使用缓存 * @returns {Promise} 字典数据对象 */ async getBatchDict(keys = [], useCache = true) { if (!Array.isArray(keys) || keys.length === 0) { console.warn('字典keys参数必须是非空数组') return {} } try { // 如果使用缓存,先检查缓存 let cachedData = {} let uncachedKeys = [] 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 }) } else { uncachedKeys = [...keys] } // 如果所有数据都在缓存中,直接返回 if (uncachedKeys.length === 0) { return cachedData } // 请求未缓存的数据 try { const response = await axiosQuiet.get('/dict/batch', { keys: uncachedKeys.join(',') }) if (response && response.code === 1) { const newData = response.data || {} // 更新缓存 if (useCache) { this.updateCache(newData) } // 合并缓存数据和新数据 return { ...cachedData, ...newData } } else { console.warn('批量获取字典失败:', response?.msg || '未知错误') return cachedData // 返回已缓存的数据 } } catch (requestError) { console.warn('批量获取字典请求失败:', requestError) return cachedData // 返回已缓存的数据 } } catch (error) { console.error('批量获取字典异常:', error) return {} } } /** * 根据业务场景获取字典数据 * @param {String} scene 业务场景 * @param {Boolean} useCache 是否使用缓存 * @returns {Promise} 字典数据对象 */ async getDictByScene(scene, useCache = true) { if (!scene) { console.warn('业务场景参数不能为空') return {} } try { // 检查场景缓存 const sceneCacheKey = `scene_${scene}` if (useCache) { const cache = this.getCache() if (cache[sceneCacheKey] && this.isCacheValid(cache[sceneCacheKey])) { return cache[sceneCacheKey].data } } const response = await axiosQuiet.get(`/dict/scene/${scene}`) if (response && response.code === 1) { const data = response.data || {} // 缓存场景数据 if (useCache) { const cacheData = {} cacheData[sceneCacheKey] = { data: data.data || {}, timestamp: Date.now() } this.updateCache(cacheData) } return data.data || {} } else { console.warn('根据场景获取字典失败:', response?.msg || '未知错误') return {} } } catch (error) { console.error('根据场景获取字典异常:', error) return {} } } /** * 获取单个字典数据 * @param {String} key 字典key * @param {Boolean} useCache 是否使用缓存 * @returns {Promise} 字典数据数组 */ async getDict(key, useCache = true) { if (!key) { console.warn('字典key不能为空') return [] } const result = await this.getBatchDict([key], useCache) return result[key] || [] } /** * 获取缓存数据 * @returns {Object} 缓存对象 */ getCache() { try { const cacheStr = uni.getStorageSync(this.cacheKey) return cacheStr ? JSON.parse(cacheStr) : {} } 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)) } catch (error) { console.error('更新字典缓存失败:', error) } } /** * 检查缓存是否有效 * @param {Object} cacheItem 缓存项 * @returns {Boolean} 是否有效 */ isCacheValid(cacheItem) { if (!cacheItem || !cacheItem.timestamp) { return false } return (Date.now() - cacheItem.timestamp) < this.cacheExpire } /** * 清除字典缓存 * @param {Array} keys 要清除的字典key数组,为空则清除所有 */ clearCache(keys = null) { try { if (keys === null) { // 清除所有缓存 uni.removeStorageSync(this.cacheKey) } else if (Array.isArray(keys)) { // 清除指定keys的缓存 const cache = this.getCache() keys.forEach(key => { delete cache[key] }) uni.setStorageSync(this.cacheKey, JSON.stringify(cache)) } } catch (error) { console.error('清除字典缓存失败:', error) } } /** * 获取字典映射关系 * @returns {Promise} 映射关系对象 */ async getDictMapping() { try { const response = await axiosQuiet.get('/dict/mapping') if (response && response.code === 1) { return response.data || {} } else { console.warn('获取字典映射失败:', response?.msg || '未知错误') return {} } } catch (error) { console.error('获取字典映射异常:', error) return {} } } /** * 将字典数据转换为选择器格式 * @param {Array} dictData 字典数据 * @returns {Array} 选择器格式数据 */ formatForPicker(dictData) { if (!Array.isArray(dictData)) { return [] } return dictData.map(item => ({ text: item.name || item.text || '', value: item.value || '', sort: item.sort || 0, memo: item.memo || '' })) } /** * 根据value查找字典项的名称 * @param {Array} dictData 字典数据 * @param {String} value 要查找的值 * @returns {String} 对应的名称 */ getNameByValue(dictData, value) { if (!Array.isArray(dictData)) { return '' } const item = dictData.find(item => String(item.value) === String(value)) return item ? (item.name || item.text || '') : '' } /** * 预加载常用字典数据 * @param {Array} keys 要预加载的字典keys * @returns {Promise} */ async preloadDict(keys = []) { if (!Array.isArray(keys) || keys.length === 0) { return } try { await this.getBatchDict(keys, true) console.log('字典预加载完成:', keys) } catch (error) { console.error('字典预加载失败:', error) } } /** * 获取客户添加页面需要的字典数据 * @returns {Promise} 字典数据对象 */ async getCustomerAddDict() { const keys = [ 'SourceChannel', 'source', 'customer_purchasing_power', 'cognitive_concept', 'decision_maker', 'preliminarycustomerintention', 'kh_status', 'distance' ] return await this.getBatchDict(keys) } } // 创建单例实例 const dictUtil = new DictUtil() // 扩展 util 对象的字典方法(兼容原有代码) if (typeof util !== 'undefined') { // 保持原有的 getDict 方法兼容性 const originalGetDict = util.getDict util.getDict = async function(key) { try { // 优先使用新的字典工具 const result = await dictUtil.getDict(key) if (result && result.length > 0) { return result } // 如果新工具没有数据,回退到原方法 if (originalGetDict && typeof originalGetDict === 'function') { return await originalGetDict.call(this, key) } return [] } catch (error) { console.error('获取字典失败:', error) return [] } } // 添加新的批量获取方法 util.getBatchDict = dictUtil.getBatchDict.bind(dictUtil) util.getDictByScene = dictUtil.getDictByScene.bind(dictUtil) util.clearDictCache = dictUtil.clearCache.bind(dictUtil) util.preloadDict = dictUtil.preloadDict.bind(dictUtil) util.getCustomerAddDict = dictUtil.getCustomerAddDict.bind(dictUtil) } export default dictUtil