vue拦截请求添加loading

var queue = {}
axios.create({
timeout: 60000
})
axios.defaults.baseURL = baseUrl
// request interceptor
axios.interceptors.request.use(config => {
queue[config.url] = config.url;
const token = sessionStorage.getItem(‘Access-Token’)
if (token) {
config.headers[‘Access-Token’] = token
}
// const imgKey = sessionStorage.getItem(‘imgKey’)
// if(imgKey){
// config.headers[‘Captcha-Key’] = imgKey;
// config.headers[‘Captcha-Code’] = sessionStorage.getItem(‘imgCode’);
// sessionStorage.removeItem(‘imgKey’)
// sessionStorage.removeItem(‘imgCode’)
// }
return config
}, (err) => {
console.error(err)
Message.error(‘请求异常,’ + err)
})
axios.interceptors.response.use((res) => {
if (res.status === 200) {

const responseData = res.data
if (responseData.returnCode) {
  if (responseData.returnCode == '000000') {
    return responseData
  } else {
    Message.closeAll()
    Message.error('服务器处理异常,Error:' + responseData.returnMsg)
  }
  return false
}
if (responseData.code === 200) {
	delete queue[res.url]
	if(Object.keys(queue).length){
		//关闭loading
	}
  return responseData
} else if (responseData.code === 401) {
  sessionStorage.removeItem('Access-Token')
  sessionStorage.clear();
  router.push({ path: '/login' })
  Message.closeAll()
  Message.error('登录已失效,请重新登录')
} else {
  console.log(res.data.msg)
  const ms = responseData.message ? responseData.message : res.data.msg
  Message.closeAll()
  Message.error('服务器处理异常,Error:' + ms)
}

} else {
Message.closeAll()
Message.error(‘服务器通信异常,Error:’ + res.message || res.msg)
}
}, (err) => {
Message.closeAll()
Message.error(‘服务器异常,’ + err)
})

你可能感兴趣的:(vue)