4 changed files with 67 additions and 1 deletions
@ -0,0 +1,9 @@ |
|||
/** |
|||
* Configure and register global directives |
|||
*/ |
|||
import type { App } from 'vue'; |
|||
import { setupPermissionDirective } from './permission'; |
|||
|
|||
export function setupGlobDirectives(app: App) { |
|||
setupPermissionDirective(app); |
|||
} |
@ -0,0 +1,48 @@ |
|||
/** |
|||
* Global authority directive |
|||
* Used for fine-grained control of component permissions |
|||
* @Example v-auth="name" |
|||
*/ |
|||
import type { App, Directive, DirectiveBinding } from 'vue'; |
|||
import { authorizationService } from '/nerv-lib/saas/store/modules/authorization-service'; |
|||
|
|||
function isAuth(el: Element, binding: any) { |
|||
console.log(el, binding); |
|||
|
|||
const { checkPermission, checkAllPermission, checkPermissionRouter } = authorizationService(); |
|||
const { value, modifiers } = binding; |
|||
|
|||
if (!value) return; |
|||
//操作按钮
|
|||
if (modifiers.op) { |
|||
if (!checkPermission(value)) { |
|||
el.parentNode?.removeChild(el); |
|||
} |
|||
} |
|||
// 全部
|
|||
if (modifiers.all) { |
|||
if (!checkAllPermission(value)) { |
|||
el.parentNode?.removeChild(el); |
|||
} |
|||
} |
|||
// 路由
|
|||
if (modifiers.route) { |
|||
if (!checkPermissionRouter(value)) { |
|||
el.parentNode?.removeChild(el); |
|||
} |
|||
} |
|||
} |
|||
|
|||
const mounted = (el: Element, binding: DirectiveBinding<string | string[]>) => { |
|||
isAuth(el, binding); |
|||
}; |
|||
|
|||
const authDirective: Directive = { |
|||
mounted, |
|||
}; |
|||
|
|||
export function setupPermissionDirective(app: App) { |
|||
app.directive('auth', authDirective); |
|||
} |
|||
|
|||
export default authDirective; |
Loading…
Reference in new issue