import axios from "axios";
import { message } from "ant-design-vue";
import { getToken } from "./token";
// 创建axios实例

const instance = axios.create({
  baseURL: process.env.VUE_APP_BASE_URL,
  // 设置相应的时间
  timeout: 5000,
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json",
  },
});
const token:String = getToken();

// 请求拦截器
instance.interceptors.request.use(
  function (config: any) {
    // 这里判断localStorage里面是否存在token，如果有则在请求头里面设置
    if (token) {
    // config.headers.Authorization = token;
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

// 响应式拦截器
instance.interceptors.response.use(
  function (response) {
    if (response.status === 200 ) {
      return response.data
    } else {
      message.error(response.data.message);
    }
  },
  function (error) {
    return Promise.reject(error);
  }
);

export default instance;
