king
2021-06-02 e543372cc70a19ff2630c79d8421c2c593e54e5f
src/api/cacheutils.js
@@ -138,7 +138,7 @@
  }
  /**
   * @description 创建websql中配置信息的保存时间
   * @description 获取websql中的配置信息
   */
  static getWebSqlMenuConfig (MenuID, userid) {
    if (!window.GLOB.WebSql || !MenuID || !userid) return Promise.reject()
@@ -199,7 +199,6 @@
        }
        if (!window.GLOB.IndexDB.objectStoreNames.contains('configs')) {
          let objectStore = window.GLOB.IndexDB.createObjectStore('configs', { keyPath: 'id' })
          objectStore.createIndex('type', 'type', { unique: false })
          objectStore.createIndex('menuid', 'menuid', { unique: false })
          objectStore.createIndex('userid', 'userid', { unique: false })
        }
@@ -285,4 +284,88 @@
    }
  }
  /**
   * @description 删除IndexedDB中保存的配置信息
   */
  static delMenuIndexDBConfig (key) {
    if (!window.GLOB.IndexDB || !key) return Promise.resolve()
    return new Promise(resolve => {
      let request = window.GLOB.IndexDB.transaction(['configs'], 'readwrite')
        .objectStore('configs')
        .delete(key)
      request.onsuccess = () => {
        resolve()
      }
      request.onerror = () => {
        window.GLOB.IndexDB = null
        resolve()
      }
    })
  }
  /**
   * @description 删除IndexedDB中保存的配置信息-批量
   */
  static delIndexDBConfig (keys) {
    if (!window.GLOB.IndexDB || !keys) return
    let objectStore = window.GLOB.IndexDB.transaction(['configs'], 'readwrite').objectStore('configs')
    objectStore.openCursor().onsuccess = (event) => {
      let cursor = event.target.result
      if (cursor) {
        if (cursor.value && keys.includes(cursor.value.menuid)) {
          let request = objectStore.delete(cursor.key)
          request.onerror = () => {
            window.GLOB.IndexDB = null
          }
        }
        cursor.continue()
      }
    }
  }
  /**
   * @description 获取IndexedDB中的配置信息
   */
  static getIndexDBMenuConfig (MenuID, userid) {
    if (!window.GLOB.IndexDB || !MenuID || !userid) return Promise.reject()
    let key = MenuID + userid
    return new Promise((resolve, reject) => {
      let request = window.GLOB.IndexDB.transaction(['configs']).objectStore('configs').get(key)
      request.onerror = () => {
        window.GLOB.IndexDB = null
        reject()
      }
      request.onsuccess = () => {
        if (request.result) {
          resolve(request.result)
        } else {
          reject()
        }
      }
    })
  }
  /**
   * @description 将数据写入IndexedDB
   */
  static writeInIndexDB (data) {
    if (!window.GLOB.IndexDB || !data) return
    let request = window.GLOB.IndexDB.transaction(['configs'], 'readwrite')
      .objectStore('configs')
      .add(data)
    request.onerror = () => {
      window.GLOB.IndexDB = null
    }
  }
}