Skip to content
This repository was archived by the owner on Nov 1, 2019. It is now read-only.

Commit 959b468

Browse files
committed
update: api
1 parent 91cfbb7 commit 959b468

File tree

1 file changed

+95
-92
lines changed

1 file changed

+95
-92
lines changed

src/util/api.js

Lines changed: 95 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import axios from 'axios'
22
const qs = require('qs')
33
const urlPrefix = '/api/'
4-
let api = {}
54

65
const interceptors = () => {
76
axios.interceptors.request.use((config) => {
@@ -17,102 +16,106 @@ const interceptors = () => {
1716
})
1817
}
1918

20-
/**
21-
* GET api
22-
* @param {String} getfix url head
23-
* @param {Object} data parameters
24-
* @param {Object} self this
25-
*/
26-
api.getUrlData = (getfix, data, callback, self) => {
27-
const url = urlPrefix + getfix + (Object.keys(data).length > 0 ? `?${qs.stringify(data)}` : '')
28-
interceptors()
29-
axios.get(url)
30-
.then(res => {
31-
callback(res)
32-
}).catch(error => {
33-
if (error.response) {
34-
callback(error.response)
35-
} else {
36-
callback(error, true)
37-
}
38-
})
39-
}
19+
class API {
20+
/**
21+
* GET api
22+
* @param {String} getfix url head
23+
* @param {Object} data parameters
24+
* @param {Object} self this
25+
*/
26+
getUrlData = (getfix, data, callback, self) => {
27+
const url = `${urlPrefix}${getfix}${(Object.keys(data).length > 0 ? `?${qs.stringify(data)}` : '')}`
28+
interceptors()
29+
axios.get(url)
30+
.then(res => {
31+
callback(res)
32+
}).catch(error => {
33+
if (error.response) {
34+
callback(error.response)
35+
} else {
36+
callback(error, true)
37+
}
38+
})
39+
}
4040

41-
/**
42-
* DELETE api
43-
* @param {String} getfix url head
44-
* @param {Object} data parameters
45-
* @param {Object} self this
46-
*/
47-
api.deleteUrlData = (getfix, data, callback, self) => {
48-
const url = urlPrefix + getfix + (Object.keys(data).length > 0 ? `?${qs.stringify(data)}` : '')
49-
interceptors()
50-
axios.delete(url)
51-
.then(res => {
52-
callback(res)
53-
}).catch(error => {
54-
if (error.response) {
55-
callback(error.response)
56-
} else {
57-
callback(error, true)
58-
}
59-
})
60-
}
41+
/**
42+
* DELETE api
43+
* @param {String} getfix url head
44+
* @param {Object} data parameters
45+
* @param {Object} self this
46+
*/
47+
deleteUrlData = (getfix, data, callback, self) => {
48+
const url = `${urlPrefix}${getfix}${(Object.keys(data).length > 0 ? `?${qs.stringify(data)}` : '')}`
49+
interceptors()
50+
axios.delete(url)
51+
.then(res => {
52+
callback(res)
53+
}).catch(error => {
54+
if (error.response) {
55+
callback(error.response)
56+
} else {
57+
callback(error, true)
58+
}
59+
})
60+
}
6161

62-
/**
63-
* POST api
64-
* @param {String} postfix url head
65-
* @param {Object} data parameters
66-
* @param {Object} self this
67-
*/
68-
api.postUrlData = (postfix, data, callback, isJson, self) => {
69-
interceptors()
70-
axios.post(urlPrefix + postfix, isJson ? data : qs.stringify(data))
71-
.then(res => {
72-
callback(res)
73-
})
74-
.catch(error => {
75-
if (error.response) {
76-
callback(error.response)
77-
} else {
78-
callback(error, true)
79-
}
80-
})
81-
}
62+
/**
63+
* POST api
64+
* @param {String} postfix url head
65+
* @param {Object} data parameters
66+
* @param {Object} self this
67+
*/
68+
postUrlData = (postfix, data, callback, isJson, self) => {
69+
interceptors()
70+
axios.post(`${urlPrefix}${postfix}`, isJson ? data : qs.stringify(data))
71+
.then(res => {
72+
callback(res)
73+
})
74+
.catch(error => {
75+
if (error.response) {
76+
callback(error.response)
77+
} else {
78+
callback(error, true)
79+
}
80+
})
81+
}
8282

83-
/**
84-
* PUT api
85-
* @param {String} postfix url head
86-
* @param {Object} data parameters
87-
* @param {Object} self this
88-
*/
89-
api.putUrlData = (postfix, data, callback, isJson, self) => {
90-
interceptors()
91-
axios.put(urlPrefix + postfix, isJson ? data : qs.stringify(data))
92-
.then(res => {
93-
callback(res)
94-
})
95-
.catch(error => {
96-
if (error.response) {
97-
callback(error.response)
98-
} else {
99-
callback(error, true)
100-
}
101-
})
102-
}
83+
/**
84+
* PUT api
85+
* @param {String} postfix url head
86+
* @param {Object} data parameters
87+
* @param {Object} self this
88+
*/
89+
putUrlData = (postfix, data, callback, isJson, self) => {
90+
interceptors()
91+
axios.put(`${urlPrefix}${postfix}`, isJson ? data : qs.stringify(data))
92+
.then(res => {
93+
callback(res)
94+
})
95+
.catch(error => {
96+
if (error.response) {
97+
callback(error.response)
98+
} else {
99+
callback(error, true)
100+
}
101+
})
102+
}
103103

104-
/**
105-
* GET Download
106-
* @param {String} url download url
107-
*/
108-
api.downloadUrl = (url) => {
109-
let iframe = document.createElement('iframe')
110-
iframe.style.display = 'none'
111-
iframe.src = url
112-
iframe.onload = function () {
113-
document.body.removeChild(iframe)
104+
/**
105+
* GET Download
106+
* @param {String} url download url
107+
*/
108+
downloadUrl = (url) => {
109+
let iframe = document.createElement('iframe')
110+
iframe.style.display = 'none'
111+
iframe.src = url
112+
iframe.onload = function () {
113+
document.body.removeChild(iframe)
114+
}
115+
document.body.appendChild(iframe)
114116
}
115-
document.body.appendChild(iframe)
116117
}
117118

119+
const api = new API()
120+
118121
export default api

0 commit comments

Comments
 (0)