Commit e97df29e by caoyanzhi

页面访问拦截和按钮操作拦截

parent aa83c3aa
import { request } from './request.js';
import { origin } from '@/config';
// 项目中配置的路由信息
let projectRoutes = [];
// 有权限访问的路由
let accessPath = [];
// 无权限的操作项
let itemPermission = {};
function getGicMenu() {
const getUserMenu = request({ url: '/hb-manage-web/list-menus-auth' }).then(res => {
accessPath = getProjectMenu(res.result || []);
});
const getUserOperation = request({ url: '/hb-manage-web/list-operation-auth' }).then(res => {
itemPermission = getItemPerm(res.result || []);
})
return Promise.all([getUserMenu, getUserOperation]);
}
function getProjectMenu(menu) {
let result = [];
menu.forEach(el => {
if (el.menuUrl) {
result.push(el.menuUrl);
}
if (Array.isArray(el.children)) {
result = result.concat(getProjectMenu(el.children));
}
})
return result;
}
function getItemPerm(itemPerms) {
let result = {};
itemPerms.forEach(el => {
result[el.menuCode] = true;
})
return result;
}
function flatRoutes(routes) {
let result = [];
routes.forEach(el => {
if (Array.isArray(el.children)) {
result = result.concat(flatRoutes(el.children));
}
result.push(el.path);
})
return result;
}
// 路由跳转拦截
function intercept(to, from, next) {
const hasPerm = accessPath.some(el => {
return el.indexOf('/#') >= 0 ? el == `/#${to.path}` : el == to.path;
})
// 有权限访问
if (hasPerm) {
return next();
};
// 无权限访问
if (projectRoutes.some(el => el == to.path) || accessPath.length == 0) {
window.location.href = `${origin}/damo-system/no-access`;
} else {
next(accessPath[0]);
}
}
// 操作项权限拦截(包括按钮、下拉列表选项、switch开关、复选框等等操作项)
function itemPerm(itemPermCode) {
return !!itemPermission[itemPermCode];
}
let permission = function(config) {
const { writeRoute, router, routes, createApp } = config;
window.onload = async function() {
// 获取菜单数据
// 获取权限数据
const path = window.location.pathname;
if (path.indexOf('/login') == -1) {
await getGicMenu();
}
if (Array.isArray(writeRoute)) {
accessPath = accessPath.concat(writeRoute);
}
if (Array.isArray(routes)) {
// 项目中路由文件配置的路径
projectRoutes = flatRoutes(routes);
}
router.beforeEach(intercept);
// 创建vue实例,渲染页面
if (typeof createApp == 'function') createApp();
};
}
export { permission, itemPerm }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment