import axios from 'axios'
import qs from 'qs'
const service = axios.create({
  baseURL: process.env.API_ROOT, // api的base_url
  timeout: 50000 // 请求超时时间
})
function ajax (params) {
  const method = (params && params.method) || 'get'
  if (method === 'post') {
    const headers = params && params.headers && params.headers['Content-Type']
    if (headers && headers.search('json')) {
      params.data = JSON.stringify(params.params)
    } else {
      params.data = qs.stringify(params.params)
    }

    delete params.params
    return service(params)
  } else {
    return service(params)
  }
}

export {
  service,
  ajax
}
