1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /**
- * @description 判断当前路由是否包含权限
- * @param permissions
- * @param route
- * @returns {boolean|*}
- */
- function hasPermission(permissions, route) {
- if (route.meta && route.meta.permissions) {
- return permissions.some((role) => route.meta.permissions.includes(role));
- } else {
- return false;
- }
- }
- /**
- * @description intelligence模式根据permissions数组拦截路由
- * @param routes
- * @param permissions
- * @returns {[]}
- */
- export function filterAsyncRoutes(routes, permissions) {
- const finallyRoutes = [];
- routes.forEach((route) => {
- const item = { ...route };
- if (hasPermission(permissions, item)) {
- if (item.children) {
- item.children = filterAsyncRoutes(item.children, permissions);
- }
- finallyRoutes.push(item);
- }
- });
- finallyRoutes.forEach((route) => {
- if (route.children) {
- if (route.children[0].children) {
- route.redirect =
- route.path +
- "/" +
- route.children[0].path +
- "/" +
- route.children[0].children[0].path;
- } else {
- route.redirect = route.path + "/" + route.children[0].path;
- }
- route.children.forEach((croute) => {
- if (croute.children) {
- croute.redirect =
- route.path + "/" + croute.path + "/" + croute.children[0].path;
- croute.children.forEach((i) => {
- if (i.children) {
- i.redirect =
- route.path +
- "/" +
- croute.path +
- "/" +
- i.path +
- "/" +
- i.children[0].path;
- }
- });
- }
- });
- }
- });
- return finallyRoutes;
- }
- function hasRoute(i, arr) {
- if (i && arr.length) {
- return arr.some((a) => {
- if (a.path.includes(":")) {
- return i.path.includes(a.path.substring(0, a.path.indexOf("/:")));
- } else {
- return i.path.includes(a.path);
- }
- });
- } else {
- return false;
- }
- }
- export function fillterMenuRoutes(data, stateRoute) {
- let currData = [];
- data.forEach((menu) => {
- const item = { ...menu };
- if (hasRoute(item, stateRoute)) {
- if (item.children) {
- item.children = fillterMenuRoutes(item.children, stateRoute);
- }
- currData.push(item);
- }
- });
- return currData;
- }
|