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 { 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:

View File

@ -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,
};