import { loginIgnore } from '@/router/index'; import { getUserInfo } from '@/utils'; import { checkAuthorization } from '@/utils/requestUtil'; import NProgress from 'nprogress'; import { globalRoutes } from '@/router/config'; NProgress.configure({ showSpinner: false }); function hasAuthority(to) { const { path } = to; if (globalRoutes.find(m => m.path === path)) return true; const { menuList = [] } = getUserInfo(); // return !!menuList.find(i => i.menuUrl === path); return true; } /** * 进度条开始 * @param to * @param form * @param next */ const progressStart = (to, from, next) => { // start progress bar if (!NProgress.isStarted()) { NProgress.start(); } next(); }; /** * 登录守卫 * @param to * @param form * @param next * @param options */ const loginGuard = (to, from, next, options) => { const { message } = options; if (!loginIgnore.includes(to) && !checkAuthorization()) { message.warning('登录已失效,请重新登录'); next({ path: '/login' }); } else { next(); } }; /** * 权限守卫 * @param to * @param form * @param next * @param options */ const authorityGuard = (to, from, next, options) => { const { message } = options; if (!hasAuthority(to)) { message.warning(`对不起,您无权访问页面: ${to.fullPath},请联系管理员`); next({ path: '/403' }); } else { next(); } }; /** * 进度条结束 * @param to * @param form * @param options */ const progressDone = () => { // finish progress bar NProgress.done(); }; export default { beforeEach: [progressStart, loginGuard, authorityGuard], afterEach: [progressDone], };