From da34633b25d16359cd91a656acad5e811f9972b7 Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期二, 14 三月 2023 18:09:54 +0800
Subject: [PATCH] 2023-03-14

---
 src/api/cacheutils.js |  139 +++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 123 insertions(+), 16 deletions(-)

diff --git a/src/api/cacheutils.js b/src/api/cacheutils.js
index ecf5475..1c174d9 100644
--- a/src/api/cacheutils.js
+++ b/src/api/cacheutils.js
@@ -22,6 +22,13 @@
           throw 'CREATE TABLE ERROR'
         })
 
+        tx.executeSql('CREATE TABLE IF NOT EXISTS CACHES (menuid varchar(50), CreateDate varchar(50), LongParam text, CDefine1 varchar(50), CDefine2 varchar(50))', [], () => {
+
+        }, () => {
+          // eslint-disable-next-line
+          throw 'CREATE TABLE ERROR'
+        })
+
         if (window.GLOB.systemType === '') {
           tx.executeSql('CREATE TABLE IF NOT EXISTS FUNCS (func_code varchar(50), key_sql text, CDefine1 varchar(50), CDefine2 varchar(50), CDefine3 varchar(50))', [], () => {
 
@@ -38,6 +45,10 @@
     } catch (e) {
       console.warn('WebSql 鍒濆鍖栧け璐ワ紒')
       window.GLOB.WebSql = null
+
+      if (window.indexedDB) {
+        this.openIndexDB(db)
+      }
     }
   }
 
@@ -81,7 +92,6 @@
               resolve({version: '', createDate: ''})
             }
           }, (tx, results) => {
-            window.GLOB.WebSql = null
             console.warn(results)
             resolve({version: '', createDate: ''})
           })
@@ -99,7 +109,6 @@
             }
             resolve(menus)
           }, (tx, results) => {
-            window.GLOB.WebSql = null
             console.warn(results)
             resolve([])
           })
@@ -130,12 +139,14 @@
 
     if (!keys) {
       window.GLOB.WebSql.transaction(tx => {
-        tx.executeSql(`DELETE FROM CONFIGS`, [], () => {}, () => { window.GLOB.WebSql = null })
+        tx.executeSql(`DELETE FROM CONFIGS`, [], () => {}, (tx, results) => {
+          console.warn(results)
+        })
       })
     } else {
       window.GLOB.WebSql.transaction(tx => {
-        tx.executeSql(`DELETE FROM CONFIGS where menuid in (${keys})`, [], () => {}, () => {
-          window.GLOB.WebSql = null
+        tx.executeSql(`DELETE FROM CONFIGS where menuid in (${keys})`, [], () => {}, (tx, results) => {
+          console.warn(results)
         })
       })
     }
@@ -150,8 +161,8 @@
       window.GLOB.WebSql.transaction(tx => {
         tx.executeSql(`DELETE FROM CONFIGS where menuid='${menuId}'`, [], () => {
           resolve()
-        }, () => {
-          window.GLOB.WebSql = null
+        }, (tx, results) => {
+          console.warn(results)
           resolve()
         })
       })
@@ -168,8 +179,8 @@
       tx.executeSql(`DELETE FROM VERSIONS where CDefine1='LongParam'`)
 
       if (version) {
-        tx.executeSql('INSERT INTO VERSIONS (version, createDate, CDefine1) VALUES (?, ?, ?)', [version, curTime, 'LongParam'], () => {}, () => {
-          window.GLOB.WebSql = null
+        tx.executeSql('INSERT INTO VERSIONS (version, createDate, CDefine1) VALUES (?, ?, ?)', [version, curTime, 'LongParam'], () => {}, (tx, results) => {
+          console.warn(results)
         })
       }
     })
@@ -199,7 +210,6 @@
             reject()
           }
         }, (tx, results) => {
-          window.GLOB.WebSql = null
           console.warn(results)
           reject()
         })
