hasPermi.ts 867 B

1234567891011121314151617181920212223242526
  1. import { Session } from '@/utils/storage';
  2. import { CACHE_KEY } from '@/constants/index';
  3. /**
  4. * 权限校验指令
  5. * 使用方法:v-hasPermi="['system:user:add']"
  6. */
  7. export default {
  8. mounted(el: HTMLElement, binding: any) {
  9. const { value } = binding;
  10. const perms: string[] = Session.get(CACHE_KEY.PERMS) || [];
  11. if (value && value instanceof Array && value.length > 0) {
  12. const hasPermissions = perms.some(permission => {
  13. return (value as string[]).includes(permission);
  14. });
  15. if (!hasPermissions) {
  16. // 如果没有权限,则移除元素
  17. el.parentNode && el.parentNode.removeChild(el);
  18. }
  19. } else {
  20. console.error(`[v-hasPermi]: 请设置操作权限标签值, 如 v-hasPermi="['system:user:add']"`);
  21. }
  22. }
  23. };