From 978422df937459777d58d35d2eaf0069a4c10dfc Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 20 Jan 2024 10:55:40 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20request=EF=BC=9A=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=EF=BC=8C=E5=A2=9E=E5=8A=A0=20401=20=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E8=B6=85=E6=97=B6=E7=9A=84=E8=87=AA=E5=8A=A8=E5=BC=B9?= =?UTF-8?q?=E7=AA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sheep/request/index.js | 48 ++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/sheep/request/index.js b/sheep/request/index.js index c8dddcf..e1a1de8 100644 --- a/sheep/request/index.js +++ b/sheep/request/index.js @@ -73,10 +73,13 @@ const http = new Request({ */ http.interceptors.request.use( (config) => { + // 自定义处理【auth 授权】:必须登录的接口,则跳出 AuthModal 登录弹窗 if (config.custom.auth && !$store('user').isLogin) { showAuthModal(); return Promise.reject(); } + + // 自定义处理【loading 加载中】:如果需要显示 loading,则显示 loading if (config.custom.showLoading) { LoadingInstance.count++; LoadingInstance.count === 1 && @@ -88,6 +91,8 @@ http.interceptors.request.use( }, }); } + + // 增加 token 令牌、terminal 终端、tenant 租户的请求头 const token = uni.getStorageSync('token'); if (token) config.header['Authorization'] = token; // TODO 芋艿:特殊处理 @@ -114,28 +119,37 @@ http.interceptors.response.use( $store('user').setToken(response.data.data.accessToken); } + // 自定处理【loading 加载中】:如果需要显示 loading,则关闭 loading response.config.custom.showLoading && closeLoading(); - if (response.data.error !== 0 && response.data.code !== 0) { - if (response.config.custom.showError) + + // 自定义处理【error 错误提示】:如果需要显示错误提示,则显示错误提示 + if (response.data.code !== 0) { + // 特殊:如果 401 错误码,则跳转到登录页 or 刷新令牌 + if (response.data.code === 401) { + handleAuthorized(); + } + + // 错误提示 + if (response.config.custom.showError) { uni.showToast({ title: response.data.msg || '服务器开小差啦,请稍后再试~', icon: 'none', mask: true, }); - return Promise.resolve(response.data); + } } - // 成功时的提示 - if ( - (response.data.error === 0 || response.data.code === 0) && - (response.data.msg !== '' || response.config.custom.successMsg !== '') && - response.config.custom.showSuccess - ) { + + // 自定义处理【showSuccess 成功提示】:如果需要显示成功提示,则显示成功提示 + if (response.config.custom.showSuccess + && response.config.custom.successMsg !== '' + && response.data.code === 0) { uni.showToast({ title: response.config.custom.successMsg, icon: 'none', }); } - // TODO 芋艿:全局的 error code 处理;例如说 401 + + // 返回结果:包括 code + data + msg return Promise.resolve(response.data); }, (error) => { @@ -153,8 +167,7 @@ http.interceptors.response.use( } else { errorMessage = '请先登录'; } - userStore.logout(true); - showAuthModal(); + handleAuthorized() break; case 403: errorMessage = '拒绝访问'; @@ -184,7 +197,7 @@ http.interceptors.response.use( errorMessage = '网络超时'; break; case 505: - errorMessage = 'HTTP版本不受支持'; + errorMessage = 'HTTP 版本不受支持'; break; } if (error.errMsg.includes('timeout')) errorMessage = '请求超时'; @@ -209,6 +222,15 @@ http.interceptors.response.use( }, ); +/** + * 处理 401 未登录的错误 + */ +const handleAuthorized = () => { + const userStore = $store('user'); + userStore.logout(true); + showAuthModal(); +} + const request = (config) => { if (config.url[0] !== '/') { config.url = apiPath + config.url;