const axios = require('axios')
const { readConfigFile } = require('../utils/config');
const pyapp = readConfigFile().pyapp


/* axios.defaults.withCredentials = true*/

// create an axios instance
const service = axios.create()
const baseUrl = 'http://'+pyapp+':7074'
const basePrefixUrl = 'api'
const timeout = 600000
const uploadTimeout = 600000

 function request(config) {
  return service(config)
}

/* get 默认取数据 */
 function get(config) {
  return _get({ ...config, baseUrl })
}
 function getPay(config) {
  return _get({ ...config, basePayUrl })
}
function _get(config = {
  baseUrl,
  url: '',
  data: {}
}) {

  const thisBasePrefixUrl = config.basePrefixUrl !== undefined ? config.basePrefixUrl : basePrefixUrl
  return service.get(config.baseUrl + config.url, {
    params: {
      ...config.data
    },
    headers: config.headers || {},
    timeout: config.timeout ||  timeout
  })
}
 function post(config) {
  return _post({ ...config, baseUrl })
}
 function postPay(config) {
  return _post({ ...config, baseUrl: basePayUrl })
}
/* post 默认提交数据 */
function _post(config = {
  baseUrl,
  url: '',
  data: {}
}) {
  const thisBasePrefixUrl = config.basePrefixUrl !== undefined ? config.basePrefixUrl : basePrefixUrl
  return service.post(config.baseUrl + config.url, {
    ...config.data
  }, {
    headers: config.headers || {},
    timeout: config.timeout ||  timeout,
    roles:config.roles,
  })
}


module.exports = {
  get,
  post
}
