import axios from 'axios'

const http = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 50000
})

// axios请求拦截器
http.interceptors.request.use(config => {
  const isToken = config.headers.isToken === false
  if(!isToken && localStorage.getItem('token')){
    config.headers.Authorization = `Bearer ${localStorage.getItem('token')}`
  }

  config.headers.clientId = 'e5cd7e4891bf95d1d19206ce24a7b32e'

  return config
}, e => Promise.reject(e))

// axios响应式拦截器
http.interceptors.response.use(res => {
  if(res.data.code == 401) {
    localStorage.removeItem('token')
    localStorage.removeItem('userInfo')
    window.location.href = '/login'
  }
  return res.data;
}, e => {
  return Promise.reject(e)
})

export default http
