修改分模块

This commit is contained in:
2021-12-22 16:13:55 +08:00
parent bb6925f3a2
commit 14bf2bd17e
8 changed files with 81 additions and 173 deletions

View File

@@ -1,7 +1,7 @@
/*
* @Date: 2020-12-14 09:07:03
* @LastEditors: gtz
* @LastEditTime: 2021-01-29 14:24:20
* @LastEditors: zwq
* @LastEditTime: 2021-12-22 11:38:46
* @FilePath: \basic-admin\src\store\getters.js
* @Description:
*/
@@ -19,7 +19,6 @@ const getters = {
introduction: state => state.user.introduction,
username: state => state.user.username,
roles: state => state.user.roles,
menus: state => state.user.menus,
permission_routes: state => state.permission.routes
}
export default getters

View File

@@ -1,36 +1,38 @@
import { dynamicRoutes, constantRoutes } from '@/router'
import { getListItems } from '@/utils/tree'
/*
* @Author: zwq
* @Date: 2021-09-18 16:09:08
* @LastEditors: zwq
* @LastEditTime: 2021-12-22 15:43:46
* @Description:
*/
import { asyncRoutes, constantRoutes } from '@/router'
/**
* Use meta.role to determine if the current user has permission
* @param roles
* @param route
*/
function hasPermission(menuList, route) {
// if (route.meta && route.meta.roles) {
// return roles.some(role => route.meta.roles.includes(role))
// } else {
// return true
// }
if (route.hidden) {
function hasPermission(roles, route) {
if (route.meta && route.meta.roles) {
return roles.some(role => route.meta.roles.includes(role))
} else {
return true
}
return menuList.some(m => route.path === m.h)
}
/**
* Filter asynchronous routing tables by recursion
* @param routes asyncRoutes
* @param menuList
* @param roles
*/
export function filterAsyncRoutes(routes, menuList) {
export function filterAsyncRoutes(routes, roles) {
const res = []
routes.forEach(route => {
const tmp = { ...route }
if (hasPermission(menuList, tmp)) {
if (hasPermission(roles, tmp)) {
if (tmp.children) {
tmp.children = filterAsyncRoutes(tmp.children, menuList)
tmp.children = filterAsyncRoutes(tmp.children, roles)
}
res.push(tmp)
}
@@ -52,19 +54,13 @@ const mutations = {
}
const actions = {
generateRoutes({ commit }, menus) {
generateRoutes({ commit }, roles) {
return new Promise(resolve => {
let accessedRoutes
console.log(menus)
if (!menus) {
accessedRoutes = []
if (roles.includes('admin')) {
accessedRoutes = asyncRoutes || []
} else {
const menuList = []
getListItems(menus, node => menuList.push(node))
console.log(menuList)
console.log(dynamicRoutes)
accessedRoutes = filterAsyncRoutes(dynamicRoutes, menuList)
console.log(accessedRoutes)
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
}
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)

View File

@@ -9,7 +9,6 @@ const state = {
avatar: '',
introduction: '',
roles: [],
menus: [],
username: Cookies.get('username') || ''
}
@@ -26,9 +25,6 @@ const mutations = {
SET_AVATAR: (state, avatar) => {
state.avatar = avatar
},
SET_MENUS: (state, menus) => {
state.menus = menus
},
SET_ROLES: (state, roles) => {
state.roles = roles
},
@@ -70,17 +66,16 @@ const actions = {
reject('Verification failed, please Login again.')
}
const { roles, name, avatar, introduction, menus } = data
const { roles, name, avatar, introduction } = data
// roles must be a non-empty array
if (!menus || menus.length <= 0) {
reject('getInfo: menus must be a non-null array!')
if (!roles || roles.length <= 0) {
reject('getInfo: roles must be a non-null array!')
}
commit('SET_ROLES', roles || [])
commit('SET_ROLES', roles)
commit('SET_NAME', name)
commit('SET_AVATAR', avatar)
commit('SET_MENUS', menus)
commit('SET_INTRODUCTION', introduction)
resolve(data)
}).catch(error => {
@@ -97,7 +92,6 @@ const actions = {
dispatch('tagsView/delAllViews', null, { root: true })
commit('SET_TOKEN', '')
commit('SET_ROLES', [])
commit('SET_MENUS', [])
commit('SET_USERNAME')
Cookies.set('username', '')
localStorage.clear()