Vue 权限菜单及按钮权限

一.服务端数据

Vue 权限菜单需要根据后端返回的数据来实现

[
{pid:-1,name:'购物车',id:1,auth:'cart'},
{pid:1,name:'购物车列表',id:4,auth:'cart-list'},
{pid:4,name:'彩票',id:5,auth:'lottery'},
{pid:4,name:'商品',id:6,auth:'product'},
{pid:-1,name:'商店',id:2,auth:'shop'},
{pid:-1,name:'个人中心',id:3,auth:'store'},
];

通过 express 返回权限列表

const express = require('express');

const app = express();
app.all('_', (req, res, next) => {
res.header('Access-Control-Allow-Origin', '_');
// Access-Control-Allow-Headers ,可根据浏览器的 F12 查看,把对应的粘贴在这里就行
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods', '\*');
res.header('Content-Type', 'application/json;charset=utf-8');
next();
});
app.get('/roleAuth', (req, res) => {
res.json({
menuList: [
{pid:-1,name:'购物车',id:1,auth:'cart'},
{pid:1,name:'购物车列表',id:4,auth:'cart-list'},
{pid:4,name:'彩票',id:5,auth:'lottery'},
{pid:4,name:'商品',id:6,auth:'product'},
{pid:-1,name:'商店',id:2,auth:'shop'},
{pid:-1,name:'个人中心',id:3,auth:'store'},
]
});
});
app.listen(3000);

二.静态菜单

使用 element-ui 构建静态菜单

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);





选项 1-1-1
选项 1-1-2

选项 1-2


导航二


导航三


导航四


路由配置

import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'

Vue.use(Router)
export const authRoutes = [ // 权限路由
{
path: '/cart',
name: 'cart',
component: () => import('@/views/Cart'),
children: [
{
path: 'cart-list',
name: 'cart-list',
component: () => import('@/views/CartList'),
children: [
{
path: 'lottery',
name: 'lottery',
component: () => import('@/views/Lottery'),
},
{
path: 'product',
name: 'product',
component: () => import('@/views/Product'),
},
],
},
],
},
];
export default new Router({ // 默认导出 首页和 404 页面
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/',
name: 'home',
component: Home
},
{
path:'*',
component:{
render:h=>h('h1',{},'Not Found')
}
}
]
})

三.获取权限

根据后端返回的数据,格式化树结构,并提取用户权限

// 默认设置没有获取过权限
export default new Vuex.Store({
state: {
hasPermission:false
},
mutations: {
setPermission(state){
state.hasPermission = true
}
},
})

在路由跳转前看下是否获取过权限,如果没有获取过,就获取权限存入 vuex 中

router.beforeEach(async (to,from,next)=>{
if(!store.state.hasPermission){
// 获取最新路由列表
let newRoutes = await store.dispatch('getRouteList');
router.addRoutes(newRoutes); // 增加新路由
next({...to,replace:true})
}else{
next(); // 获取过就不需要再次获取了
}
})

四.获取相关需要数据

const getMenListAndAuth = (menuList)=>{
let menu = [];
let sourceMap = {};
let auth = [];
menuList.forEach(m => {
m.children = []; // 增加孩子列表
sourceMap[m.id] = m;
auth.push(m.auth)
if(m.pid === -1){
menu.push(m); // 根节点
}else{
sourceMap[m.pid] && sourceMap[m.pid].children.push(m)
}
});
return {menu,auth} // 获取菜单数据和权限数据
}
async getRouteList({dispatch,commit}){
let auths = await axios.get('http://localhost:3000/roleAuth');
let menuList = auths.data.menuList;
let {menu,auth} = getMenListAndAuth(menuList);
}

五.找到需要添加的路由

import {authRoutes} from './router'
const getRoutes = auth => {
const filter = (authRoutes)=>{
return authRoutes.filter(route=>{
// 包含权限
if(auth.includes(route.name)){
if(route.children){
route.children = filter(route.children);
}
return true;
}
})
}
return filter(authRoutes);
};
// 获取需要添加的路由列表
async getRouteList({ dispatch, commit }) {
let auths = await axios.get("http://localhost:3000/roleAuth");
let menuList = auths.data.menuList;
let { menu, auth } = getMenListAndAuth(menuList);
commit("setMenu", menu); // 将菜单数据保存起来
commit("setPermission"); // 权限获取完毕
// 通过 auth 查找需要添加的路由
return getRoutes(auth);
}

六.递归渲染菜单

渲染 Menu 组件提取公共部分

编写递归组件


七.权限按钮控制

state: {
    hasPermission: false,
    menu: [], // 菜单权限
    btnPermission:{ // 按钮权限
        edit:false,
        add:true
    }
},

查看当前按钮是否有权限

编辑
添加

自定义指令的使用

directives: {
has: {
inserted(el, bindings, vnode) {
let value = bindings.value;
// 在 vuex 中查看是否有按钮权限
let flag = vnode.context.\$store.state.btnPermission[value];
// 如果没有全选则将按钮删除即可
!flag && el.parentNode.removeChild(el);
}
}
}

你可能感兴趣的:(vue.js)