| |
| * 请求拦截、错误统一处理 |
| */ |
| import axios from 'axios' |
| import { message } from 'ant-design-vue' |
| |
| const instance = axios.create({ |
| baseURL: 'http://localhost:8080', |
| validateStatus: function (status) { |
| |
| return status >= 200 && status < 300; |
| }, |
| transformResponse: function (data) { |
| console.log(data) |
| return data; |
| }, |
| headers: { |
| 'Content-Type': 'application/json;charset=utf-8' |
| }, |
| timeout: 1000 * 5, |
| responseType: 'json', |
| responseEncoding: 'utf8' |
| }) |
| |
| |
| * 请求拦截器 |
| */ |
| instance.interceptors.request.use(function (config) { |
| |
| const token = '' |
| token && (config.headers.token = token) |
| return config; |
| }, function (error) { |
| return Promise.reject(error); |
| }); |
| |
| |
| * 响应拦截器 |
| */ |
| instance.interceptors.response.use(function (response) { |
| console.log(response) |
| return response; |
| }, function (error) { |
| console.log(error.response) |
| if (error.response) { |
| |
| switch (error.response.status) { |
| case 401: { |
| console.log('未登录') |
| |
| message.error("未登录") |
| break |
| } |
| |
| case 403: { |
| console.log('登录过期,请重新登录') |
| |
| |
| message.error("登录过期,请重新登录") |
| break |
| } |
| |
| case 404: { |
| console.log('请求地址错误') |
| message.error("请求地址错误") |
| break |
| } |
| |
| default: { |
| console.log('其他错误') |
| } |
| } |
| } else { |
| |
| message.error("网络连接异常") |
| } |
| |
| |
| |
| return Promise.reject(error); |
| }); |
| |
| export default instance; |