import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import { VueAxios } from './axios'
// import { ACCESS_TOKEN, TENANT_ID } from '@/store/mutation-types'

/**
 * 【指定 axios的 baseURL】
 * 如果手工指定 baseURL: '/jeecg-boot'
 * 则映射后端域名，通过 vue.config.js
 * @type {*|string}
 */
// 创建 axios 实例
const service = axios.create({
  baseURL: process.env.API, // api base_url
  timeout: 9000, // 请求超时时间
})

// request interceptor
service.interceptors.request.use(config => {
  // const token = Vue.ls.get(ACCESS_TOKEN)
  // if (token) {
  //   config.headers['Authorization'] = 'Bearer ' + token // 让每个请求携带自定义 token 请根据实际情况自行修改
  // }
  return config
}, (error) => {
  return Promise.reject(error)
})

// response interceptor
service.interceptors.response.use((response) => {
  if (response.data.code !== 200) {
    Vue.prototype.$Jnotification.error(
      { message: '系统提示', description: response.data.message, duration: 4 })
  }
  if (response.status == 200) {
    return response.data
  }
}, () => {
  return Promise.reject('error')
})

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

export {
  installer as VueAxios,
  service as axios,
}
