permission.ts 915 B

12345678910111213141516171819202122232425262728293031323334
  1. import { ObjectDirective } from 'vue';
  2. import { usePermission } from '@/hooks/web/usePermission';
  3. /** 支持v-permission直接传入string, string[],以及兼容原来的方法 */
  4. export const permission: ObjectDirective = {
  5. mounted(el: HTMLButtonElement, binding) {
  6. const val = binding.value;
  7. if (val === undefined) return;
  8. let permissionVal: string[] = [];
  9. if (typeof val === 'string') {
  10. permissionVal = [val];
  11. } else if (val instanceof Array) {
  12. permissionVal = val;
  13. }
  14. const { hasPermission } = usePermission();
  15. if (!hasPermission(permissionVal)) {
  16. el.remove();
  17. return;
  18. }
  19. const { action, effect } = val;
  20. if (!hasPermission(action)) {
  21. if (effect == 'disabled') {
  22. el.disabled = true;
  23. el.style['disabled'] = 'disabled';
  24. el.classList.add('is-disabled');
  25. } else {
  26. el.remove();
  27. }
  28. }
  29. },
  30. };