import cache from '@/utils/cache'

//request拦截器

service.interceptors.request.use(config => {
  //只对部分接口数据缓存
  if ((config.url.indexOf('system/assetequipmentsort/jsoncode') > -1){
    let urls = getUrl(config)
    let value = cache.session.getJSON(urls)
    if (value){
      console.log(urls,value)
      config.adapter = ()=>{
        return new Promise((resolve, reject) => {
          resolve({
            config:config,
            headers:config.headers,
            request:config.request,
            data: value,
            status: 200,
            statusText: 'OK'
          })
        })
      }
    }
  }
})

//响应拦截器

service.interceptors.response.use(res => {
 //只对部分接口数据缓存
if ((res.config.url.indexOf('system/assetequipmentsort/jsoncode') > -1){
  if (res.data){
    let urls = getUrl(res.config)
    let value = cache.session.getJSON(urls)
    if (!value){
      cache.session.setJSON(urls,res.data)
    }
  }
}
  return res.data
})

//处理url作为key

function getUrl(config) {
  let url = config.url
  if (config.params && config.method == 'post'){
    if (url.indexOf('?') > -1) {
      url = url + '&';
    } else {
      url = url + '?';
    }
    for (const propName of Object.keys(config.params)) {
      const value = config.params[propName];
      var part = encodeURIComponent(propName) + "=";
      if (value !== null && typeof (value) !== "undefined") {
        if (typeof value === 'object') {
          for (const key of Object.keys(value)) {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + "=";
            url += subPart + encodeURIComponent(value[key]) + "&";
          }
        } else {
          url += part + encodeURIComponent(value) + "&";
        }
      }
    }
    url = url.slice(0, -1);
  }
  return url
}
//cache.js
const sessionCache = {
  set (key, value) {
    if (!sessionStorage) {
      return
    }
    if (key != null && value != null) {
      sessionStorage.setItem(key, value)
    }
  },
  get (key) {
    if (!sessionStorage) {
      return null
    }
    if (key == null) {
      return null
    }
    return sessionStorage.getItem(key)
  },
  setJSON (key, jsonValue) {
    if (jsonValue != null) {
      this.set(key, JSON.stringify(jsonValue))
    }
  },
  getJSON (key) {
    const value = this.get(key)
    if (value != null) {
      return JSON.parse(value)
    }
  },
  remove (key) {
    sessionStorage.removeItem(key);
  }
}
const localCache = {
  set (key, value) {
    if (!localStorage) {
      return
    }
    if (key != null && value != null) {
      localStorage.setItem(key, value)
    }
  },
  get (key) {
    if (!localStorage) {
      return null
    }
    if (key == null) {
      return null
    }
    return localStorage.getItem(key)
  },
  setJSON (key, jsonValue) {
    if (jsonValue != null) {
      this.set(key, JSON.stringify(jsonValue))
    }
  },
  getJSON (key) {
    const value = this.get(key)
    if (value != null) {
      return JSON.parse(value)
    }
  },
  remove (key) {
    localStorage.removeItem(key);
  }
}
export default {
  /**
   * 会话级缓存
   */
  session: sessionCache,
  /**
   * 本地缓存
   */
  local: localCache
}

sessionCache只能储存4M数据,储存大量数据需要用indexedDB

GitHub 加速计划 / vu / vue
80
16
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:4 个月前 )
73486cb5 * chore: fix link broken Signed-off-by: snoppy <michaleli@foxmail.com> * Update packages/template-compiler/README.md [skip ci] --------- Signed-off-by: snoppy <michaleli@foxmail.com> Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 6 个月前
e428d891 Updated Browser Compatibility reference. The previous currently returns HTTP 404. 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