From 26cfe3e8a62eb8a948fe365defd4d14eb30aaff5 Mon Sep 17 00:00:00 2001 From: red-deng-deng <1924913374@qq.com> Date: Mon, 29 Nov 2021 14:37:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3token=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/assets/api.js | 51 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) 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)); }