king
2023-01-17 d5ce81026882ada34e5d49411be7c90ee96cc102
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/**
 * @description 缓存工具类
 */
export default class CacheUtils {
  /**
   * @description 打开websql
   */
  static openWebSql (db) {
    try {
      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))', [], () => {
        
        }, () => {
          // 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'
        })
 
        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))', [], () => {
 
          }, () => {
            // 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 清空函数
   */
  static clearFuncs () {
    if (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()
 
      window.GLOB.IndexDB.transaction(['version'], 'readwrite').objectStore('version').delete('funcs')
    }
  }
 
  /**
   * @description 获取websql中保存信息版本
   */
  static getWebSqlVersion () {
    if (!window.GLOB.WebSql) {
      return Promise.reject()
    }
 
    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: ''})
          })
        })
      })
    )
 
    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)
      })
    })
  }
 
  /**
   * @description 删除websql中保存的配置信息
   */
  static delWebSqlConfig (keys) {
    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)
        })
      })
    }
  }
 
  /**
   * @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()
        }, (tx, results) => {
          console.warn(results)
          resolve()
        })
      })
    })
  }
 
  /**
   * @description 更新websql中配置信息的版本
   */
  static updateWebSqlversion (version, curTime) {
    if (!window.GLOB.WebSql) return
 
    window.GLOB.WebSql.transaction(tx => {
      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)
        })
      }
    })
  }
 
  /**
   * @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) => {
          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 将缓存数据写入websql
   */
  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, 2)
      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 })
        }
        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' })
        }
      }
    } catch (e) {
      console.warn('IndexedDB 初始化失败!')
      window.GLOB.IndexDB = null
    }
  }
 
  /**
   * @description 获取IndexedDB中保存信息版本
   */
  static getIndexDBVersion () {
    if (!window.GLOB.IndexDB) {
      return Promise.reject()
    }
 
    let deffers = []
 
    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 updateIndexDBversion (version) {
    if (!window.GLOB.IndexDB || !version) return
 
    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 {
          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 (menuids) {
    if (!window.GLOB.IndexDB) return
 
    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
      }
 
      request.onsuccess = (e) => {
        let cursor = e.target.result
        if (cursor) {
          if (menuids.includes(cursor.value.menuid)) {
            cursor.delete()
          }
          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 将数据写入IndexedDB
   */
  static writeInIndexDB (data) {
    if (!window.GLOB.IndexDB || !data) return
 
    window.GLOB.IndexDB.transaction(['configs'], 'readwrite').objectStore('configs').add(data)
  }
 
  /**
   * @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) {
    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()
        }
      }
    }
  }
}