import axios from 'axios';

const request = axios.create({
  timeout: 5000
});

//响应拦截器
request.interceptors.response.use(
  response => {
    if (response.data.code === 200) {
      return response.data;
    }
    return Promise.reject(response.data);
  },
  error => {
    console.error('请求错误:', error);
    return Promise.reject(error);
  }
);

//请求拦截器
request.interceptors.request.use(
  config => {
    console.log('Request:',config);
    const token = localStorage.getItem('accessToken');
    if (token) {
      config.headers['Authorization'] = token;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

export default request;