refactor: 登出业务逻辑优化

This commit is contained in:
RubyLiu 2023-07-04 14:58:07 +08:00 committed by 刘瑞斌
parent e70b48162b
commit a953f85056
2 changed files with 14 additions and 15 deletions

View File

@ -1,11 +1,11 @@
import { Message, Modal } from '@arco-design/web-vue'; import { Message, Modal } from '@arco-design/web-vue';
import { useI18n } from '@/hooks/useI18n'; import { useI18n } from '@/hooks/useI18n';
import { useRouter } from 'vue-router';
import type { ErrorMessageMode } from '#/axios'; import type { ErrorMessageMode } from '#/axios';
import useUser from '@/hooks/useUser';
export default function checkStatus(status: number, msg: string, errorMessageMode: ErrorMessageMode = 'message'): void { export default function checkStatus(status: number, msg: string, errorMessageMode: ErrorMessageMode = 'message'): void {
const { t } = useI18n(); const { t } = useI18n();
const { logout } = useUser();
let errMessage = ''; let errMessage = '';
switch (status) { switch (status) {
case 400: case 400:
@ -13,8 +13,7 @@ export default function checkStatus(status: number, msg: string, errorMessageMod
break; break;
case 401: { case 401: {
errMessage = msg || t('api.errMsg401'); errMessage = msg || t('api.errMsg401');
const router = useRouter(); logout();
router.push('/login');
break; break;
} }
case 403: case 403:

View File

@ -3,24 +3,24 @@ import { Message } from '@arco-design/web-vue';
import { useUserStore } from '@/store'; import { useUserStore } from '@/store';
import { useI18n } from '@/hooks/useI18n'; import { useI18n } from '@/hooks/useI18n';
/**
*
* @returns
*/
export default function useUser() { export default function useUser() {
const router = useRouter(); const router = useRouter();
const userStore = useUserStore(); const userStore = useUserStore();
const { t } = useI18n(); const { t } = useI18n();
/**
* const logout = async (logoutTo?: string) => {
* @param logoutTo
* @returns
*/
const logout = async () => {
await userStore.logout(); await userStore.logout();
const currentRoute = router.currentRoute.value;
Message.success(t('message.logoutSuccess')); 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 { return {
logout, logout,
}; };