import axios from "axios";
const request = axios.create({
  baseURL: window.customENV.baseURL, // api 的 base_url
  timeout: window.customENV.timeout, // request timeout
});

// request interceptor
request.interceptors.request.use(
  (config: any) => {
    //   config.headers["Authorization"] = token
    return config;
  },
  (error: any) => {
    // Do something with request error
    console.log(error); // for debug
    Promise.reject(error);
  }
);

request.interceptors.response.use(
  function (response: { data: any; }) {
    const data = response.data;
    if (data.code === 0 || data.code === 200 || data.code == "200") {
      // localStorage.setItem('token', data.token)
      return data;
    } else {
      if (data.code == 405) {
        console.log("没有权限");
      }
      // message.error(data.msg || data.desc)
      return Promise.reject(data.msg);
    }
  },
  function (error: { response: { data: { path: string; }; }; }) {
    console.log(error.response.data.path);
    if (error.response.data.path == "/api/login/noAuthority") {
      console.log("没有权限");
    }
    // if(error.code === 4002) {
    //   message.error(error.message)
    //   window.location.href = '#/login'
    //   return Promise.reject(error)
    // }
    return Promise.reject(error.response);
  }
);

export default request;
