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 |  435 +++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 297 insertions(+), 138 deletions(-)

diff --git a/src/api/cacheutils.js b/src/api/cacheutils.js
index 3a983a0..1c174d9 100644
--- a/src/api/cacheutils.js
+++ b/src/api/cacheutils.js
@@ -5,10 +5,9 @@
   /**
    * @description 鎵撳紑websql
    */
-  static openWebSql (sysType) {
-    let service = window.GLOB.service ? '-' + window.GLOB.service.replace('/', '') : ''
+  static openWebSql (db) {
     try {
-      window.GLOB.WebSql = openDatabase(`mkdb${service}`, '1', 'mk-pc-database', 50 * 1024 * 1024)
+      window.GLOB.WebSql = openDatabase(db, '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))', [], () => {
         
@@ -23,7 +22,14 @@
           throw 'CREATE TABLE ERROR'
         })
 
-        if (sysType === 'local' && window.GLOB.systemType === '') {
+        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))', [], () => {
 
           }, () => {
@@ -39,14 +45,19 @@
     } catch (e) {
       console.warn('WebSql 鍒濆鍖栧け璐ワ紒')
       window.GLOB.WebSql = null
+
+      if (window.indexedDB) {
+        this.openIndexDB(db)
+      }
     }
   }
 
   /**
    * @description 娓呯┖鍑芥暟
    */
