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

104 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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";
NProgress.configure({ showSpinner: false }); // NProgress Configuration
const whiteList = ['/login', '/auth-redirect', '/register']; // no redirect whitelist
//每次路由变动,都会调用这个路由守卫
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();
console.log('88888',hasToken)
if (hasToken) {
//有token
// 且前往登录
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但是不去登录
// determine whether the user has obtained his permission roles through getInfo
// const hasRoles = userStore().roles && userStore().roles.length > 0;
const hasRoles = true;
// console.log('hasRoles=', hasRoles);
// 如果用角色
if (hasRoles) {
// 继续执行
const accessRoutes = await permissionStore().generateRoutes(['admin']);
accessRoutes.forEach(item => {
router.addRoute(item);
});
next();
} else {
// 没有角色
try {
// get user info
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const infoRes = await userStore().getInfo() as any; //获取角色,调用查询个人信息的接口
console.log('permissionuserinfor:',infoRes)
let roles:any = [];
if (infoRes.roles) {
roles = infoRes.roles;
}
roles = ['admin']
// 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 });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
// 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
/* 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();
});