44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
|
|
/*
|
|||
|
|
* @Author: 田鑫
|
|||
|
|
* @Date: 2023-03-05 14:46:43
|
|||
|
|
* @LastEditors: 田鑫
|
|||
|
|
* @LastEditTime: 2023-03-05 15:59:25
|
|||
|
|
* @Description: 路由登录状态守卫
|
|||
|
|
*/
|
|||
|
|
import type { Router, LocationQueryRaw } from 'vue-router';
|
|||
|
|
import NProgress from 'nprogress'; // progress bar
|
|||
|
|
|
|||
|
|
import { isLogin, clearAllLocalStorage } from '@/utils/auth';
|
|||
|
|
import { useUserStore } from '@/stores/modules/user';
|
|||
|
|
|
|||
|
|
export default function setupUserLoginInfoGuard(router: Router) {
|
|||
|
|
router.beforeEach(async (to, from, next) => {
|
|||
|
|
console.log('access login info router guard');
|
|||
|
|
NProgress.start();
|
|||
|
|
if (to.name === 'auth') {
|
|||
|
|
next();
|
|||
|
|
}
|
|||
|
|
const userStore = useUserStore();
|
|||
|
|
//* 判断用户是否登录,若登录则放过,进入下一步
|
|||
|
|
//* 若无,则清空所有缓存并弹回登录鉴权页
|
|||
|
|
if (isLogin()) {
|
|||
|
|
if (userStore.role) {
|
|||
|
|
next();
|
|||
|
|
} else {
|
|||
|
|
userStore.getUserInfo();
|
|||
|
|
next();
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
clearAllLocalStorage();
|
|||
|
|
// todo 跳转回登录鉴权页,当前为mock路由地址
|
|||
|
|
next({
|
|||
|
|
name: 'auth',
|
|||
|
|
query: {
|
|||
|
|
redirect: to.name,
|
|||
|
|
...to.query,
|
|||
|
|
} as LocationQueryRaw,
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|