UNPKG

dwui

Version:

a part of wrapper on iView UI

68 lines (63 loc) 1.89 kB
/** * 浏览器本地缓存工具类 */ /** * 缓存数据到localstorage * @param cacheKey 缓存数据的key * @param cacheScope 缓存数据的scope,相当于二级key,用来对同一个cacheKey下面的数据进行二次划分隔离,避免出现当前用户读取到上一个用户的缓存数据 * @param cacheData 需要缓存的数据 */ export const setLocalStorageCacheData = (cacheKey, cacheScope, cacheData) => { if (!cacheScope) { cacheScope = 'default' } let existCacheData = localStorage[cacheKey] if (!existCacheData) { let initObject = {} initObject[cacheScope] = cacheData localStorage[cacheKey] = JSON.stringify(initObject) } else { let cacheKeyData = JSON.parse(existCacheData) cacheKeyData[cacheScope] = cacheData localStorage[cacheKey] = JSON.stringify(cacheKeyData) } } /** * 从localstorage 中获取缓存的值 * @param cacheKey 缓存的key * @param cacheScope 缓存的scope,详情看set方法中的解释 * @param defaultValue 不存在时,返回的默认值 * @return {*} */ export const getLocalStorageCacheData = (cacheKey, cacheScope, defaultValue) => { if (!cacheScope) { cacheScope = 'default' } let existCacheData = localStorage[cacheKey] if (!existCacheData) { return defaultValue } else { let cacheKeyScopeData = JSON.parse(existCacheData)[cacheScope] if (cacheKeyScopeData) { return cacheKeyScopeData } else { return defaultValue } } } /** * 设置打点的树节点 * @param openTrees * @param cacheScope * @param 缓存有效域 */ export const setOpenTreeItem = (cacheScope, openTrees) => { setLocalStorageCacheData('openTrees', cacheScope, openTrees) } /** * 返回打点的树节点信息 * @return {any} */ export const getOpenTreeItem = (cacheScope) => { return getLocalStorageCacheData('openTrees', cacheScope, []) }