import axios from 'axios'
import { reqXUrl } from '../utils/config'
const instance = axios.create({
  baseURL: reqXUrl,
  timeout: 400000,
  headers: {
  }
})

instance.defaults.withCredentials=false
// 添加请求拦截器
instance.interceptors.request.use(function (config) {
  let headers = {
    'loginDevice': 'localhost:8099',
    'Content-Type': 'application/json; charset=utf-8',
    'X-Requested-With': 'XMLHttpRequest'
  }

  if(document.getCookie("token")){
    Object.assign(headers, { "token": decodeURIComponent(document.getCookie("token")) })
  }
 
  return Object.assign({}, config, {
    headers
  })
}, function (error) {
  return Promise.reject(error)
})

/**
 * 拦截响应response，并做一些错误处理
 */
instance.interceptors.response.use((response) => {
  console.log("************:::",response)
  let res = response.data
  if (res.code == -1) {
    // ElMessage({
    //     message: res.msg,
    //     type: 'error'
    // })
    window.location.href = "/#/login"
    // router.push('/seller');
    return Promise.resolve(res)
  } else if ((res.code != undefined && res.code != 0) || (res.resultCode!=undefined && res.resultCode!=0)) {
    // alert(res.msg);
    // ElMessage({
    //   message: res.msg || res.resultMessage,
    //   type: 'error'
    // })
    return Promise.resolve(res)
    // return;
  }
  return res
}, (err, a, b) => {
  console.log(err, a, b)
  if (err && err.response) {
    switch (err.response) {
      case 400:
        err.message = '请求参数错误'
        break
      case 401:
        err.message = '未授权，请登录'
        break
      case 403:
        err.message = '跨域拒绝访问'
        break
      case 404:
        err.message = `请求地址出错: ${err.response.config.url}`
        break
      case 408:
        err.message = '请求超时'
        break
      case 500:
        err.message = '服务器内部错误'
        break
      case 501:
        err.message = '服务未实现'
        break
      case 502:
        err.message = '网关错误'
        break
      case 503:
        err.message = '服务不可用'
        break
      case 504:
        err.message = '网关超时'
        break
      case 505:
        err.message = 'HTTP版本不受支持'
        break
      default:
        break
    }
  }
  return Promise.reject(err)
})

export const request = (($i) => {
  return {
    get (url, params = {}) {
      return $i.get(url, {
        params
      })
    },
    post (url, options = {}) {
      return $i.post(url, options)
    },
    formPost (url, options = {}) {
      let formData = new FormData()
      for (let key in options) {
        formData.append(key, options[key])
      }
      return $i.post(url, formData, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
        }
      })
    },
    file (url, options = {}) {
      let formData = new FormData()
      for (let key in options) {
        formData.append(key, options[key])
      }
      return $i.post(url, formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
    },
    formData(url, params) {
      const formData = new FormData()
      formData.append('file', params)
      return $i.post(url, formData,{
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
    },
    delete (url, data = {}) {
      return $i.delete(url, { data })
    },
    put (url, options = {}) {
      return $i.put(url, options)
    }
  }
})(instance)
