import axios from 'axios'

// 创建axios实例
const request = axios.create({
  baseURL: '/shixian',
  timeout: 10000
})

// 请求拦截器
request.interceptors.request.use(
  config => {
    // 可以在这里添加token等请求头
    const token = localStorage.getItem('token')
    if (token) {
      config.headers['Authorization'] = token
    }
    // 添加用户类型到请求头
    const userType = localStorage.getItem('userType')
    if (userType) {
      config.headers['X-User-Type'] = userType
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
request.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    console.error('请求错误：', error)
    return Promise.reject(error)
  }
)

export default request
