2022-09-15 14:21:11 +08:00
|
|
|
|
import router from './router';
|
|
|
|
|
import userStore from './store/modules/user';
|
|
|
|
|
import permissionStore from './store/modules/permission';
|
|
|
|
|
import NProgress from 'nprogress'; // progress bar
|
|
|
|
|
import 'nprogress/nprogress.css'; // progress bar style
|
|
|
|
|
import { getToken } from '@/utils/auth'; // get token from cookie
|
|
|
|
|
import getPageTitle from '@/utils/get-page-title';
|
2024-04-15 20:37:53 +08:00
|
|
|
|
import {ElMessage} from "element-plus";
|
2022-09-15 14:21:11 +08:00
|
|
|
|
|
|
|
|
|
NProgress.configure({ showSpinner: false }); // NProgress Configuration
|
|
|
|
|
|
2024-04-15 20:37:53 +08:00
|
|
|
|
const whiteList = ['/login', '/auth-redirect', '/register']; // no redirect whitelist
|
2022-09-15 14:21:11 +08:00
|
|
|
|
|
2024-04-15 12:47:42 +08:00
|
|
|
|
//每次路由变动,都会调用这个路由守卫
|
2022-09-15 14:21:11 +08:00
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
|
|
console.log('router.beforeEach', to.path, from.path);
|
|
|
|
|
// start progress bar
|
|
|
|
|
NProgress.start();
|
|
|
|
|
|
|
|
|
|
// set page title
|
|
|
|
|
document.title = getPageTitle(to.meta.title);
|
|
|
|
|
|
|
|
|
|
// determine whether the user has logged in
|
|
|
|
|
const hasToken = getToken();
|
2024-04-13 14:47:41 +08:00
|
|
|
|
console.log('88888',hasToken)
|
2024-04-15 12:47:42 +08:00
|
|
|
|
|
2022-09-15 14:21:11 +08:00
|
|
|
|
if (hasToken) {
|
2024-04-15 12:47:42 +08:00
|
|
|
|
//有token
|
|
|
|
|
// 且前往登录
|
2022-09-15 14:21:11 +08:00
|
|
|
|
if (to.path === '/login') {
|
|
|
|
|
// if is logged in, redirect to the home page
|
|
|
|
|
NProgress.done(); // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
|
|
|
|
|
next({ path: '/' });
|
|
|
|
|
} else {
|
2024-04-15 12:47:42 +08:00
|
|
|
|
// 有token,但是不去登录
|
2022-09-15 14:21:11 +08:00
|
|
|
|
// determine whether the user has obtained his permission roles through getInfo
|
2024-04-13 14:47:41 +08:00
|
|
|
|
// const hasRoles = userStore().roles && userStore().roles.length > 0;
|
|
|
|
|
const hasRoles = true;
|
|
|
|
|
|
2022-09-15 14:21:11 +08:00
|
|
|
|
// console.log('hasRoles=', hasRoles);
|
2024-04-15 12:47:42 +08:00
|
|
|
|
// 如果用角色
|
2022-09-15 14:21:11 +08:00
|
|
|
|
if (hasRoles) {
|
2024-04-15 12:47:42 +08:00
|
|
|
|
// 继续执行
|
|
|
|
|
const accessRoutes = await permissionStore().generateRoutes(['admin']);
|
|
|
|
|
accessRoutes.forEach(item => {
|
|
|
|
|
router.addRoute(item);
|
|
|
|
|
});
|
2022-09-15 14:21:11 +08:00
|
|
|
|
next();
|
|
|
|
|
} else {
|
2024-04-15 12:47:42 +08:00
|
|
|
|
// 没有角色
|
2022-09-15 14:21:11 +08:00
|
|
|
|
try {
|
|
|
|
|
// get user info
|
|
|
|
|
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
|
2023-03-10 18:22:34 +08:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2024-04-15 12:47:42 +08:00
|
|
|
|
const infoRes = await userStore().getInfo() as any; //获取角色,调用查询个人信息的接口
|
2024-04-13 14:47:41 +08:00
|
|
|
|
console.log('permissionuserinfor:',infoRes)
|
2024-04-15 12:47:42 +08:00
|
|
|
|
let roles:any = [];
|
2023-03-10 18:22:34 +08:00
|
|
|
|
if (infoRes.roles) {
|
|
|
|
|
roles = infoRes.roles;
|
|
|
|
|
}
|
2024-04-15 12:47:42 +08:00
|
|
|
|
roles = ['admin']
|
2022-09-15 14:21:11 +08:00
|
|
|
|
// generate accessible routes map based on roles
|
|
|
|
|
const accessRoutes = await permissionStore().generateRoutes(roles);
|
|
|
|
|
// console.log('accessRoutes=', accessRoutes)
|
|
|
|
|
|
|
|
|
|
// dynamically add accessible routes
|
|
|
|
|
// router.addRoutes(accessRoutes);
|
|
|
|
|
accessRoutes.forEach(item => {
|
|
|
|
|
router.addRoute(item);
|
|
|
|
|
});
|
|
|
|
|
// console.log('next=', accessRoutes);
|
|
|
|
|
|
|
|
|
|
// hack method to ensure that addRoutes is complete
|
|
|
|
|
// set the replace: true, so the navigation will not leave a history record
|
|
|
|
|
next({ ...to, replace: true });
|
2023-03-10 18:22:34 +08:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
} catch (error: any) {
|
2022-09-15 14:21:11 +08:00
|
|
|
|
// remove token and go to login page to re-login
|
|
|
|
|
await userStore().resetToken();
|
|
|
|
|
ElMessage.error(error.message || 'Has Error');
|
|
|
|
|
NProgress.done();
|
|
|
|
|
next(`/login?redirect=${to.path}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-04-15 12:47:42 +08:00
|
|
|
|
// 没有token
|
2022-09-15 14:21:11 +08:00
|
|
|
|
/* has no token*/
|
|
|
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
|
|
|
// in the free login whitelist, go directly
|
|
|
|
|
next();
|
|
|
|
|
} else {
|
|
|
|
|
// other pages that do not have permission to access are redirected to the login page.
|
|
|
|
|
NProgress.done();
|
|
|
|
|
next(`/login?redirect=${to.path}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.afterEach(() => {
|
|
|
|
|
// finish progress bar
|
|
|
|
|
NProgress.done();
|
|
|
|
|
});
|