king
2020-08-09 ce2b708f61de1855771d78f35309bd77df9d3b15
src/api/index.js
@@ -6,6 +6,32 @@
import Utils from '@/utils/utils.js'
import options from '@/store/options.js'
let mkDB = null
if (window.GLOB.webSqlUsable) {
  let service = window.GLOB.service ? '-' + window.GLOB.service.replace('/', '') : ''
  try {
    mkDB = openDatabase(`mkdb${service}`, '1', 'mk-pc-database', 50 * 1024 * 1024)
    mkDB.transaction(tx => {
      tx.executeSql('CREATE TABLE IF NOT EXISTS VERSIONS (id unique, version 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 (id unique, 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'
      })
    })
  } catch (e) {
    console.warn(e)
    mkDB = null
  }
}
axios.defaults.crossDomain = true
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
axios.defaults.withCredentials = true
@@ -199,6 +225,82 @@
  }
  /**
   * @description 获取系统基本设置,启用或更新websql
   */
  getSystemStyle () {
    // 获取系统信息
    let param = {
      func: 's_Get_style',
      TypeCharOne: 'PC',
      LText: `select '${window.GLOB.appkey}'`,
    }
    param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
    param.secretkey = Utils.encrypt(param.LText, param.timestamp)
    param.userid = sessionStorage.getItem('UserID') || ''
    param.lang = localStorage.getItem('lang') || ''
    param.SessionUid = localStorage.getItem('SessionUid') || ''
    param.LoginUID = sessionStorage.getItem('LoginUID') || ''
    param.appkey = window.GLOB.appkey || ''
    if (window.GLOB.mainSystemApi) {
      param.rduri = window.GLOB.mainSystemApi
    }
    param.nonc = Utils.getuuid()
    let keys = Object.keys(param).sort()
    let values = ''
    keys.forEach(key => {
      if (key === 'rduri' || key === 't') return
      if (typeof(param[key]) === 'object') {
        values += key + JSON.stringify(param[key])
      } else {
        values += key + param[key]
      }
    })
    param.sign  = md5(values)
    param.t = new Date().getTime()
    return new Promise(resolve => {
      axios({
        url: `/webapi/dostars${param.func ? '/' + param.func : ''}`,
        data: param
      }).then(res => {
        if (mkDB) {
          let version = res.app_version ? `${res.app_version}` : '1'
          // mkDB.transaction(tx => {
          //   tx.executeSql('DROP TABLE CONFIGS')
          // })
          mkDB.transaction(tx => {
            tx.executeSql('SELECT * FROM VERSIONS', [], (tx, results) => {
              if (results.rows.length === 0) {
                tx.executeSql('INSERT INTO VERSIONS (id, version) VALUES (?, ?)', ['pc', version])
                tx.executeSql('DELETE FROM CONFIGS')
              } else if (results.rows.length === 1) {
                let _ver = results.rows[0]
                if (version !== _ver.version) {
                  tx.executeSql(`UPDATE VERSIONS SET version=${version}`)
                  tx.executeSql('DELETE FROM CONFIGS')
                }
              } else if (results.rows.length > 1) {
                tx.executeSql('DELETE FROM VERSIONS')
                tx.executeSql('INSERT INTO VERSIONS (id, version) VALUES (?, ?)', ['pc', version])
                tx.executeSql('DELETE FROM CONFIGS')
              }
            }, (tx, results) => {
              console.warn(results)
            })
          })
        }
        resolve(res)
      })
    })
  }
  /**
   * @description 获取或修改云端配置
   */
  getCloudConfig (param) {
@@ -311,6 +413,133 @@
  }
  /**
   * @description 获取系统配置,取值优先等级websql、缓存、服务器
   * @param {Object}  param   请求参数
   */
  getCacheConfig (param) {
    param.userid = sessionStorage.getItem('UserID') || ''
    param.lang = localStorage.getItem('lang') || ''
    param.SessionUid = localStorage.getItem('SessionUid') || ''
    param.LoginUID = sessionStorage.getItem('LoginUID') || ''
    param.appkey = window.GLOB.appkey || ''
    if (sessionStorage.getItem('isEditState') === 'true') { // 编辑状态,单点登录服务器为云端
      if (options.cloudServiceApi) { // 存在云端地址时,使用云端系统参数
        param.rduri = options.cloudServiceApi
        param.userid = sessionStorage.getItem('CloudUserID') || ''
        param.LoginUID = sessionStorage.getItem('CloudLoginUID') || ''
      }
    } else if (window.GLOB.mainSystemApi) {
      param.rduri = window.GLOB.mainSystemApi
    }
    let _param = JSON.parse(JSON.stringify(param)) // 缓存校验,去除时间和加密字符
    delete _param.timestamp
    delete _param.secretkey
    delete _param.open_key
    _param = JSON.stringify(_param)
    _param  = md5(_param)
    if (mkDB) {
      param.nonc = Utils.getuuid()
      let keys = Object.keys(param).sort()
      let values = ''
      keys.forEach(key => {
        if (key === 'rduri' || key === 't') return
        if (typeof(param[key]) === 'object') {
          values += key + JSON.stringify(param[key])
        } else {
          values += key + param[key]
        }
      })
      param.sign  = md5(values)
      param.t = new Date().getTime()
      return new Promise(resolve => {
        mkDB.transaction(tx => {
          tx.executeSql(`SELECT * FROM CONFIGS WHERE menuid='${param.MenuID}' and userid='${param.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 {
              axios({
                url: `/webapi/dostars${param.func ? '/' + param.func : ''}`,
                data: param
              }).then(res => {
                if (res.status) {
                  this.writeInWebSql([param.MenuID, param.MenuID, param.userid, res.open_edition, res.web_edition, res.LongParam, res.LongParamUser])
                }
                resolve(res)
              })
            }
          }, (tx, results) => {
            axios({
              url: `/webapi/dostars${param.func ? '/' + param.func : ''}`,
              data: param
            }).then(res => {
              if (res.status) {
                window.GLOB.CacheMap.set(_param, res)
              }
              resolve(res)
            })
            mkDB = null
            console.warn(results)
          })
        })
      })
    } else if (window.GLOB.CacheMap.has(_param)) {
      return Promise.resolve(window.GLOB.CacheMap.get(_param))
    } else {
      param.nonc = Utils.getuuid()
      let keys = Object.keys(param).sort()
      let values = ''
      keys.forEach(key => {
        if (key === 'rduri' || key === 't') return
        if (typeof(param[key]) === 'object') {
          values += key + JSON.stringify(param[key])
        } else {
          values += key + param[key]
        }
      })
      param.sign  = md5(values)
      param.t = new Date().getTime()
      return new Promise(resolve => {
        axios({
          url: `/webapi/dostars${param.func ? '/' + param.func : ''}`,
          data: param
        }).then(res => {
          if (res.status) {
            window.GLOB.CacheMap.set(_param, res)
          }
          resolve(res)
        })
      })
    }
  }
  /**
   * @description 将数据写入websql
   */
  writeInWebSql (data) {
    if (!mkDB) return
    mkDB.transaction(tx => {
      tx.executeSql('INSERT INTO CONFIGS (id, menuid, userid, openEdition, webEdition, LongParam, LongParamUser) VALUES (?, ?, ?, ?, ?, ?, ?)', data)
    })
  }
  /**
   * @description 获取系统配置,优先从缓存中取值,增加appkey
   * @param {Object}  param   请求参数
   * @param {Boolean} SSO     是否为单点登录地址
@@ -335,6 +564,7 @@
    let _param = JSON.parse(JSON.stringify(param)) // 缓存校验,去除时间和加密字符
    delete _param.timestamp
    delete _param.secretkey
    delete _param.open_key
    _param = JSON.stringify(_param)
    _param  = md5(_param)