import axios from 'axios'
import { VueAxios } from './axios'
import TokenStorageUtil from "./TokenStorageUtil";
import YchConstant from "./YchConstant";
/**
 * 【指定 axios的 baseURL】
 * 如果手工指定 baseURL: '/yuncheng-boot'
 * 则映射后端域名，通过 vue.config.js
 * @type {*|string}
 */
let apiBaseUrl = YchConstant.PREFIX_REQUEST;
// 创建 axios 实例
const service = axios.create({
  //baseURL: '/yuncheng-boot',
  baseURL: apiBaseUrl, // api base_url
  timeout: 30000 // 请求超时时间
})

// request interceptor
service.interceptors.request.use(config => {
  const token = TokenStorageUtil.get()
  if (token) {
    config.headers[ 'X-Access-Token' ] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
  if(config.method=='get'){
    if(config.url.indexOf("system/dict/getDictItems")<0){
      config.params = {
        _t: Date.parse(new Date())/1000,
        ...config.params
      }
    }
  }
  if(config.url.includes("/common/upload")){
    config.timeout = 1000 * 60 * 10
  }
  return config
},(error) => {
  return Promise.reject(error)
})

// response interceptor
service.interceptors.response.use((response) => {
    return response.data
  })

const installer = {
  vm: {},
  install (Vue, router = {}) {
    Vue.use(VueAxios, router, service)
  }
}

export {
  installer as VueAxios,
  service as axios
}
