From a953f850569fa44ec826ff097d750a6310b5344a Mon Sep 17 00:00:00 2001 From: RubyLiu Date: Tue, 4 Jul 2023 14:58:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=99=BB=E5=87=BA=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/api/http/checkStatus.ts | 7 +++---- frontend/src/hooks/useUser.ts | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/frontend/src/api/http/checkStatus.ts b/frontend/src/api/http/checkStatus.ts index 56ab023da1..ff3d808a60 100644 --- a/frontend/src/api/http/checkStatus.ts +++ b/frontend/src/api/http/checkStatus.ts @@ -1,11 +1,11 @@ import { Message, Modal } from '@arco-design/web-vue'; import { useI18n } from '@/hooks/useI18n'; -import { useRouter } from 'vue-router'; - import type { ErrorMessageMode } from '#/axios'; +import useUser from '@/hooks/useUser'; export default function checkStatus(status: number, msg: string, errorMessageMode: ErrorMessageMode = 'message'): void { const { t } = useI18n(); + const { logout } = useUser(); let errMessage = ''; switch (status) { case 400: @@ -13,8 +13,7 @@ export default function checkStatus(status: number, msg: string, errorMessageMod break; case 401: { errMessage = msg || t('api.errMsg401'); - const router = useRouter(); - router.push('/login'); + logout(); break; } case 403: diff --git a/frontend/src/hooks/useUser.ts b/frontend/src/hooks/useUser.ts index b91726304e..bb8edd7afd 100644 --- a/frontend/src/hooks/useUser.ts +++ b/frontend/src/hooks/useUser.ts @@ -3,24 +3,24 @@ import { Message } from '@arco-design/web-vue'; import { useUserStore } from '@/store'; import { useI18n } from '@/hooks/useI18n'; -/** - * 用户相关 - * @returns 调用方法 - */ export default function useUser() { const router = useRouter(); const userStore = useUserStore(); const { t } = useI18n(); - /** - * 登出 - * @param logoutTo 登出后跳转的页面 - * @returns - */ - const logout = async () => { + + const logout = async (logoutTo?: string) => { await userStore.logout(); + const currentRoute = router.currentRoute.value; Message.success(t('message.logoutSuccess')); - router.push({ name: 'login' }); + router.push({ + name: logoutTo && typeof logoutTo === 'string' ? logoutTo : 'login', + query: { + ...router.currentRoute.value.query, + redirect: currentRoute.name as string, + }, + }); }; + return { logout, };