api.js
1.16 KB
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
import util from '@/libs/util';
import iView from 'iview';
import qs from 'qs';
class Api {
constructor() {
}
_get(url, data = {}, options = {}) {
let params = this._2params(data);
if (!PRODUCTION) { //eslint-disable-line
params.debug = 'XYZ';
}
return util.ajax.get(url, Object.assign({params}, options)).then(result => result.data).catch(this.catch);
}
_post(url, data = {}, json = false, options = {}) {
let params = this._2params(data);
if (!PRODUCTION) { //eslint-disable-line
url += (url.indexOf('?') >= 0 ? '&' : '?') + 'debug=XYZ';
}
if (!json) {
params = qs.stringify(params);
}
return util.ajax.post(url, params, options).then(result => {
return result.data;
}).catch(this.catch);
}
catch({message}) {
iView.Message.destroy();
iView.Message.warning(message || '接口错误');
return Promise.resolve({});
}
_2params(data) {
if (Array.isArray(data)) {
return data;
}
const params = {};
Object.keys(data).forEach(k => {
if (data[k] || data[k] === 0) {
params[k] = data[k];
}
});
return params;
}
}
export default Api;