king
2021-07-18 2850799963a5bff04aeeb9eb73dc4eb91a0dc165
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
import axios from 'axios'
import md5 from 'md5'
import jsSHA from 'jssha'
import { notification } from 'antd'
 
window.GLOB.WebSql = null
 
if (window.openDatabase) {
  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 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'
      })
    })
  } catch (e) {
    console.warn('WebSql 初始化失败!')
    window.GLOB.WebSql = 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)
  }
 
  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()
        })
      })
    })
  }
 
  getInterfaces () {
    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(`SELECT * FROM INTERFACES`, [], (tx, results) => {
          // let paramItem = results.rows[0]
          resolve(results)
        }, () => {
          window.GLOB.WebSql = null
          reject()
        })
      })
    })
  }
 
  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()
        })
      })
    })
  }
 
  /**
   * @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()
        })
      })
    })
  }
}
 
export default new Api()