| | |
| | | */ |
| | | 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 将数据写入websql |
| | | */ |
| | | 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('/', '') : '' |
| | | static openIndexDB (db) { |
| | | try { |
| | | let request = window.indexedDB.open(`mkdb${service}`, 1) |
| | | let request = window.indexedDB.open(db, 3) |
| | | request.onerror = () => { |
| | | console.warn('IndexedDB 初始化失败!') |
| | | } |
| | |
| | | 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.IndexDB.objectStoreNames.contains('funcs')) { |
| | | window.GLOB.IndexDB.createObjectStore('funcs', { keyPath: 'id' }) |
| | | } |
| | | } |
| | | } catch (e) { |
| | | console.warn('IndexedDB 初始化失败!') |
| | |
| | | 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) => { |
| | | 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 = [] |
| | | let ids = [] |
| | | |
| | | request.onerror = () => { |
| | | resolve(menus) |
| | | } |
| | | |
| | | request.onsuccess = (e) => { |
| | | let cursor = e.target.result |
| | | if (cursor) { |
| | | if (cursor.value.menuid && !ids.includes(cursor.value.menuid)) { |
| | | menus.push(`'${cursor.value.menuid}','${cursor.value.open_edition || 'mk'}'`) |
| | | ids.push(cursor.value.menuid) |
| | | } |
| | | 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 |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | static updateIndexDBversion (version) { |
| | | if (!window.GLOB.IndexDB || !version) return |
| | | |
| | | version.id = 'mksoft' |
| | | if (!version) { |
| | | window.GLOB.IndexDB.transaction(['configs'], 'readwrite').objectStore('configs').delete('mksoft') |
| | | } 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.onsuccess = () => { |
| | | if (request.result) { |
| | | objectStore.put(version) |
| | | } else { |
| | | objectStore.add(version) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @description 删除IndexedDB中保存的配置信息 |
| | | * @description 删除IndexedDB中保存的配置信息-批量 |
| | | */ |
| | | static delMenuIndexDBConfig (key) { |
| | | if (!window.GLOB.IndexDB || !key) return Promise.resolve() |
| | | static delIndexDBConfig (menuids) { |
| | | if (!window.GLOB.IndexDB) return |
| | | |
| | | return new Promise(resolve => { |
| | | let request = window.GLOB.IndexDB.transaction(['configs'], 'readwrite') |
| | | .objectStore('configs') |
| | | .delete(key) |
| | | if (!menuids) { |
| | | window.GLOB.IndexDB.transaction(['configs'], 'readwrite').objectStore('configs').clear() |
| | | } else { |
| | | let request = window.GLOB.IndexDB.transaction(['configs'], 'readwrite').objectStore('configs').openCursor() |
| | | |
| | | request.onsuccess = () => { |
| | | resolve() |
| | | request.onsuccess = (e) => { |
| | | let cursor = e.target.result |
| | | if (cursor) { |
| | | if (menuids.includes(cursor.value.menuid)) { |
| | | cursor.delete() |
| | | } |
| | | cursor.continue() |
| | | } |
| | | } |
| | | request.onerror = () => { |
| | | window.GLOB.IndexDB = null |
| | | resolve() |
| | | } |
| | | }) |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @description 获取IndexedDB中的配置信息 |
| | | */ |
| | | static getIndexDBMenuConfig (MenuID, userid) { |
| | | if (!window.GLOB.IndexDB || !MenuID || !userid) return Promise.reject() |
| | | let key = MenuID + userid |
| | | static getIndexDBMenuConfig (key) { |
| | | if (!window.GLOB.IndexDB || !key) return Promise.reject() |
| | | |
| | | return new Promise((resolve, reject) => { |
| | | let request = window.GLOB.IndexDB.transaction(['configs']).objectStore('configs').get(key) |
| | | |
| | | request.onerror = () => { |
| | | window.GLOB.IndexDB = null |
| | | reject() |
| | | } |
| | | |
| | |
| | | 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 将数据写入IndexedDB |
| | | */ |
| | | 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, limit) { |
| | | 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 = () => { |
| | | if (limit && request.result) { |
| | | if (request.result.CreateDate > limit) { |
| | | resolve(request.result) |
| | | } else { |
| | | resolve() |
| | | } |
| | | } else { |
| | | 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.value.CreateDate.length === 10) { |
| | | cursor.delete() |
| | | } |
| | | cursor.continue() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |