import Cookies from 'js-cookies';
import axios from 'axios';
/**
* 封装,拦截
*/
axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers['token'] = 'token';
axios.defaults.headers.post['Content-Type'] = 'application/json';
const Axios = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 10000,
headers: {
'X-Custom-Header': 'foobar',
'token':'token'
}
});
Axios.interceptors.request.use(config => {
//
if(true) {
config.headers['from'] = 'web'
return config;
} else {
return config;
}
},error => {
return Promise.reject(error);
})
Axios.interceptors.response.use(res => {
// res schema
// res.status
// res.data
// res.statusText
// res.headers
// res.config
// res.request
if(res.status = 200) {
return res.data;
}
},error => {
// 错误处理
const code = error.response.status;
switch (code ) {
case code > 500:
// toast('server:error')
break;
default:
break;
}
return Promise.reject(error)
})
const get = (path,params) => {
return Axios.get(path,{params: params})
}
const post = (path,params) => {
return Axios.post(path,params)
}
// concurrent request
const all = (...args) => {
return axios.all(args)
}
export { get,post,all }
references