-  static clearFuncs (sysType) {
-    if (sysType !== 'local' || window.GLOB.systemType !== '') return
+  static clearFuncs () {
+    if (window.GLOB.systemType !== '') return
+
     if (window.GLOB.WebSql) {
       window.GLOB.WebSql.transaction(tx => {
         tx.executeSql('DELETE FROM FUNCS')
@@ -57,8 +68,7 @@
       let objectStore = window.GLOB.IndexDB.transaction(['funcs'], 'readwrite').objectStore('funcs')
       objectStore.clear()
 
-      let funcStore = window.GLOB.IndexDB.transaction(['version'], 'readwrite').objectStore('version')
-      funcStore.put({id: 'funcs', version: '1.0', createDate: '1970-01-01 14:59:09.000'})
+      window.GLOB.IndexDB.transaction(['version'], 'readwrite').objectStore('version').delete('funcs')
     }
   }
 
@@ -69,32 +79,55 @@
     if (!window.GLOB.WebSql) {
       return Promise.reject()
     }
-    return new Promise((resolve, reject) => {
-      window.GLOB.WebSql.transaction(tx => {
-        tx.executeSql("SELECT * FROM VERSIONS where CDefine1='LongParam'", [], (tx, results) => {
-          if (results.rows.length === 0) {
-            tx.executeSql('DELETE FROM VERSIONS')
-            tx.executeSql('DELETE FROM CONFIGS')
+
+    let deffers = []
+
+    deffers.push(
+      new Promise((resolve) => {
+        window.GLOB.WebSql.transaction(tx => {
+          tx.executeSql("SELECT * FROM VERSIONS where CDefine1='LongParam'", [], (tx, results) => {
+            if (results.rows[0]) {
+              resolve(results.rows[0])
+            } else {
+              resolve({version: '', createDate: ''})
+            }
+          }, (tx, results) => {
+            console.warn(results)
             resolve({version: '', createDate: ''})
-          } else {
-            resolve(results.rows[0])
-          }
-        }, (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 })
+    deffers.push(
+      new Promise((resolve) => {
+        window.GLOB.WebSql.transaction(tx => {
+          tx.executeSql(`SELECT * FROM CONFIGS`, [], (tx, results) => {
+            let menus = []
+            for (let i = 0; i < results.rows.length; i++) {
+              menus.push(`'${results.rows[i].menuid}','${results.rows[i].openEdition || 'mk'}'`)
+            }
+            resolve(menus)
+          }, (tx, results) => {
+            console.warn(results)
+            resolve([])
+          })
+        })
+      })
+    )
+
+    return new Promise((resolve) => {
+      Promise.all(deffers).then(res => {
+        let result = res[0]
+
+        if (result.createDate && !/^\d{4}-\d{2}-\d{2}/.test(result.createDate)) {
+          result.createDate = ''
+        }
+
+        result.menuids = res[1].join(';')
+
+        resolve(result)
+      })
     })
   }
 
@@ -102,12 +135,21 @@
    * @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
+    if (!window.GLOB.WebSql) return
+
+    if (!keys) {
+      window.GLOB.WebSql.transaction(tx => {
+        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})`, [], () => {}, (tx, results) => {
+          console.warn(results)
+        })
+      })
+    }
   }
 
   /**
@@ -119,22 +161,10 @@
       window.GLOB.WebSql.transaction(tx => {
         tx.executeSql(`DELETE FROM CONFIGS where menuid='${menuId}'`, [], () => {
           resolve()
-        }, () => {
-          window.GLOB.WebSql = null
+        }, (tx, results) => {
+          console.warn(results)
           resolve()
         })
-      })
-    })
-  }
-
-  /**
-   * @description 鏇存柊websql涓厤缃俊鎭殑淇濆瓨鏃堕棿
-   */
-  static updateWebSqlTime (curTime) {
-    if (!window.GLOB.WebSql || !curTime) return
-    window.GLOB.WebSql.transaction(tx => {
-      tx.executeSql(`UPDATE VERSIONS SET createDate='${curTime}' where CDefine1='LongParam'`, [], () => {}, () => {
-        window.GLOB.WebSql = null
       })
     })
   }
@@ -143,23 +173,16 @@
    * @description 鏇存柊websql涓厤缃俊鎭殑鐗堟湰
    */
   static updateWebSqlversion (version, curTime) {
-    if (!window.GLOB.WebSql || !curTime || !version) return
+    if (!window.GLOB.WebSql) return
+
     window.GLOB.WebSql.transaction(tx => {
-      tx.executeSql(`UPDATE VERSIONS SET version='${version}', createDate='${curTime}' where CDefine1='LongParam'`, [], () => {}, () => {
-        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, CDefine1) VALUES (?, ?, ?)', [version, curTime, 'LongParam'], () => {}, () => {
-        window.GLOB.WebSql = null
-      })
+      tx.executeSql(`DELETE FROM VERSIONS where CDefine1='LongParam'`)
+
+      if (version) {
+        tx.executeSql('INSERT INTO VERSIONS (version, createDate, CDefine1) VALUES (?, ?, ?)', [version, curTime, 'LongParam'], () => {}, (tx, results) => {
+          console.warn(results)
+        })
+      }
     })
   }
 
@@ -187,7 +210,6 @@
             reject()
           }
         }, (tx, results) => {
-          window.GLOB.WebSql = null
           console.warn(results)
           reject()
         })
@@ -206,12 +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 (sysType) {
-    let service = window.GLOB.service ? '-' + window.GLOB.service.replace('/', '') : ''
+  static openIndexDB (db) {
     try {
-      let request = window.indexedDB.open(`mkdb${service}`, 1)
+      let request = window.indexedDB.open(db, 2)
       request.onerror = () => {
         console.warn('IndexedDB 鍒濆鍖栧け璐ワ紒')
       }
@@ -228,7 +293,10 @@
           objectStore.createIndex('menuid', 'menuid', { unique: false })
           objectStore.createIndex('userid', 'userid', { unique: false })
         }
-        if (window.GLOB.systemType === '' && sysType === 'local' && !window.GLOB.IndexDB.objectStoreNames.contains('funcs')) {
+        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' })
         }
       }
@@ -245,38 +313,66 @@
     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()
-      }
+    let deffers = []
 
-      request.onsuccess = () => {
-        if (request.result) {
-          resolve(request.result)
-        } else {
-          this.clearIndexDBConfig()
+    deffers.push(
+      new Promise((resolve) => {
+        let request = window.GLOB.IndexDB.transaction(['version'])
+          .objectStore('version')
+          .get('mksoft')
+
+        request.onerror = (event) => {
+          window.GLOB.IndexDB = null
+          console.warn(event)
           resolve({version: '', createDate: ''})
         }
-      }
+
+        request.onsuccess = () => {
+          if (request.result) {
+            resolve(request.result)
+          } else {
+            resolve({version: '', createDate: ''})
+          }
+        }
+      })
+    )
+
+    deffers.push(
+      new Promise((resolve) => {
+        let request = window.GLOB.IndexDB.transaction(['configs']).objectStore('configs').openCursor()
+        let menus = []
+
+        request.onerror = () => {
+          window.GLOB.IndexDB = null
+          resolve(menus)
+        }
+
+        request.onsuccess = (e) => {
+          let cursor = e.target.result
+          if (cursor) {
+            menus.push(`'${cursor.value.menuid}','${cursor.value.open_edition || 'mk'}'`)
+            cursor.continue()
+          } else {
+            resolve(menus)
+          }
+        }
+      })
+    )
+
+    return new Promise((resolve) => {
+      Promise.all(deffers).then(res => {
+        let result = res[0]
+
+        if (result.createDate && !/^\d{4}-\d{2}-\d{2}/.test(result.createDate)) {
+          result.createDate = ''
+        }
+
+        result.menuids = res[1].join(';')
+
+        resolve(result)
+      })
     })
-  }
-
-  /**
-   * @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
-    }
   }
 
   /**
@@ -285,29 +381,35 @@
   static updateIndexDBversion (version) {
     if (!window.GLOB.IndexDB || !version) return
 
-    version.id = 'mksoft'
+    if (!version) {
+      let request = window.GLOB.IndexDB.transaction(['configs'], 'readwrite').objectStore('configs').delete('mksoft')
+  
+      request.onerror = () => {
+        window.GLOB.IndexDB = null
+      }
+    } else {
+      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
+      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 {
+          let add = objectStore.add(version)
+  
+          add.onerror = () => {
+            window.GLOB.IndexDB = null
+          }
         }
       }
     }
@@ -337,24 +439,30 @@
   /**
    * @description 鍒犻櫎IndexedDB涓繚瀛樼殑閰嶇疆淇℃伅-鎵归噺
    */
-  static delIndexDBConfig (keys) {
-    if (!window.GLOB.IndexDB || !keys) return
+  static delIndexDBConfig (menuids) {
+    if (!window.GLOB.IndexDB) return
 
-    let objectStore = window.GLOB.IndexDB.transaction(['configs'], 'readwrite').objectStore('configs')
+    if (!menuids) {
+      let request = window.GLOB.IndexDB.transaction(['configs'], 'readwrite').objectStore('configs').clear()
+  
+      request.onerror = () => {
+        window.GLOB.IndexDB = null
+      }
+    } else {
+      let request = window.GLOB.IndexDB.transaction(['configs'], 'readwrite').objectStore('configs').openCursor()
+  
+      request.onerror = () => {
+        window.GLOB.IndexDB = null
+      }
 
-    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
+      request.onsuccess = (e) => {
+        let cursor = e.target.result
+        if (cursor) {
+          if (menuids.includes(cursor.value.menuid)) {
+            cursor.delete()
           }
+          cursor.continue()
         }
-
-        cursor.continue()
       }
     }
   }
@@ -365,6 +473,7 @@
   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)
 
@@ -389,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