当前位置:   article > 正文

axios 拦截错误_axios错误拦截

axios错误拦截
/**axios封装
 * 请求拦截、相应拦截、错误统一处理
 */
import axios from 'axios'
import { MessageBox } from 'element-ui'

import 'core-js';
import promiseFinally from 'core-js/features/promise/finally';
if(!window.Promise.prototype['finally']){
    window.Promise.prototype['finally'] = promiseFinally;
}

const axiosService = axios.create({
	baseURL: '/api/manage', 			// url = base url + request url
	headers: {
		'Cache-Control': 'no-cache, no-store',
		'Pragma': 'no-cache'
    },
	timeout: 6 * 60 * 1000		// 超时时间
})

// request interceptor
axiosService.interceptors.request.use(
	config => {
		const separator = config.url.indexOf('?') === -1 ? '?' : '&';
		config.url =  config.url + separator +"noCache="+new Date().getTime();//防止浏览器缓存
		return config
	},
	error => {
		return Promise.reject(error)
	}
);


let messageInstance = null;
const resetMessage = (params) => {
    if(messageInstance) {
        if(messageInstance.close) messageInstance.close()
    }
    messageInstance = MessageBox({
		title: params.title,
		type: params.type,
		message: params.message,
		confirmButtonText: '确定',
		callback: () => {
			if(params.callback){
				console.log(params);
				params.callback()
			}
		}
	})
}

// response
axiosService.interceptors.response.use(
	response => {
		const data = response.data					
		if(data.code == 410){	//未登录,跳转登录页
			resetMessage({
				title:'登录超时',
				type:'warning',
				message: data.msg,
				callback: () => {
					window.history.pushState({},'','/login');
				}
			})
            return data;
		} else if(data.code == 401){		//无权限
			window.history.pushState({},'','/401');
            return data;
		} else if(data.code !== 200){
			resetMessage({
				title:'错误',
				type:'error',
				message: data.msg,
				callback: () => {

				}				
			})						
			return Promise.reject(new Error(data.msg))
		} else {				
            return data;
		}
	},
	error => {
		resetMessage({
			title:'错误',
			type:'error',
			message: error.message,
			callback: () => {
				// window.history.pushState({},'','/login');
			}
		})
		return Promise.reject(error);
	}
)


const axiosPromiseFinally = require('promise.prototype.finally');
axiosPromiseFinally.shim();

export default axiosService
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/57088
推荐阅读
相关标签
  

闽ICP备14008679号