import axios from 'axios'

const http = axios.create({
  baseURL: '/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')
    window.location.href = '/login'
  }
  return res.data;
}, e => {
  return Promise.reject(e)
})

export default http
