diff --git a/src/assets/api.js b/src/assets/api.js index d524b01c..c2efb829 100644 --- a/src/assets/api.js +++ b/src/assets/api.js @@ -11,7 +11,29 @@ const upload = (method, url, params, responseType) => { body: params, } let http = fetch(url, obj).then(res =>{ - return res.json() + if(res.status==200){ + return res.json() + }else{ + switch (res.status) { + // 401: 未登录 + case 401: + res.json().then(response =>{ + if(response.code==403){ + Notify.clear(); + Notify.error(response.message); + setTimeout(() => { + window.location.href='/login'; + }, 2000) + }else if(response.code==401){ + Notify.clear(); + Notify.error(response.message); + } + } + ).catch(); + + break; + } + } } ); return http.then(response => response).catch(error => console.error('Error:', error)); } @@ -36,7 +58,29 @@ const upload = (method, url, params, responseType) => { } } let http = fetch(url, obj).then(res =>{ - return res.blob() + if(res.status==200){ + return res.blob() + }else{ + switch (res.status) { + // 401: 未登录 + case 401: + res.json().then(response =>{ + if(response.code==403){ + Notify.clear(); + Notify.error(response.message); + setTimeout(() => { + window.location.href='/login'; + }, 2000) + }else if(response.code==401){ + Notify.clear(); + Notify.error(response.message); + } + } + ).catch(); + break; + } + } + } ); return http.then(response => response).catch(error => console.error('Error:', error)); } @@ -110,9 +154,6 @@ const req = (method, url, params, responseType) => { break; } } - - console.log(res); - } ); return http.then(response =>response).catch(error => console.error('Error:', error)); }