From 093ef6cb8a06af7cb3bed77455b7f1639d6f7f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A0=81=E5=86=9C=E5=B0=8F=E6=98=93?= <237972113@qq.com> Date: Sun, 1 May 2022 18:43:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ehas=E7=9A=84directive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-ui/src/directive/index.js | 2 + ruoyi-ui/src/directive/permission/has.js | 50 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 ruoyi-ui/src/directive/permission/has.js diff --git a/ruoyi-ui/src/directive/index.js b/ruoyi-ui/src/directive/index.js index b2901d7e2..dcb0dee11 100644 --- a/ruoyi-ui/src/directive/index.js +++ b/ruoyi-ui/src/directive/index.js @@ -1,3 +1,4 @@ +import has from './permission/has' import hasRole from './permission/hasRole' import hasPermi from './permission/hasPermi' import dialogDrag from './dialog/drag' @@ -6,6 +7,7 @@ import dialogDragHeight from './dialog/dragHeight' import clipboard from './module/clipboard' const install = function(Vue) { + Vue.directive('has', hasRole) Vue.directive('hasRole', hasRole) Vue.directive('hasPermi', hasPermi) Vue.directive('clipboard', clipboard) diff --git a/ruoyi-ui/src/directive/permission/has.js b/ruoyi-ui/src/directive/permission/has.js new file mode 100644 index 000000000..cb3c14536 --- /dev/null +++ b/ruoyi-ui/src/directive/permission/has.js @@ -0,0 +1,50 @@ +/** +* v-has 操作权限处理 +* Copyright (c) 2022 码农小易 +*/ + +import store from '@/store' + +export default { + inserted(el, binding, vnode) { + const { value } = binding; + const all_permission = "*:*:*"; + const super_admin = "admin"; + + const permissions = store.getters && store.getters.permissions + const roles = store.getters && store.getters.roles + + const R = /R\((.*?)\)/g; + const Rs = value.match(R) || []; + + const P = /P\((.*?)\)/g; + const Ps = value.match(P) || []; + + if (Ps.length === 0 || Rs.length === 0){ + throw new Error(`请设置操作权限标签值`); + } + + var expression = value; + + for (let i = 0; i < Rs.length; i++) { + const hasRole = roles.some(role => { + return super_admin === role || Rs[i] === `R(${role})`; + }) + + expression = hasRole ? expression.replace(Rs[i], 'true') : expression.replace(Rs[i], 'false'); + } + + for (let i = 0; i < Ps.length; i++) { + const hasPermi = permissions.some(permission => { + return all_permission === permission || Ps[i] === `P(${permission})`; + }) + + expression = hasPermi ? expression.replace(Ps[i], 'true') : expression.replace(Ps[i], 'false'); + } + + if (!eval(expression)) { + el.parentNode && el.parentNode.removeChild(el); + } + + } +}