Skip to content

Commit

Permalink
🐛 fix(auth): support multilevel route for util.getRouterObjByName
Browse files Browse the repository at this point in the history
- refactor router.beforeEach
- add redirect from access-test to 403 page
  • Loading branch information
Baoyx007 committed Nov 23, 2017
1 parent e16c977 commit bbc279c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 34 deletions.
38 changes: 15 additions & 23 deletions src/libs/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,21 @@ util.showThisRoute = function (itAccess, currentAccess) {
};

util.getRouterObjByName = function (routers, name) {
let routerObj = {};
routers.forEach(item => {
if (item.name === 'otherRouter') {
item.children.forEach((child, i) => {
if (child.name === name) {
routerObj = item.children[i];
}
});
} else {
if (item.children.length === 1) {
if (item.children[0].name === name) {
routerObj = item.children[0];
}
} else {
item.children.forEach((child, i) => {
if (child.name === name) {
routerObj = item.children[i];
}
});
}
if (!name || !routers || !routers.length) {
return null;
}
// debugger;
let routerObj = null;
for (let item of routers) {
if (item.name === name) {
return item;
}
});
return routerObj;
routerObj = util.getRouterObjByName(item.children, name);
if (routerObj) {
return routerObj;
}
}
return null;
};

util.handleTitle = function (vm, item) {
Expand Down Expand Up @@ -237,7 +229,7 @@ util.toDefaultPage = function (routers, name, route, next) {
let i = 0;
let notHandle = true;
while (i < len) {
if (routers[i].name === name && routers[i].redirect === undefined) {
if (routers[i].name === name && routers[i].children && routers[i].redirect === undefined) {
route.replace({
name: routers[i].children[0].name
});
Expand Down
11 changes: 6 additions & 5 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ router.beforeEach((to, from, next) => {
name: 'home_index'
});
} else {
if (Util.getRouterObjByName([otherRouter, ...appRouter], to.name).access !== undefined) { // 判断用户是否有权限访问当前页
if (Util.getRouterObjByName([otherRouter, ...appRouter], to.name).access === parseInt(Cookies.get('access'))) {
const curRouterObj = Util.getRouterObjByName([otherRouter, ...appRouter], to.name);
if (curRouterObj && curRouterObj.access !== undefined) { // 需要判断权限的路由
if (curRouterObj.access === parseInt(Cookies.get('access'))) {
Util.toDefaultPage([otherRouter, ...appRouter], to.name, router, next); // 如果在地址栏输入的是一级菜单则默认打开其第一个二级菜单的页面
} else {
next({
replace: true,
name: 'error_403'
name: 'error-403'
});
}
} else {
Util.toDefaultPage([otherRouter, ...appRouter], to.name, router, next);
} else { // 没有配置权限的路由, 直接通过
Util.toDefaultPage([...routers], to.name, router, next);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const appRouter = [
access: 0,
component: Main,
children: [
{ path: 'index', title: '权限测试页', name: 'accesstest_index' }
{ path: 'index', title: '权限测试页', name: 'accesstest_index', access: 0, component: resolve => { require(['@/views/access/access-test.vue'], resolve); } }
]
},
{
Expand Down
11 changes: 6 additions & 5 deletions src/views/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,12 @@
// console.log(val)
},
beforePush (name) {
if (name === 'accesstest_index') {
return false;
} else {
return true;
}
// if (name === 'accesstest_index') {
// return false;
// } else {
// return true;
// }
return true;
},
fullscreenChange (isFullScreen) {
// console.log(isFullScreen);
Expand Down
17 changes: 17 additions & 0 deletions src/views/access/access-test.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<template>
<div>
<Card shadow>
<p>当前用户的权限值是 0 时,才可以看到这个页面。</p>
</Card>
</div>
</template>

<script>
export default {
};
</script>

<style>
</style>

0 comments on commit bbc279c

Please sign in to comment.