
import { getToken } from '@/utils/auth'
import store from '@/store'
import axios from 'axios'

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: 6000 * 10,
  transformRequest: [function(data, headers) {
    headers['Content-Type'] = 'application/json'
    return JSON.stringify(data)
  }]
// request timeout
})
service.interceptors.request.use(
  config => {
    // do something before request is sent
    if (store.getters.token) {
      config.headers['Token'] = getToken()
    }
    return config
  },
  error => {
    // do something with request error
    return Promise.reject(error)
  }
)
service.interceptors.response.use(
  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    const res = response.data.data || response.data.Data || response.data
    if (Object.prototype.toString.call(res) === '[object Array]') {
      for (var i = 0; i < res.length; i++) {
        for (var r in res[i]) {
          if (res[i][r] != null && res[i][r].toString().search(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?/i) >= 0) {
            res[i][r] = res[i][r].toString().replace('T', ' ')
          }
        }
      }
    }
    return res
  },
  error => {
    return Promise.reject(error.response.data.message || error.response.data)
  }
)
/**
 * 计费渠道更换
 * @param {*} params
 * @returns
 */
export function _ChangerOrderCreateChannelId(params) {
  return service({
    url: '/order/ChangerOrderCreateChannelId',
    method: 'post',
    params: { ...params }
  })
}
