| | |
| | | import axios from 'axios'; |
| | | import router from '@/router' |
| | | // 引入elementUI |
| | | import tip from 'element-ui'; |
| | | const api = axios.create({ |
| | | baseURL: 'http://42.193.1.25:8082/', // 请求的公共地址部分 |
| | | timeout: 15000 |
| | | }) |
| | | // 拦截器 |
| | | // 请求 |
| | |
| | | config.headers.Authorization = tokenHead + token; |
| | | } |
| | | return config; |
| | | },(err)=>{ |
| | | }, (err) => { |
| | | console.log(err); |
| | | } |
| | | ) |
| | | // 响应 |
| | | api.interceptors.response.use( |
| | | function (response) { |
| | | if (response.data.code === 401) { |
| | | tip.Message({ |
| | | type: 'warning', |
| | | message: '登录身份已过期', |
| | | }) |
| | | router.push('/login'); |
| | | } |
| | | return response.data; |
| | | }, |
| | | function (error) { |
| | | const {code} = error.response.data; |
| | | if(code === 401){ |
| | | console.log(error); |
| | | if(error.code === "ECONNABORTED"){ |
| | | tip.Message({ |
| | | type:'error', |
| | | message:'网络请求超市', |
| | | duration: 3 * 1000, |
| | | }) |
| | | return; |
| | | } |
| | | const {code,status} = error.response.data; |
| | | if (error.code === 'ERR_NETWORK') { |
| | | console.log(1); |
| | | tip.Message({ |
| | | type: 'error', |
| | | message: '服务器故障' |
| | | }) |
| | | return; |
| | | } |
| | | if (code === 401) { |
| | | console.log(2); |
| | | tip.Message({ |
| | | type: 'warning', |
| | | message: '登录身份已过期', |
| | | }) |
| | | router.push('/login'); |
| | | return; |
| | | } |
| | | if (status === 500) { |
| | | console.log(3); |
| | | tip.Message({ |
| | | type: 'error', |
| | | message: '网络请求出错' |
| | | }) |
| | | return; |
| | | } |
| | | } |
| | | ); |