directive.js 1018 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import Vue from 'vue';
  2. const rolesList = []
  3. const permission = (el, binding) => {
  4. const roles = getRoles();
  5. if (!roles) {
  6. return;
  7. }
  8. let userPermissionList = Array.isArray(binding.value) ? binding.value : [binding.value];
  9. // 当前用户的权限列表
  10. if (!userPermissionList.some(e => roles.includes(e))) {
  11. el.parentNode && el.parentNode.removeChild(el);
  12. }
  13. }
  14. const getRoles = ()=>{
  15. let _roles = uni.getStorageSync('jurisdiction');
  16. let list = uni.getStorageSync('rolesList');
  17. if(list&&list.length > 0){
  18. return list
  19. }
  20. else{
  21. list = getUserAllRoles(_roles);
  22. uni.setStorageSync("rolesList", list)
  23. return list
  24. }
  25. }
  26. const getUserAllRoles = (item) => {
  27. console.log(item)
  28. for(let i = 0;i<item.length;i++){
  29. rolesList.push(item[i].name)
  30. console.log('用户权限',rolesList)
  31. if(item[i].children&&item[i].children.length>0){
  32. getUserAllRoles(item[i].children)
  33. }
  34. }
  35. return rolesList;
  36. }
  37. Vue.directive('hasPermission', {
  38. inserted: permission,
  39. componentUpdated: permission
  40. })