import axios from 'axios'


export const BASE_URL = import.meta.env.VITE_BASE_URL


//用来拦截用的
axios.defaults.headers.post["Content-Type"] = "application/json;charset=utf-8";

const http = axios.create({
  baseURL: BASE_URL,
  timeout: 1000 * 60 * 5,
})


 
// 请求拦截器
http.interceptors.request.use(
  config => {
    let token = localStorage.getItem('token')
    if (token) {
      config.headers.token = token
    }
    let lang = localStorage.getItem('locale')
    lang = lang && lang == 'zh-CN' ? 'zh' : lang
    config.headers.lang = lang ? lang : 'en'
    return config
  },
  err => {
    return Promise.reject(err)
  }
)
 
// 响应拦截器
http.interceptors.response.use(
  res => {
    // console.log('res', res)
    if (res.data.ret == 1) {
      return Promise.resolve(res.data)
    }
    else{
      return Promise.reject(res.data)
    }
  },
  err => {
    return Promise.reject(err)
  }
)
 

export default http