king
2023-06-14 08cce3334a2dc81d690b518136b0aaea64e48b0b
src/views/interface/api/index.js
@@ -1,25 +1,30 @@
import axios from 'axios'
import md5 from 'md5'
import jsSHA from 'jssha'
import { notification } from 'antd'
window.GLOB.WebSql = null
window.GLOB.IndexDB = null
if (window.openDatabase) {
  let service = window.GLOB.service ? '-' + window.GLOB.service.replace('/', '') : ''
let service = window.GLOB.service ? '-' + window.GLOB.service.replace('/', '') : ''
let db = `mk_inter${service}`
if (window.indexedDB) {
  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 INTERFACES (uuid varchar(50), createDate varchar(50), method varchar(50), interface text, params text, headers text, active varchar(50), raw text, formData text, CDefine1 varchar(50), CDefine2 varchar(50), CDefine3 varchar(50), CDefine4 varchar(50), CDefine5 text)', [], () => {
      }, () => {
        // eslint-disable-next-line
        throw 'CREATE TABLE ERROR'
      })
    })
    let request = window.indexedDB.open(db, 1)
    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('interfaces')) {
        window.GLOB.IndexDB.createObjectStore('interfaces', { keyPath: 'id' })
      }
    }
  } catch (e) {
    console.warn('WebSql 初始化失败!')
    window.GLOB.WebSql = null
    console.warn('IndexedDB 初始化失败,历史记录将无法查询或修改!')
    window.GLOB.IndexDB = null
  }
}
@@ -102,73 +107,46 @@
    return axios(config)
  }
  writeInWebSql (data) {
    if (!window.GLOB.WebSql) {
      notification.warning({ top: 92, message: 'WebSql开启失败!', duration: 5 })
      return
    }
    return new Promise((resolve, reject) => {
      window.GLOB.WebSql.transaction(tx => {
        tx.executeSql(`INSERT INTO INTERFACES (uuid, createDate, method, interface, params, headers, active, raw, formData) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, data, (tx, results) => {
          resolve(results)
        }, () => {
          resolve()
        })
      })
    })
  writeInIndexDB (data) {
    if (!window.GLOB.IndexDB) return
    window.GLOB.IndexDB.transaction(['interfaces'], 'readwrite').objectStore('interfaces').add(data)
  }
  getInterfaces () {
    if (!window.GLOB.WebSql) {
      notification.warning({ top: 92, message: 'WebSql开启失败!', duration: 5 })
      return
    }
    if (!window.GLOB.IndexDB) return Promise.resolve()
    return new Promise((resolve, reject) => {
      window.GLOB.WebSql.transaction(tx => {
        tx.executeSql(`SELECT * FROM INTERFACES`, [], (tx, results) => {
          // let paramItem = results.rows[0]
          resolve(results)
        }, () => {
          window.GLOB.WebSql = null
          reject()
        })
      })
      let request = window.GLOB.IndexDB.transaction(['interfaces']).objectStore('interfaces').openCursor()
      let list = []
      request.onsuccess = (e) => {
        let cursor = e.target.result
        if (cursor) {
          if (cursor.value) {
            list.push(cursor.value)
          }
          cursor.continue()
        } else {
          resolve(list)
        }
      }
    })
  }
  delInterface (uuid) {
    if (!window.GLOB.WebSql) {
      notification.warning({ top: 92, message: 'WebSql开启失败!', duration: 5 })
      return
    }
    return new Promise((resolve, reject) => {
      window.GLOB.WebSql.transaction(tx => {
        tx.executeSql(`DELETE FROM INTERFACES where uuid = '${uuid}'`, [], (tx, results) => {
          resolve(results)
        }, () => {
          resolve()
        })
      })
    })
  delInterface (id) {
    if (!window.GLOB.IndexDB) return
    window.GLOB.IndexDB.transaction(['interfaces'], 'readwrite').objectStore('interfaces').delete(id)
  }
  /**
   * @description 清空接口调用记录
   */
  clearInterfaces () {
    if (!window.GLOB.WebSql) {
      notification.warning({ top: 92, message: 'WebSql开启失败!', duration: 5 })
      return
    }
    return new Promise((resolve, reject) => {
      window.GLOB.WebSql.transaction(tx => {
        tx.executeSql(`DELETE FROM INTERFACES`, [], (tx, results) => {
          resolve(results)
        }, () => {
          resolve()
        })
      })
    })
    if (!window.GLOB.IndexDB) return
    window.GLOB.IndexDB.transaction(['interfaces'], 'readwrite').objectStore('interfaces').clear()
  }
}