text: import axios from 'axios'
  import { MessageBox, Message } from 'element-ui'
  import store from '@/store'
  import { getToken } from '@/utils/auth'
  
  // create an axios instance
  const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 5000, // request timeout
  })
  
  service.interceptors.request.use(
    (config) => {
      // 如果存在 token 则附带在 http header 中
      if (store.getters.token) {
        config.headers['Authorization'] = `Bearer ${getToken()}`
      }
      return config
    },
    (error) => {
      // do something with request error
      console.log(error) // for debug
      return Promise.reject(error)
    }
  )
  
  // response interceptor
  service.interceptors.response.use(
    (response) => {
      const res = response.data
      if (res.code !== 0) {
        const errmsg = res.msg || '请求失败'
        Message({
          message: errmsg,
          type: 'error',
          duration: 5 * 1000
        })
        if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
          MessageBox.confirm('Token验证已失效，请重新登录！', '确认登出', {
            confirmButtonText: '重新登录',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            store.dispatch('user/resetToken').then(() => {
              location.reload()
            })
          })
        }
        return Promise.reject(new Error(errmsg))
      } else {
        return res
      }
    },
    (error) => {
      // console.log({ error }) // for debug
      const { msg } = error.response.data
      Message({
        message: msg || '请求失败',
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(error)
    }
  )
  
  export default service
  