|
|
@@ -8,10 +8,10 @@ import { getToken } from '@/utils/auth' // getToken from cookie
|
|
|
NProgress.configure({ showSpinner: false })// NProgress Configuration
|
|
|
|
|
|
// permission judge function
|
|
|
-function hasPermission(roles, permissionRoles) {
|
|
|
- if (roles.indexOf('admin') >= 0) return true // admin permission passed directly
|
|
|
- if (!permissionRoles) return true
|
|
|
- return roles.some(role => permissionRoles.indexOf(role) >= 0)
|
|
|
+function hasPermission(perms, permissions) {
|
|
|
+ if (perms.indexOf('*') >= 0) return true // admin permission passed directly
|
|
|
+ if (!permissions) return true
|
|
|
+ return perms.some(perm => permissions.indexOf(perm) >= 0)
|
|
|
}
|
|
|
|
|
|
const whiteList = ['/login', '/auth-redirect']// no redirect whitelist
|
|
|
@@ -24,10 +24,10 @@ router.beforeEach((to, from, next) => {
|
|
|
next({ path: '/' })
|
|
|
NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
|
|
|
} else {
|
|
|
- if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
|
|
|
+ if (store.getters.perms.length === 0) { // 判断当前用户是否已拉取完user_info信息
|
|
|
store.dispatch('GetUserInfo').then(res => { // 拉取user_info
|
|
|
- const roles = res.data.data.roles // note: roles must be a array! such as: ['editor','develop']
|
|
|
- store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
|
|
|
+ const perms = res.data.data.perms // note: perms must be a array! such as: ['GET /aaa','POST /bbb']
|
|
|
+ store.dispatch('GenerateRoutes', { perms }).then(() => { // 根据perms权限生成可访问的路由表
|
|
|
router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
|
|
|
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
|
|
|
})
|
|
|
@@ -39,7 +39,7 @@ router.beforeEach((to, from, next) => {
|
|
|
})
|
|
|
} else {
|
|
|
// 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
|
|
|
- if (hasPermission(store.getters.roles, to.meta.roles)) {
|
|
|
+ if (hasPermission(store.getters.perms, to.meta.perms)) {
|
|
|
next()
|
|
|
} else {
|
|
|
next({ path: '/401', replace: true, query: { noGoBack: true }})
|