import axios from 'axios'
import { BASE_API } from '/public/config/server.js'
import router from './../router/index'

// 创建axios实例
const service = axios.create({
  baseURL: BASE_API + "/api",
  timeout: 50000, // 请求超时时间
  withCredentials: false,
})

// request拦截器
service.interceptors.request.use(config => {
  config.headers['X-Token'] = localStorage.getItem("Token") // 让每个请求携带自定义token 请根据实际情况自行修改
  // config.headers['X-Token'] = '56d544cd'
  return config
}, error => {
  // Do something with request error
  Promise.reject(error)
})

service.interceptors.response.use(function (response) {
  return response
}, async function (error) {

  if (error.response.data === "权限验证失败") {
    localStorage.removeItem('Token');
    router.push({ name: 'login' });
  }
})

export default service

