// import request from './request';
// import apis from '../config/api';

// export default (file) => {
//   return new Promise((resolve, reject) => {
//     const fm = new FormData()
//     fm.append('file', file)
//     request({
//       ...apis.file.upload,
//       data: fm,
//     }).then((data) => {
//       resolve(data)
//     }).catch((err) => {
//       reject(err.message || err)
//     })
//   })
// }
import axios from 'axios'
import { Message } from 'element-ui'
// import store from '@/store'
// import { getToken } from '@/utils/auth'

// create an axios instance
const service = axios.create({
  headers: { 'Accept': '*/*' },
  baseURL: process.env.BASE_API, // api 的 base_url
  timeout: 10000 // request timeout
})

service.defaults.withCredentials = true

// request interceptor
service.interceptors.request.use(
  config => {
    // Do something before request is sent
    // if (store.getters.token) {
    //   // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
    //   config.headers['X-Token'] = getToken()
    // }
    return config
  },
  error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    // console.log('response: ', response)
    const resData = response.data

    if (resData.code !== '0') {
      Message({
        message: resData.message,
        type: 'error',
        duration: 5 * 1000
      })
    }
    return response
  },

  error => {
    console.log('err :', error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
