根据日期查询的报表内容缓存到Local Storage

let CacheReportData = {
  /**
   * 获取缓存
   * @param obj 调用该方法的对象
   * @param name 缓存唯一标识符
   * @param startDate 查询的日期条件
   * @param pagingData 分页参数
   * @returns {*} 返回选中日期的数据
   */
  getCacheData: function(obj, name, startDate, pagingData) {
    let localStorageCache = window.localStorage;
    let localStorageCacheObj = localStorageCache.getItem(name);
    try {
      if (localStorageCacheObj.indexOf('obj-') === 0) {
        localStorageCacheObj = localStorageCacheObj.slice(4);
        for (const cache of JSON.parse(localStorageCacheObj)) {
          //如果查询日期和页码一直就调用浏览器本地缓存的数据
          if (cache.key === startDate && cache._current === pagingData._current) {
            pagingData._current = cache._current;
            pagingData._pageSize = cache._pageSize;
            pagingData._total = cache._total;
            return cache.values;
          }
        }
      }
    } catch (e) {}
  },

  /**
   * 设置缓存
   * @param obj 调用该方法的对象
   * @param name 标识符
   * @param startDate 查询的日期条件
   * @param list 数据集合
   * @param paging 分页对象
   */
  setCacheData: function(obj, name, startDate, list, paging) {
    let cacheDatas = {};
    cacheDatas.key = startDate;
    cacheDatas.values = list;
    cacheDatas._current = paging.current;
    cacheDatas._pageSize = paging.pageSize;
    cacheDatas._total = paging.total;
    obj.state.cacheData.push(cacheDatas);

    let v = JSON.stringify(obj.state.cacheData);
    v = 'obj-' + v;
    let localStorage = window.localStorage;
    localStorage.setItem(name, v);
  },
};

export default CacheReportData;

你可能感兴趣的:(根据日期查询的报表内容缓存到Local Storage)