/**
 * Created by Zhang Haijun on 2017/8/24.
 * axios#request(config)
 * axios#get(url[, config])
 * axios#delete(url[, config])
 * axios#head(url[, config])
 * axios#options(url[, config])
 * axios#post(url[, data[, config]])
 * axios#put(url[, data[, config]])
 * axios#patch(url[, data[, config]])
 */
import axios from 'axios'
import NProgress from 'nprogress'
import qs from 'qs'
import 'nprogress/nprogress.css'
import { getToken, setToken } from 'utils/auth'
import store from '@/store'
import { ElNotification } from 'element-plus'
import 'element-plus/es/components/notification/style/css'

const codeMessage = {
  200: '服务器成功返回请求的数据。',
  201: '新建或修改数据成功。',
  202: '一个请求已经进入后台排队（异步任务）。',
  204: '删除数据成功。',
  400: '发出的请求有错误，服务器没有进行新建或修改数据的操作。',
  401: '用户没有权限（令牌、用户名、密码错误）。',
  403: '用户得到授权，但是访问是被禁止的。',
  404: '发出的请求针对的是不存在的记录，服务器没有进行操作。',
  406: '请求的格式不可得。',
  410: '请求的资源被永久删除，且不会再得到的。',
  422: '当创建一个对象时，发生一个验证错误。',
  500: '服务器发生错误，请检查服务器。',
  502: '网关错误。',
  503: '服务不可用，服务器暂时过载或维护。',
  504: '网关超时。'
}
const axiosInstance = axios.create({
  baseURL: '/api',
  headers: { 'Content-Type': 'application/json', BsmAjaxHeader: true },
  timeout: 20000,
  paramsSerializer: params => qs.stringify(params, { arrayFormat: 'indices' })
})
// 请求完成回调
const finishCallback = function () {
  NProgress.done()
}
// 报错处理
const handleError = function (response) {
  if (!response) return // 容错处理
  let title = `请求错误 ${response.status}: ${response.config.url}`
  let errorText = codeMessage[response.status] || response.statusText
  const { data } = response
  if (typeof data?.message == 'string') {
    title = data.message
    errorText = ''
  }
  Notification({
    type: 'error',
    title,
    message: errorText
  })
  const error = new Error(errorText)
  error.name = response.status
  error.response = response
  throw error
}
axiosInstance.interceptors.request.use(
  config => {
    const {
      headers,
      headers: { options = {} }
    } = config
    NProgress.start()
    if (config.method === 'get') {
      // 清除get缓存
      config.url = `${config.url}?t=${new Date().getTime()}`
    } else if (headers['Content-Type'] === 'application/x-www-form-urlencoded') {
      config.data = qs.stringify(config.data || {})
    }
    config.headers.token = getToken()
    delete config.headers.options
    config.options = options
    return config
  },
  error => Promise.reject(error)
)
axiosInstance.interceptors.response.use(
  data => {
    // const requestKey = getRequestIdentify(data.config);
    // removePending(requestKey);
    finishCallback()
    const {
      data: responseData,
      config: { options },
      headers: { token }
    } = data
    if (token) setToken(token)
    if (!responseData.success) {
      switch (responseData.status) {
        case '402':
          store.dispatch('permission/ResetRoutes')
          window.location.href = '/license'
          break
        case '401':
        case '509':
          store.dispatch('permission/ResetRoutes')
          break
        default:
      }
      if (!options.ignoreError) {
        ElNotification.error({
          message: responseData.message || responseData.data
        })
      }
    }
    return responseData
  },
  error => {
    finishCallback()
    handleError(error.response)
    return Promise.reject(error)
  }
)
export default axiosInstance
