import Axios from 'axios'; import { Message } from 'element-ui' let token = sessionStorage.getItem('token') const axios = Axios.create({ baseURL: '', timeout: 150000, // request timeout headers: { 'Authorization': token }, method: 'GET' }); let showMsg = true; axios.interceptors.request.use( (req) => { if(!token) { req.headers.token = sessionStorage.getItem('token'); return req; } }, (err) => { Message.error('请求中错误'+ err) return Promise.reject(err); } ) axios.interceptors.response.use( (res) => { if(res.status == 200) { if(res.data.code != 0) { if(showMsg) { Message.error('响应错误'+ res.data.msg) } showMsg = false; setTimeout(() => { showMsg = true; }, 3000) } } return res.data }, (err) => { Message.error('错误' + err) return Promise.reject(err) } ) export default axios;