From e915b9b595ebd61b972052727c2682da9ede4881 Mon Sep 17 00:00:00 2001 From: Aresn Date: Wed, 29 May 2024 11:32:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85=E5=85=A8=E5=B1=80status?= =?UTF-8?q?=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composables/useRequest.js | 22 +--------------------- composables/useRequestError.js | 23 +++++++++++++++++++++++ utils/fetchEventSource.js | 5 ++--- 3 files changed, 26 insertions(+), 24 deletions(-) create mode 100644 composables/useRequestError.js diff --git a/composables/useRequest.js b/composables/useRequest.js index ce672c7..d3864a2 100644 --- a/composables/useRequest.js +++ b/composables/useRequest.js @@ -20,27 +20,7 @@ const request = async (url, options = {}) => { }, onResponseError({ request, response, options }) { const status = response.status - if (process.client && (status === 401 || status === 403)) { - // 全局弹提示 - let title - if (status === 401) title = '抱歉,您尚未登录' - else if (status === 403) title = '抱歉,您没有权限' - nextTick(() => { - const toast = useToast() - toast.add({ - icon: 'i-heroicons-exclamation-triangle-20-solid', - timeout: 3000, - title: title, - color: 'red' - }) - }) - // 全局弹登录 - const { $isSignIn } = storeToRefs(useUserStore()) - const { $openSign } = useUserStore() - if (status === 401 && !$isSignIn.value) { - $openSign() - } - } + useRequestError(status) // 处理响应错误 console.log('[ResponseError]', request) } diff --git a/composables/useRequestError.js b/composables/useRequestError.js new file mode 100644 index 0000000..81057a8 --- /dev/null +++ b/composables/useRequestError.js @@ -0,0 +1,23 @@ +export default function (status) { + if (process.client && (status === 401 || status === 403)) { + // 全局弹提示 + let title + if (status === 401) title = '抱歉,您尚未登录' + else if (status === 403) title = '抱歉,您没有权限' + nextTick(() => { + const toast = useToast() + toast.add({ + icon: 'i-heroicons-exclamation-triangle-20-solid', + timeout: 3000, + title: title, + color: 'red' + }) + }) + // 全局弹登录 + const { $isSignIn } = storeToRefs(useUserStore()) + const { $openSign } = useUserStore() + if (status === 401 && !$isSignIn.value) { + $openSign() + } + } +} \ No newline at end of file diff --git a/utils/fetchEventSource.js b/utils/fetchEventSource.js index 5fc567a..71add05 100644 --- a/utils/fetchEventSource.js +++ b/utils/fetchEventSource.js @@ -11,9 +11,8 @@ export default (url, { onmessage, onerror, controller, params }) => { }, body: JSON.stringify(params), onopen (response) { - if (response.status === 401 || response.status === 403) { - throw Error() - } + const status = response.status + useRequestError(status) }, onmessage (info) { onmessage(info) -- GitLab