import axios from 'axios'
import { tansParams,judgmentNetworkReturnAddress } from "@/utils/ruoyi";

//判定http或者https
let urlText = window.location.href.split('://')[0]+'://';

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例
const service = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  baseURL: urlText+judgmentNetworkReturnAddress(),
  // 超时
  timeout: 10000
})
// request拦截器
service.interceptors.request.use(config => {
  //写入touken
  config.headers['Authorization'] = localStorage.getItem('touken');
  // get请求映射params参数
  if (config.method === 'get' && config.params) {
    let url = config.url + '?' + tansParams(config.params);
    url = url.slice(0, -1);
    config.params = {};
    config.url = url;
  }
  return config
}, error => {
  Promise.reject(error)
})
// 响应拦截器
service.interceptors.response.use(res => {
    return res.data
  },
  error => {
    return Promise.reject(error)
  }
)
export default service
