httpinterceptor.js 1.0 KB
Newer Older
!阳仔's avatar
!阳仔 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
import axios from 'axios'
let axiosSignature = axios.create()
initAxios(axiosSignature, true)
function initAxios (axiosObj, isNeedSignature = false) {
    // axios 配置
    axiosObj.defaults.timeout = 3000
    // http response 拦截器
    axiosObj.interceptors.response.use(
      response => {
        return response
      },
      error => {
        var errorMsg = ''
        if (error.response) {
          if (error.response.data && error.response.data.code && error.response.data.message) {
            errorMsg = error.response.data && error.response.data.code && error.response.data.message
            return Promise.reject(error.response.data)
          } else {
            errorMsg =
              error.response.status + ' ' + error.response.statusText || '系统异常,请稍后再试'
          }
        } else {
          errorMsg = '服务超时,请稍后重试'
        }
        let errorData = { code: 0, message: errorMsg }
        return Promise.reject(errorData)
      }
    )
  }
  export default axiosSignature