Commit 4ae0bb86 authored by 何远江's avatar 何远江

添加按钮权限指令

parent f01c34f6
import { unref } from 'vue';
import { useAuthStore } from '@/stores/modules/auth';
export default {
mounted(el, binding) {
const { value } = binding; // 获取指令的值,例如 v-permission="'create'"
const { authButtonListGet } = useAuthStore(); // 获取用户权限列表
// 如果 value 是数组,则检查是否包含所有权限
if (Array.isArray(value)) {
const hasPermission = value.every((perm) => authButtonListGet.includes(perm));
if (!hasPermission) {
el.parentNode && el.parentNode.removeChild(el);
}
} else if (!authButtonListGet.includes(value)) {
el.parentNode && el.parentNode.removeChild(el); // 从 DOM 中移除元素
}
},
};
import copy from '@/directives/copy'; import copy from '@/directives/copy';
import debounce from '@/directives/debounce'; import debounce from '@/directives/debounce';
import throttle from '@/directives/throttle'; import throttle from '@/directives/throttle';
import permission from '@/directives/permission';
export function setupDirectives(app) { export function setupDirectives(app) {
// 复制指令 // 复制指令
...@@ -9,4 +10,6 @@ export function setupDirectives(app) { ...@@ -9,4 +10,6 @@ export function setupDirectives(app) {
app.directive('debounce', debounce); app.directive('debounce', debounce);
// 节流指令 // 节流指令
app.directive('throttle', throttle); app.directive('throttle', throttle);
// 权限指令
app.directive('permission', permission);
} }
...@@ -7,7 +7,7 @@ export const useAuthStore = defineStore({ ...@@ -7,7 +7,7 @@ export const useAuthStore = defineStore({
id: 'admin-auth', id: 'admin-auth',
state: () => ({ state: () => ({
// 按钮权限列表 // 按钮权限列表
authButtonList: {}, authButtonList: [],
// 菜单权限列表 // 菜单权限列表
authMenuList: [], authMenuList: [],
// 当前页面的 router name,用来做按钮权限筛选 // 当前页面的 router name,用来做按钮权限筛选
......
...@@ -2,10 +2,14 @@ ...@@ -2,10 +2,14 @@
<div class="table-box"> <div class="table-box">
<ProTable ref="proTable" :config="config" :api="getUserPage"> <ProTable ref="proTable" :config="config" :api="getUserPage">
<template #left_buttons> <template #left_buttons>
<el-button type="primary" :icon="Plus" @click="accountFormModalRef?.openModal()" <el-button
v-permission="'AccountAdd'"
type="primary"
:icon="Plus"
@click="accountFormModalRef?.openModal()"
>新增</el-button >新增</el-button
> >
<el-button type="danger" :icon="Delete" @click="handleDelete">删除</el-button> <el-button v-permission="'AccountDelete'" type="danger" :icon="Delete" @click="handleDelete">删除</el-button>
</template> </template>
</ProTable> </ProTable>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment