import axios from 'axios'
import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/el-message.css'
import TokenService from '@/utils/token'
import router from '@/router'
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
const httpInstance=axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 10000
})
// axios请求拦截器
httpInstance.interceptors.request.use(config => {
    // 是否需要设置 token
  const isToken = (config.headers || {}).isToken === false
  if (TokenService.hasToken() && !isToken) {
    config.headers['Authorization'] =  TokenService.getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  }
    return config 
  }, e => Promise.reject(e))
  
// axios响应式拦截器
httpInstance.interceptors.response.use(res => {
if (res.data.code === 401){
    router.push('/login')
}
if(res.data.code==0){
ElMessage.error(res.data.msg)
}
return res.data
}, e => {
return Promise.reject(e)
})
// let abb=1;
export default httpInstance;

