refactor: 登出业务逻辑优化
This commit is contained in:
parent
e70b48162b
commit
a953f85056
|
@ -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:
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue