Commit 9eff5a70 authored by smallwei's avatar smallwei

增加axios防止重复请求

parent 8b2e43fe
export default {
'000': '操作太频繁,请勿重复请求',
'401': '当前操作没有权限',
'403': '当前操作没有权限',
'404': '资源不存在',
......
......@@ -57,6 +57,7 @@ RouterPlugin.install = function(router, store) {
},
//动态路由
formatRoutes: function(aMenu, first) {
if (!aMenu) return;
const aRouter = []
const propsConfig = this.$website.menu.props;
const propsDefault = {
......@@ -71,7 +72,7 @@ RouterPlugin.install = function(router, store) {
name = oMenu[propsDefault.label],
icon = oMenu[propsDefault.icon],
children = oMenu[propsDefault.children];
if (component) {
const isChild = children.length !== 0;
const oRouter = {
path: path,
......@@ -112,7 +113,7 @@ RouterPlugin.install = function(router, store) {
})()
}
aRouter.push(oRouter)
}
})
return aRouter
}
......
......@@ -20,14 +20,21 @@ axios.defaults.timeout = 30000;
//跨域请求,允许保存cookie
axios.defaults.withCredentials = true;
NProgress.configure({ showSpinner: false }) // NProgress Configuration
//HTTPrequest拦截
const requestMap = new Map();
//HTTPrequest拦截
axios.interceptors.request.use(config => {
const keyString = JSON.stringify(Object.assign({}, { url: config.url, method: config.method }, config.data));
if (requestMap.get(keyString)) {
return Promise.reject('code:000')
}
requestMap.set(keyString, true);
config = Object.assign(config, { _keyString: keyString });
NProgress.start() // start progress bar
if (store.getters.access_token) {
config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
}
if (sessionStorage.getItem('tenantId')){
if (sessionStorage.getItem('tenantId')) {
config.headers['TENANT_ID'] = sessionStorage.getItem('tenantId') // 租户ID
}
return config
......@@ -36,9 +43,12 @@ axios.interceptors.request.use(config => {
return Promise.reject(error)
})
//HTTPresponse拦截
axios.interceptors.response.use(data => {
axios.interceptors.response.use(res => {
NProgress.done();
return data
// 重置requestMap
const config = Object.assign(res.config);
requestMap.set(config._keyString, false);
return res
}, error => {
NProgress.done()
let errMsg = error.toString()
......@@ -47,7 +57,7 @@ axios.interceptors.response.use(data => {
message: errorCode[code] || errorCode['default'],
type: 'error'
})
if(parseInt(code) === 401 || parseInt(code) === 403){
if (parseInt(code) === 401 || parseInt(code) === 403) {
store.dispatch('FedLogOut').then(() => {
router.push({ path: '/login' });
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment