import axios from 'axios'


// create an axios instance
const service = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  baseURL: window.Glod.baseURLConfig,
  // baseURL: 'http://127.0.0.1:8200/',
  timeout: 10000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    config.headers['Content-Type'] = 'application/json'
    return config
  },
  error => {
    // do something with request error
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    const data = response.data
    if (!data.success) {
      // return false
        return false
    }
    return data
  },
  error => {
    console.log(error)
    if (error.toString().indexOf('Error: Network Error') !== -1) {
      // alert('网络请求错误')
      // removeToken()
      return Promise.reject(error)
    }
    if (error.toString().indexOf('Error: Request failed with status code 404') !== -1) {
      alert('请求地址错误')
      return Promise.reject(error)
    }

    if (error.toString().indexOf('Error: timeout') !== -1) {
      // alert('网络请求超时')
      return Promise.reject(error)
    }
  }
)

export default service

