12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import Vue from 'vue';
- const rolesList = []
- const permission = (el, binding) => {
- const roles = getRoles();
- if (!roles) {
- return;
- }
- let userPermissionList = Array.isArray(binding.value) ? binding.value : [binding.value];
- // 当前用户的权限列表
- if (!userPermissionList.some(e => roles.includes(e))) {
- el.parentNode && el.parentNode.removeChild(el);
- }
- }
- const getRoles = ()=>{
- let _roles = uni.getStorageSync('jurisdiction');
- let list = uni.getStorageSync('rolesList');
- if(list&&list.length > 0){
- return list
- }
- else{
- list = getUserAllRoles(_roles);
- uni.setStorageSync("rolesList", list)
- return list
- }
-
- }
- const getUserAllRoles = (item) => {
- console.log(item)
- for(let i = 0;i<item.length;i++){
- rolesList.push(item[i].name)
- console.log('用户权限',rolesList)
- if(item[i].children&&item[i].children.length>0){
- getUserAllRoles(item[i].children)
- }
- }
- return rolesList;
- }
- Vue.directive('hasPermission', {
- inserted: permission,
- componentUpdated: permission
- })
|