65 lines
1.2 KiB
JavaScript
65 lines
1.2 KiB
JavaScript
import axios from 'axios'
|
|
import utils from '../utils'
|
|
|
|
class HTTP {
|
|
constructor () {
|
|
this.request()
|
|
this.response()
|
|
}
|
|
|
|
async get (params) {
|
|
this.SetBaseUrl()
|
|
return await axios.get(params.url, {
|
|
params: params.data,
|
|
headers: params.header
|
|
})
|
|
}
|
|
|
|
async post (params) {
|
|
this.SetBaseUrl()
|
|
return await axios.post(params.url, params.data, {
|
|
headers: params.header
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 请求拦截器
|
|
*/
|
|
request () {
|
|
axios.interceptors.request.use(function (config) {
|
|
// 在发送请求之前做些什么
|
|
return config
|
|
}, function (error) {
|
|
// 对请求错误做些什么
|
|
return Promise.reject(error)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 添加响应拦截器
|
|
*/
|
|
response () {
|
|
axios.interceptors.response.use(function (response) {
|
|
// 对响应数据做点什么
|
|
switch (response.data.status) {
|
|
case 200:
|
|
return response.data.data;
|
|
break;
|
|
|
|
default:
|
|
throw new Error(response.data.msg)
|
|
break
|
|
}
|
|
}, function (error) {
|
|
// 对响应错误做点什么
|
|
return Promise.reject(error)
|
|
})
|
|
}
|
|
|
|
SetBaseUrl () {
|
|
axios.defaults.baseURL = utils.checkUrl()
|
|
}
|
|
}
|
|
|
|
export default new HTTP()
|