/**
 * @description: 请求
 * @author: Adrian
 */
/* eslint-disable */
import axios from "axios";
import { Message } from "element-ui";
import { getToken } from '../utils/auth'
const service = axios.create({
  timeout: 10*1000
});
// request interceptor
service.interceptors.request.use(
  config => {
    if (getToken()) {
      config.headers['X-AUTH-TOKEN'] = getToken()
    }
    return config
  },
  error => {
    Promise.reject(error);
  }
);

// respone interceptor
service.interceptors.response.use(
  response => {
    const status = response.data.code || 200
    const message = response.data.msg || '未知错误';
    // 如果请求为非200否者默认统一处理
    if (status !== 200) {
      Message({
        message: message,
        type: 'error'
      })
      return Promise.reject(new Error(message))
    }
    return response;
  },
  error => {
    if(error.message.includes('status code')){
      Message({
        message:error.message,
        type: "error",
      });
    }else{
      Message({
        message:'请求超时，请重新操作',
        type: "error",
      });
    }
    return Promise.reject(new Error(error));
  }
);

export default service;
