king
2023-06-14 08cce3334a2dc81d690b518136b0aaea64e48b0b
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
import axios from 'axios'
import md5 from 'md5'
import jsSHA from 'jssha'
 
window.GLOB.IndexDB = null
 
let service = window.GLOB.service ? '-' + window.GLOB.service.replace('/', '') : ''
let db = `mk_inter${service}`
 
if (window.indexedDB) {
  try {
    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('IndexedDB 初始化失败,历史记录将无法查询或修改!')
    window.GLOB.IndexDB = null
  }
}
 
axios.defaults.crossDomain = true
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
axios.defaults.withCredentials = false
 
axios.interceptors.request.use((config) => {
  return config
}, (error) => {
  return Promise.reject(error)
})
 
axios.interceptors.response.use((response) => {
  return Promise.resolve(response)
}, (error) => {
  return Promise.reject(error)
})
 
class Api {
  /**
   * @description 登录系统, 获取用户信息
   */
  dologon (url, method, header, n) {
    let config = {
      url,
      method
    }
 
    if (header) {
      config.headers = header
    }
 
    if (n) {
      let _param = JSON.parse(n)
      let param = {}
 
      Object.keys(_param).forEach(key => {
        param[key.toLowerCase()] = _param[key]
      })
 
      if (param.type && param.username && param.password && param.timestamp) {
        if (param.type === 'S') {
          let shaObj = new jsSHA('SHA-1', 'TEXT')
          shaObj.update(param.password)
          param.password = shaObj.getHash('HEX').toUpperCase()
          param.password = md5(param.username + param.password + param.timestamp)
        } else if (/^mk_/ig.test(param.type)) {
          let shaObj = new jsSHA('SHA-1', 'TEXT')
          shaObj.update(param.password)
          param.password = shaObj.getHash('HEX').toUpperCase()
          param.password = md5(param.privatekey + param.username + param.password + param.timestamp)
 
          delete param.privatekey
        }
      }
 
      config.data = JSON.stringify(param)
    }
 
    return axios(config)
  }
 
  /**
   * @description 通用请求
   */
  normalRequest (url, method, header, n) {
    let config = {
      url,
      method
    }
 
    if (header) {
      config.headers = header
    }
    if (n) {
      config.data = n
    }
 
    return axios(config)
  }
 
  writeInIndexDB (data) {
    if (!window.GLOB.IndexDB) return
 
    window.GLOB.IndexDB.transaction(['interfaces'], 'readwrite').objectStore('interfaces').add(data)
  }
 
  getInterfaces () {
    if (!window.GLOB.IndexDB) return Promise.resolve()
 
    return new Promise((resolve, 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 (id) {
    if (!window.GLOB.IndexDB) return
 
    window.GLOB.IndexDB.transaction(['interfaces'], 'readwrite').objectStore('interfaces').delete(id)
  }
 
  /**
   * @description 清空接口调用记录
   */
  clearInterfaces () {
    if (!window.GLOB.IndexDB) return
 
    window.GLOB.IndexDB.transaction(['interfaces'], 'readwrite').objectStore('interfaces').clear()
  }
}
 
export default new Api()