Jsl-OA-Web-Re/src/permission.ts

104 lines
3.6 KiB
TypeScript
Raw Normal View History

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';
import {ElMessage} from "element-plus";
2022-09-15 14:21:11 +08:00
NProgress.configure({ showSpinner: false }); // NProgress Configuration
const whiteList = ['/login', '/auth-redirect', '/register']; // no redirect whitelist
2022-09-15 14:21:11 +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)
2022-09-15 14:21:11 +08:00
if (hasToken) {
//有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 {
// 有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);
// 如果用角色
2022-09-15 14:21:11 +08:00
if (hasRoles) {
// 继续执行
const accessRoutes = await permissionStore().generateRoutes(['admin']);
accessRoutes.forEach(item => {
router.addRoute(item);
});
2022-09-15 14:21:11 +08:00
next();
} else {
// 没有角色
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
const infoRes = await userStore().getInfo() as any; //获取角色,调用查询个人信息的接口
2024-04-13 14:47:41 +08:00
console.log('permissionuserinfor:',infoRes)
let roles:any = [];
2023-03-10 18:22:34 +08:00
if (infoRes.roles) {
roles = infoRes.roles;
}
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 {
// 没有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();
});