Files
tms-erp-web/src/permission.js
T
2026-09-16 06:30:30 +08:00

87 lines
2.5 KiB
JavaScript

import router from './router/';
import store from './store';
import { tabKeyOf } from '@/router/tab';
import { getToken } from '@/utils/auth';
import {
consumeReloadQuery,
hasReloadQuery,
isChunkLoadError,
reloadForChunkError,
setPendingRoutePath,
} from '@/utils/chunk-reload';
import { ElMessage } from 'element-plus';
import NProgress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css'; // progress bar style
NProgress.configure({ showSpinner: false });
const lockPage = '/lock'; //锁屏页
// 懒加载 chunk / CSS 失败时整页刷新,避免菜单点击后卡死
router.onError(error => {
if (!isChunkLoadError(error)) return;
if (reloadForChunkError()) {
ElMessage.warning('页面资源已更新,正在重新加载…');
}
});
router.beforeEach((to, from, next) => {
if (hasReloadQuery(to.query)) {
next({
path: to.path,
query: consumeReloadQuery(to.query),
hash: to.hash,
replace: true,
});
return;
}
setPendingRoutePath(to.fullPath);
const meta = to.meta || {};
const isMenu = meta.menu === undefined ? to.query.menu : meta.menu;
store.commit('SET_IS_MENU', isMenu === undefined);
if (getToken()) {
if (store.getters.isLock && to.path !== lockPage) {
//如果系统激活锁屏,全部跳转到锁屏页
next({ path: lockPage });
} else if (to.path === '/login') {
//如果登录成功访问登录页跳转到主页
next({ path: '/' });
} else {
if (store.getters.token.length === 0) {
store.dispatch('FedLogOut').then(() => {
next({ path: '/login' });
});
} else {
const meta = to.meta || {};
const query = to.query || {};
if (meta.target) {
window.open(query.url.replace(/#/g, '&'));
return;
} else if (meta.isTab !== false) {
store.commit('ADD_TAG', {
name: query.name || to.name,
path: to.path,
fullPath: tabKeyOf(to),
params: to.params,
query: to.query,
meta: { ...meta, keepAlive: true },
});
}
next();
}
}
} else {
//判断是否需要认证,没有登录访问去登录页
if (meta.isAuth === false) {
next();
} else {
next('/login');
}
}
});
router.afterEach(to => {
NProgress.done();
let title = router.$avueRouter.generateTitle(to, { label: 'name' });
router.$avueRouter.setTitle(title);
store.commit('SET_IS_SEARCH', false);
});