import axios from 'axios'

const http = axios.create({
  baseURL: '/api',
  timeout: 10000
})

http.interceptors.request.use((config) => {
  const token = localStorage.getItem('token')
  if (token) {
    config.headers['X-Token'] = token
  }
  return config
})

http.interceptors.response.use((resp) => {
  const data = resp.data
  if (data.code !== 0) {
    return Promise.reject(new Error(data.message || '请求失败'))
  }
  return data.data
})

export default http