@@ -218,11 +228,55 @@
   }
 
   /**
+   * @description 灏嗙紦瀛樻暟鎹啓鍏ebsql
+   */
+  static writeCacheInWebSql (data) {
+    if (!window.GLOB.WebSql) return
+    window.GLOB.WebSql.transaction(tx => {
+      tx.executeSql(`DELETE FROM CACHES where menuid='${data[0]}'`)
+      if (data[2]) {
+        tx.executeSql('INSERT INTO CACHES (menuid, CreateDate, LongParam) VALUES (?, ?, ?)', data)
+      }
+    })
+  }
+
+  /**
+   * @description 鑾峰彇websql涓殑閰嶇疆淇℃伅
+   */
+  static getWebSqlCacheConfig (MenuID) {
+    if (!window.GLOB.WebSql) return Promise.resolve()
+    return new Promise((resolve, reject) => {
+      window.GLOB.WebSql.transaction(tx => {
+        tx.executeSql(`SELECT * FROM CACHES WHERE menuid='${MenuID}'`, [], (tx, results) => {
+          resolve(results.rows[0])
+        }, (tx, results) => {
+          console.warn(results)
+          resolve()
+        })
+      })
+    })
+  }
+
+  /**
+   * @description 鍒犻櫎websql涓秴杩�7澶╃殑缂撳瓨淇℃伅
+   */
+  static delWebSqlCacheConfig (date, type) {
+    if (!window.GLOB.WebSql) return
+    window.GLOB.WebSql.transaction(tx => {
+      if (type === 'all') {
+        tx.executeSql('DELETE FROM CACHES')
+      } else {
+        tx.executeSql(`DELETE FROM CACHES where CreateDate<'${date}'`)
+      }
+    })
+  }
+
+  /**
    * @description 鎵撳紑IndexedDB
    */
   static openIndexDB (db) {
     try {
-      let request = window.indexedDB.open(db, 1)
+      let request = window.indexedDB.open(db, 2)
       request.onerror = () => {
         console.warn('IndexedDB 鍒濆鍖栧け璐ワ紒')
       }
@@ -238,6 +292,9 @@
           let objectStore = window.GLOB.IndexDB.createObjectStore('configs', { keyPath: 'id' })
           objectStore.createIndex('menuid', 'menuid', { unique: false })
           objectStore.createIndex('userid', 'userid', { unique: false })
+        }
+        if (!window.GLOB.IndexDB.objectStoreNames.contains('caches')) {
+          window.GLOB.IndexDB.createObjectStore('caches', { keyPath: 'menuid' })
         }
         if (window.GLOB.systemType === '' && !window.GLOB.IndexDB.objectStoreNames.contains('funcs')) {
           window.GLOB.IndexDB.createObjectStore('funcs', { keyPath: 'id' })
@@ -441,12 +498,62 @@
   static writeInIndexDB (data) {
     if (!window.GLOB.IndexDB || !data) return
 
-    let request = window.GLOB.IndexDB.transaction(['configs'], 'readwrite')
-      .objectStore('configs')
-      .add(data)
+    window.GLOB.IndexDB.transaction(['configs'], 'readwrite').objectStore('configs').add(data)
+  }
 
-    request.onerror = () => {
-      window.GLOB.IndexDB = null
+  /**
+   * @description 灏嗘暟鎹啓鍏ndexedDB
+   */
+  static writeCacheInIndexDB (data) {
+    if (!window.GLOB.IndexDB) return
+
+    let objectStore = window.GLOB.IndexDB.transaction(['caches'], 'readwrite').objectStore('caches')
+    objectStore.delete(data.menuid)
+
+    if (data.LongParam) {
+      objectStore.add(data)
+    }
+  }
+
+  /**
+   * @description 鑾峰彇IndexedDB涓殑閰嶇疆淇℃伅
+   */
+  static getIndexDBCacheConfig (MenuID) {
+    if (!window.GLOB.IndexDB) return Promise.resolve()
+    
+    return new Promise((resolve, reject) => {
+      let request = window.GLOB.IndexDB.transaction(['caches']).objectStore('caches').get(MenuID)
+
+      request.onerror = () => {
+        resolve()
+      }
+
+      request.onsuccess = () => {
+        resolve(request.result)
+      }
+    })
+  }
+
+  /**
+   * @description 鍒犻櫎IndexedDB涓秴杩�7澶╃殑缂撳瓨淇℃伅
+   */
+  static delIndexDBCacheConfig (date, type) {
+    if (!window.GLOB.IndexDB) return
+
+    if (type === 'all') {
+      window.GLOB.IndexDB.transaction(['caches'], 'readwrite').objectStore('caches').clear()
+    } else {
+      let request = window.GLOB.IndexDB.transaction(['caches'], 'readwrite').objectStore('caches').openCursor()
+  
+      request.onsuccess = (e) => {
+        let cursor = e.target.result
+        if (cursor) {
+          if (cursor.value.CreateDate < date) {
+            cursor.delete()
+          }
+          cursor.continue()
+        }
+      }
     }
   }
 }
\ No newline at end of file

--
Gitblit v1.8.0