import axios from 'axios'
import { getTokenCookies } from './auth'
import { Message } from 'element-ui'
const service = axios.create({
  baseURL: 'http://124.90.28.158:12433//api',
  timeout: 5000 // request timeout
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    const token = getTokenCookies()
    if (token) {
      config.headers.token = token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    if (response.data.code === 500) {
      Message({
        type: 'warning',
        message: response.data.msg
      })
    }
    if (response.data.code === 0 && response.data.msg) {
      Message({
        type: 'success',
        message: response.data.msg
      })
    }
    return response.data
  },
  error => {
    return Promise.reject(error)
  }
)

export default service
