From f3167f8371d19d0ea8fe7d0e7af5517ff0b08cd2 Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期三, 07 四月 2021 23:25:29 +0800
Subject: [PATCH] 2021-04-07

---
 src/api/cacheutils.js |  371 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 371 insertions(+), 0 deletions(-)

diff --git a/src/api/cacheutils.js b/src/api/cacheutils.js
new file mode 100644
index 0000000..2aa5642
--- /dev/null
+++ b/src/api/cacheutils.js
@@ -0,0 +1,371 @@
+/**
+ * @description 缂撳瓨宸ュ叿绫�
+ */
+export default class CacheUtils {
+  /**
+   * @description 鎵撳紑websql
+   */
+  static openWebSql () {
+    let service = window.GLOB.service ? '-' + window.GLOB.service.replace('/', '') : ''
+    try {
+      window.GLOB.WebSql = openDatabase(`mkdb${service}`, '1', 'mk-pc-database', 50 * 1024 * 1024)
+      window.GLOB.WebSql.transaction(tx => {
+        tx.executeSql('CREATE TABLE IF NOT EXISTS VERSIONS (version varchar(50), createDate varchar(50), CDefine1 varchar(50), CDefine2 varchar(50), CDefine3 varchar(50))', [], () => {
+        
+        }, () => {
+          // eslint-disable-next-line
+          throw 'CREATE TABLE ERROR'
+        })
+        tx.executeSql('CREATE TABLE IF NOT EXISTS CONFIGS (menuid varchar(50), userid varchar(50), openEdition varchar(50), webEdition varchar(50), LongParam text, LongParamUser text, CDefine1 varchar(50), CDefine2 varchar(50), CDefine3 varchar(50), CDefine4 varchar(50), CDefine5 varchar(50))', [], () => {
+
+        }, () => {
+          // eslint-disable-next-line
+          throw 'CREATE TABLE ERROR'
+        })
+      })
+      // window.GLOB.WebSql.transaction(tx => {
+      //   tx.executeSql('DROP TABLE VERSIONS')
+      //   tx.executeSql('DROP TABLE CONFIGS')
+      // })
+    } catch (e) {
+      console.warn('WebSql 鍒濆鍖栧け璐ワ紒')
+      window.GLOB.WebSql = null
+    }
+  }
+
+  /**
+   * @description 鑾峰彇websql涓繚瀛樹俊鎭増鏈�
+   */
+  static getWebSqlVersion () {
+    if (!window.GLOB.WebSql) {
+      return Promise.reject()
+    }
+    return new Promise((resolve, reject) => {
+      window.GLOB.WebSql.transaction(tx => {
+        tx.executeSql('SELECT * FROM VERSIONS', [], (tx, results) => {
+          if (results.rows.length === 0) {
+            tx.executeSql('DELETE FROM CONFIGS')
+            resolve({version: '', createDate: ''})
+          } else if (results.rows.length === 1) {
+            resolve(results.rows[0])
+          } else if (results.rows.length > 1) {
+            tx.executeSql('DELETE FROM VERSIONS')
+            tx.executeSql('DELETE FROM CONFIGS')
+            resolve({version: '', createDate: ''})
+          }
+        }, (tx, results) => {
+          window.GLOB.WebSql = null
+          reject()
+          console.warn(results)
+        })
+      })
+    })
+  }
+
+  /**
+   * @description 娓呯┖websql涓繚瀛樼殑閰嶇疆淇℃伅
+   */
+  static clearWebSqlConfig () {
+    if (!window.GLOB.WebSql) return
+    window.GLOB.WebSql.transaction(tx => {
+      tx.executeSql(`DELETE FROM CONFIGS`, [], () => {}, () => { window.GLOB.WebSql = null })
+    })
+  }
+
+  /**
+   * @description 鍒犻櫎websql涓繚瀛樼殑閰嶇疆淇℃伅
+   */
+  static delWebSqlConfig (keys) {
+    if (!window.GLOB.WebSql || !keys) return
+    window.GLOB.WebSql.transaction(tx => {
+      tx.executeSql(`DELETE FROM CONFIGS where menuid in (${keys})`, [], () => {}, () => {
+        window.GLOB.WebSql = null
+      })
+    })
+  }
+
+  /**
+   * @description 鍒犻櫎websql涓繚瀛樼殑閰嶇疆淇℃伅
+   */
+  static delMenuWebSqlConfig (menuId) {
+    if (!window.GLOB.WebSql || !menuId) return Promise.resolve()
+    return new Promise(resolve => {
+      window.GLOB.WebSql.transaction(tx => {
+        tx.executeSql(`DELETE FROM CONFIGS where menuid='${menuId}'`, [], () => {
+          resolve()
+        }, () => {
+          window.GLOB.WebSql = null
+          resolve()
+        })
+      })
+    })
+  }
+
+  /**
+   * @description 鏇存柊websql涓厤缃俊鎭殑淇濆瓨鏃堕棿
+   */
+  static updateWebSqlTime (curTime) {
+    if (!window.GLOB.WebSql || !curTime) return
+    window.GLOB.WebSql.transaction(tx => {
+      tx.executeSql(`UPDATE VERSIONS SET createDate='${curTime}'`, [], () => {}, () => {
+        window.GLOB.WebSql = null
+      })
+    })
+  }
+
+  /**
+   * @description 鏇存柊websql涓厤缃俊鎭殑鐗堟湰
+   */
+  static updateWebSqlversion (version, curTime) {
+    if (!window.GLOB.WebSql || !curTime || !version) return
+    window.GLOB.WebSql.transaction(tx => {
+      tx.executeSql(`UPDATE VERSIONS SET version='${version}', createDate='${curTime}'`, [], () => {}, () => {
+        window.GLOB.WebSql = null
+      })
+    })
+  }
+  
+  /**
+   * @description 鍒涘缓websql涓厤缃俊鎭殑鐗堟湰
+   */
+  static createWebSqlversion (version, curTime) {
+    if (!window.GLOB.WebSql || !curTime || !version) return
+    window.GLOB.WebSql.transaction(tx => {
+      tx.executeSql('INSERT INTO VERSIONS (version, createDate) VALUES (?, ?)', [version, curTime], () => {}, () => {
+        window.GLOB.WebSql = null
+      })
+    })
+  }
+
+  /**
+   * @description 鑾峰彇websql涓殑閰嶇疆淇℃伅
+   */
+  static getWebSqlMenuConfig (MenuID, userid) {
+    if (!window.GLOB.WebSql || !MenuID || !userid) return Promise.reject()
+    return new Promise((resolve, reject) => {
+      window.GLOB.WebSql.transaction(tx => {
+        tx.executeSql(`SELECT * FROM CONFIGS WHERE menuid='${MenuID}' and userid='${userid}'`, [], (tx, results) => {
+          let paramItem = results.rows[0]
+          if (paramItem) {
+            resolve({
+              ErrCode: 'S',
+              ErrMesg: '',
+              LongParam: paramItem.LongParam,
+              LongParamUser: paramItem.LongParamUser,
+              message: '',
+              open_edition: paramItem.openEdition,
+              status: true,
+              web_edition: paramItem.webEdition
+            })
+          } else {
+            reject()
+          }
+        }, (tx, results) => {
+          window.GLOB.WebSql = null
+          console.warn(results)
+          reject()
+        })
+      })
+    })
+  }
+
+  /**
+   * @description 灏嗘暟鎹啓鍏ebsql
+   */
+  static writeInWebSql (data) {
+    if (!window.GLOB.WebSql || !data) return
+    window.GLOB.WebSql.transaction(tx => {
+      tx.executeSql('INSERT INTO CONFIGS (menuid, userid, openEdition, webEdition, LongParam, LongParamUser) VALUES (?, ?, ?, ?, ?, ?)', data)
+    })
+  }
+
+  /**
+   * @description 鎵撳紑IndexedDB
+   */
+  static openIndexDB () {
+    let service = window.GLOB.service ? '-' + window.GLOB.service.replace('/', '') : ''
+    try {
+      let request = window.indexedDB.open(`mkdb${service}`, 1)
+      request.onerror = () => {
+        console.warn('IndexedDB 鍒濆鍖栧け璐ワ紒')
+      }
+      request.onsuccess = () => {
+        window.GLOB.IndexDB = request.result
+      }
+      request.onupgradeneeded = (event) => {
+        window.GLOB.IndexDB = event.target.result
+        if (!window.GLOB.IndexDB.objectStoreNames.contains('version')) {
+          window.GLOB.IndexDB.createObjectStore('version', { keyPath: 'id' })
+        }
+        if (!window.GLOB.IndexDB.objectStoreNames.contains('configs')) {
+          let objectStore = window.GLOB.IndexDB.createObjectStore('configs', { keyPath: 'id' })
+          objectStore.createIndex('menuid', 'menuid', { unique: false })
+          objectStore.createIndex('userid', 'userid', { unique: false })
+        }
+      }
+    } catch (e) {
+      console.warn('IndexedDB 鍒濆鍖栧け璐ワ紒')
+      window.GLOB.IndexDB = null
+    }
+  }
+
+  /**
+   * @description 鑾峰彇IndexedDB涓繚瀛樹俊鎭増鏈�
+   */
+  static getIndexDBVersion () {
+    if (!window.GLOB.IndexDB) {
+      return Promise.reject()
+    }
+    return new Promise((resolve, reject) => {
+      let request = window.GLOB.IndexDB.transaction(['version'])
+        .objectStore('version')
+        .get('mksoft')
+
+      request.onerror = (event) => {
+        window.GLOB.IndexDB = null
+        console.warn(event)
+        reject()
+      }
+
+      request.onsuccess = () => {
+        if (request.result) {
+          resolve(request.result)
+        } else {
+          this.clearIndexDBConfig()
+          resolve({version: '', createDate: ''})
+        }
+      }
+    })
+  }
+
+  /**
+   * @description 娓呯┖IndexedDB涓繚瀛樼殑閰嶇疆淇℃伅
+   */
+  static clearIndexDBConfig () {
+    if (!window.GLOB.IndexDB) return
+    let request = window.GLOB.IndexDB.transaction(['configs'], 'readwrite').objectStore('configs').clear()
+
+    request.onerror = () => {
+      window.GLOB.IndexDB = null
+    }
+  }
+
+  /**
+   * @description 鏇存柊IndexedDB涓厤缃俊鎭殑鐗堟湰
+   */
+  static updateIndexDBversion (version) {
+    if (!window.GLOB.IndexDB || !version) return
+
+    version.id = 'mksoft'
+
+    let objectStore = window.GLOB.IndexDB.transaction(['version'], 'readwrite').objectStore('version')
+    let request = objectStore.get('mksoft')
+
+    request.onerror = () => {
+      window.GLOB.IndexDB = null
+    }
+
+    request.onsuccess = () => {
+      if (request.result) {
+        let put = objectStore.put(version)
+
+        put.onerror = () => {
+          window.GLOB.IndexDB = null
+        }
+      } else {
+        this.clearIndexDBConfig()
+
+        let add = objectStore.add(version)
+
+        add.onerror = () => {
+          window.GLOB.IndexDB = null
+        }
+      }
+    }
+  }
+
+  /**
+   * @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 灏嗘暟鎹啓鍏ndexedDB
+   */
+  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
+    }
+  }
+}
\ No newline at end of file

--
Gitblit v1.8.0