/** axios封装
 * 请求拦截、相应拦截、错误统一处理
 */
const httpRequest = axios.create({
  baseURL: 'http://192.168.2.144:8080', // 测试环境
  timeout: 3 * 60 * 1000, // 3分钟等待
  withCredentials: true, // 携带cookie
})

// 请求拦截器
httpRequest.interceptors.request.use(
  function (config) {
    if(config.data.access_token){
      config.headers['accessToken'] = config.data.access_token
    }
    // if (!isLocalEnv()) {
    //   if (getQueryVariable('token')) {
    //     config.headers['access_token'] = getQueryVariable('token')
    //   }
    // } else {
    //   config.headers['access_token'] = localStorage.getItem('token')
    //   // config.headers['access_token'] = '4b3ed521-f509-48d9-b907-ab127479df41'
    // }
    // // 上传文件
    // if (config.url == '/apiSys/oss/upload') {
    //   config.headers['Content-Type'] = 'multipart/form-data'
    // } else {
    //   config.headers['Content-Type'] = 'application/json'
    // }
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)

//响应拦截
httpRequest.interceptors.response.use(
  function (response) {
    const res = response.data
    // if (res.code == 201) {
    //   this.ELEMENT.Message.error(res.msg)
    //   if (!isLocalEnv()) {
    //     // 跳登录页
    //     window.location = window.location.origin
    //   } else {
    //     localEnvLogin()
    //   }
    // }
    return response
  },
  function (error) {
    this.ELEMENT.Message.error(error.message)
    return Promise.reject(error)
  }
)

function get(url, params) {
  return new Promise((resolve, reject) => {
    httpRequest
      .get(url, { params: params })
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  })
}
// qs.stringify(data)
function post(url, data, config) {
  return new Promise((resolve, reject) => {
    httpRequest
      .post(url, data, config)
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

function postFile(url, blob) {
  return new Promise((resolve, reject) => {
    let formdata = new FormData()
    formdata.append('multipartFile', blob)
    httpRequest
      .post(url, formdata)
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

function localEnvLogin() {
  post(
    '/apiSys/login',
    {
      username: 'admin',
      password: '1q2w3e4r5t',
    },
    {
      transformRequest: [
        function (data, headers) {
          // 将请求数据转换成功 formdata 接收格式
          headers['Content-Type'] = 'application/x-www-form-urlencoded'
          return stringify(data)
        },
      ],
    }
  ).then(res => {
    localStorage.setItem('token', res.data.token || getQueryVariable('token'))
  })
}

// 将参数转换成功 formdata 接收格式
function stringify(data) {
  let ret = ''
  for (const it in data) {
    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  }
  ret = ret.substring(0, ret.lastIndexOf('&'))
  return ret
}

// 格式化日期
function formatDate(time) {
  let date = time ? new Date(Number(time)) : new Date();
  let Y = date.getFullYear() + '-';
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
  let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
  let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
}