import axios from 'axios'
// import { useUserStore } from '../store/user'
import { layer } from '@layui/layui-vue'
import router from '../router'

const config = {
  timeout: 5000,
  // baseURL: import.meta.env.VITE_APP_BASE_API
  baseURL: window.location.origin
}

class Http {
  service
  constructor (config) {
    this.service = axios.create(config)

    /* 请求拦截 */
    this.service.interceptors.request.use(
      (config) => {
        // const userInfoStore = useUserStore();
        // if (userInfoStore.token) {
        //     (config.headers as AxiosRequestHeaders).token = userInfoStore.token as string
        // } else {
        //     if(router.currentRoute.value.path!=='/login') {
        //         router.push('/login');
        //     }
        // }

        return config
      },
      error => {
        return Promise.reject(error)
      }
    )

    /* 响应拦截 */
    this.service.interceptors.response.use(
      (response) => {
        switch (response.data.code) {
          case 200:
            return response.data
          case 400:
            return response.data
            // eslint-disable-next-line no-unreachable
            break
          case 401:
            return response.data
            // eslint-disable-next-line no-unreachable
            break
          case 403:
            return response.data
            // eslint-disable-next-line no-unreachable
            break
          case 404:
            return response.data
            // eslint-disable-next-line no-unreachable
            break
          case 408:
            return response.data
            // eslint-disable-next-line no-unreachable
            break
          case 500:
            return response.data
            // eslint-disable-next-line no-unreachable
            break
          case 501:
            return response.data
            // eslint-disable-next-line no-unreachable
            break
          case 502:
            return response.data
            // eslint-disable-next-line no-unreachable
            break
          case 503:
            return response.data
            // eslint-disable-next-line no-unreachable
            break
          case 504:
            return response.data
            // eslint-disable-next-line no-unreachable
            break
          case 505:
            return response.data
            // eslint-disable-next-line no-unreachable
            break
          case 99998:
            layer.confirm('会话超时, 请重新登录', {
              icon: 2,
              yes: function () {
                router.push('/login')
                layer.closeAll()
              }
            })
            return response.data
          default:
            break
        }
        if (!response.data.code) {
          // eslint-disable-next-line prefer-promise-reject-errors
          return Promise.reject({
            msg: '请求json失败',
            code: '404',
            data: '请求json失败'
          })
        }
      },
      error => {
        return Promise.reject(error)
      }
    )
  }

  /**
   * GET 方法
   * @param url
   * @param [params]
   * @param [_object]
   */
  get (url, params, _object = {}) {
    return this.service.get(url, { params, ..._object })
  }

  /**
   * POST 方法
   * @param url
   * @param [params]
   * @param [_object]
   */
  post (url, params, _object = {}) {
    return this.service.post(url, params, _object)
  }

  /**
   * PUT 方法
   * @param url
   * @param [params]
   * @param [_object]
   */
  put (url, params, _object = {}) {
    return this.service.put(url, params, _object)
  }

  /**
   * DELETE 方法
   * @param url
   * @param [params]
   * @param [_object]
   */
  delete (url, params, _object = {}) {
    return this.service.delete(url, { params, ..._object })
  }
}

export default new Http(config)
