auth.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { router } from '@/router'
  2. import { App, Directive, DirectiveBinding } from 'vue'
  3. import {useUserStore} from "@/store/modules/user";
  4. /**
  5. * 权限指令(后端控制模式可用)
  6. * 用法:
  7. * <el-button v-auth="123">按钮</el-button>
  8. */
  9. interface AuthBinding extends DirectiveBinding {
  10. value: number
  11. }
  12. function checkAuthPermission(el: HTMLElement, binding: AuthBinding): void {
  13. // 获取当前路由的权限列表
  14. // const authList = (router.currentRoute.value.meta.authList as Array<{ authMark: string }>) || []
  15. // 检查是否有对应的权限标识
  16. // const hasPermission = authList.some((item) => item.authMark === binding.value)
  17. // 如果没有权限,移除元素
  18. if (!useUserStore().checkAuth(binding.value)) {
  19. removeElement(el)
  20. }
  21. }
  22. function removeElement(el: HTMLElement): void {
  23. if (el.parentNode) {
  24. el.parentNode.removeChild(el)
  25. }
  26. }
  27. const authDirective: Directive = {
  28. mounted: checkAuthPermission,
  29. updated: checkAuthPermission
  30. }
  31. export function setupAuthDirective(app: App): void {
  32. app.directive('auth', authDirective)
  33. }