customInstructions.js 1.1 KB

123456789101112131415161718192021222324252627282930
  1. // 权限控制
  2. const PermissionDirective = { // 数组, 权限 code 和 布尔值,
  3. updated(el, binding, vnode ) {
  4. const routePath = vnode.ctx.appContext.config.globalProperties.$route.path;
  5. const userInfo = JSON.parse(sessionStorage.getItem('userInfo') || '');
  6. const authorityCodes = (userInfo?.userInfo?.functionList || []).map(({ code }) => code);
  7. const permissions = binding.value;
  8. const hasPermission = (binding.value || []).filter(item => typeof item !== 'boolean');
  9. if (!Array.isArray(permissions)) {
  10. console.error('权限必须以数组形式提供');
  11. return;
  12. }
  13. if (permissions.some((element) => element === true)) {
  14. return;
  15. }
  16. if (!hasPermission.every(permission => authorityCodes.includes(permission))) {
  17. el.parentNode && el.parentNode.removeChild(el);
  18. }
  19. },
  20. };
  21. // 导出的自定义指令
  22. const customize = [
  23. { key: 'permission', directive: PermissionDirective, name: '角色权限' }
  24. ]
  25. export default customize;