import axios from 'axios'
import openNotification from '../utils/notification'
import { message } from 'ant-design-vue'
import router from 'vue-router'
const service = axios.create({
  baseURL: '/api',
  timeout: 120000
})

service.interceptors.request.use((config) => {
  const webConfig = config

  return webConfig
})
service.interceptors.response.use(
  (response) => {
    const res = response.data

    if (res.errCode !== 0) {
      if (res.ERR_TOKEN == 2 || res.errmsg == '校验token过期') {
        setTimeout(() => {
          // router.push({ path: '/login' })
        }, 1000)
      } else {
        console.log(res.errmsg, 'res.errmsg')
        // message.error(res.errmsg)
        // openNotification({
        //   status: 'error',
        //   desc: res.tip
        // })
      }
    } else {
      message.error(res.errmsg)
    }
    return res
  },
  (error) => {
    if (
      error.name === 'AxiosError' &&
      error.message === 'timeout of 120000ms exceeded' &&
      error.code === 'ECONNABORTED'
    ) {
      return error
    }
  }
)

export default service
