From c33ac9ddcdbed91bd2267bed2a96199441806a04 Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期四, 22 九月 2022 17:00:15 +0800 Subject: [PATCH] 2022-09-22 --- src/api/cacheutils.js | 138 +++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 125 insertions(+), 13 deletions(-) diff --git a/src/api/cacheutils.js b/src/api/cacheutils.js index 409b427..3a983a0 100644 --- a/src/api/cacheutils.js +++ b/src/api/cacheutils.js @@ -5,7 +5,7 @@ /** * @description 鎵撳紑websql */ - static openWebSql () { + static openWebSql (sysType) { let service = window.GLOB.service ? '-' + window.GLOB.service.replace('/', '') : '' try { window.GLOB.WebSql = openDatabase(`mkdb${service}`, '1', 'mk-pc-database', 50 * 1024 * 1024) @@ -22,6 +22,15 @@ // eslint-disable-next-line throw 'CREATE TABLE ERROR' }) + + if (sysType === 'local' && 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))', [], () => { + + }, () => { + // eslint-disable-next-line + throw 'CREATE TABLE ERROR' + }) + } }) // window.GLOB.WebSql.transaction(tx => { // tx.executeSql('DROP TABLE VERSIONS') @@ -34,6 +43,26 @@ } /** + * @description 娓呯┖鍑芥暟 + */ + static clearFuncs (sysType) { + if (sysType !== 'local' || window.GLOB.systemType !== '') return + if (window.GLOB.WebSql) { + window.GLOB.WebSql.transaction(tx => { + tx.executeSql('DELETE FROM FUNCS') + + tx.executeSql(`UPDATE VERSIONS SET createDate='1970-01-01 14:59:09.000' where CDefine1='funcs'`) + }) + } else if (window.GLOB.IndexDB) { + 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'}) + } + } + + /** * @description 鑾峰彇websql涓繚瀛樹俊鎭増鏈� */ static getWebSqlVersion () { @@ -42,16 +71,13 @@ } return new Promise((resolve, reject) => { window.GLOB.WebSql.transaction(tx => { - tx.executeSql('SELECT * FROM VERSIONS', [], (tx, results) => { + tx.executeSql("SELECT * FROM VERSIONS where CDefine1='LongParam'", [], (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: ''}) + } else { + resolve(results.rows[0]) } }, (tx, results) => { window.GLOB.WebSql = null @@ -107,7 +133,7 @@ static updateWebSqlTime (curTime) { if (!window.GLOB.WebSql || !curTime) return window.GLOB.WebSql.transaction(tx => { - tx.executeSql(`UPDATE VERSIONS SET createDate='${curTime}'`, [], () => {}, () => { + tx.executeSql(`UPDATE VERSIONS SET createDate='${curTime}' where CDefine1='LongParam'`, [], () => {}, () => { window.GLOB.WebSql = null }) }) @@ -119,7 +145,7 @@ 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}'`, [], () => {}, () => { + tx.executeSql(`UPDATE VERSIONS SET version='${version}', createDate='${curTime}' where CDefine1='LongParam'`, [], () => {}, () => { window.GLOB.WebSql = null }) }) @@ -131,14 +157,14 @@ 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], () => {}, () => { + tx.executeSql('INSERT INTO VERSIONS (version, createDate, CDefine1) VALUES (?, ?, ?)', [version, curTime, 'LongParam'], () => {}, () => { window.GLOB.WebSql = null }) }) } /** - * @description 鍒涘缓websql涓厤缃俊鎭殑淇濆瓨鏃堕棿 + * @description 鑾峰彇websql涓殑閰嶇疆淇℃伅 */ static getWebSqlMenuConfig (MenuID, userid) { if (!window.GLOB.WebSql || !MenuID || !userid) return Promise.reject() @@ -182,7 +208,7 @@ /** * @description 鎵撳紑IndexedDB */ - static openIndexDB () { + static openIndexDB (sysType) { let service = window.GLOB.service ? '-' + window.GLOB.service.replace('/', '') : '' try { let request = window.indexedDB.open(`mkdb${service}`, 1) @@ -199,9 +225,11 @@ } 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 }) + } + if (window.GLOB.systemType === '' && sysType === 'local' && !window.GLOB.IndexDB.objectStoreNames.contains('funcs')) { + window.GLOB.IndexDB.createObjectStore('funcs', { keyPath: 'id' }) } } } catch (e) { @@ -285,4 +313,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 灏嗘暟鎹啓鍏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