import axios from 'axios'
import { ElMessage } from 'element-plus'

const httpRequest = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API,
  timeout: 60000,
  withCredentials: true,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})

// 请求拦截
httpRequest.interceptors.request.use(
  function (config) {
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)

// 响应拦截
httpRequest.interceptors.response.use(
  function (response) {
    return response.data
  },
  function (error) {
    if (error.response) {
      const { status, message } = error.response.data
      if (status === 400) {
        ElMessage.error(message)
      }
    } else {
      ElMessage.error(error)
    }
    return Promise.reject(error)
  }
)

export default httpRequest
