- Vue.directive('permission', permission);
export const permission = {
inserted(el, binding) {
checkRolePermission(el, binding, 'permission');
}
};
function checkRolePermission(el, binding, type) {
const {value} = binding;
if (value && value instanceof Array && value.length > 0) {
const permissionRoles = value;
let hasPermission = false;
if (type === 'roles') {
hasPermission = hasRoles(...permissionRoles);
} else if (type === 'permission') {
hasPermission = hasPermissions(...permissionRoles);
}
if (!hasPermission) {
el.parentNode && el.parentNode.removeChild(el);
}
}
}
export function hasPermissions(...permissions) {
for (let p of permissions) {
if (hasPermission(p)) {
return true;
}
}
return false;
}
// 是否含有某个角色
export function hasRoles(...roles) {
let user = getCurrentUser();
let rs = user.roles.map(r => r.id);
for (let item of roles) {
if (rs.indexOf(item) > -1) {
return true;
}
}
return false;
}
- Vue.directive('roles', roles);
// 基本同*Vue.directive('permission', permission);
export const roles = {
inserted(el, binding) {
checkRolePermission(el, binding, 'roles');
}
};
- Vue.directive('xpack', xpack);
export const xpack = {
inserted(el, binding) {
checkLicense(el, binding);
}
};
function checkLicense(el, binding, type) {
let v = hasLicense();
if (!v) {
el.parentNode && el.parentNode.removeChild(el);
}
}
export function hasLicense() {
let v = localStorage.getItem(LicenseKey);
return v === 'valid';
}
- Vue.directive('modules', modules);
export const modules = {
inserted(el, binding) {
checkModule(el, binding);
}
};
function checkModule(el, binding) {
const {value} = binding;
if (value && value instanceof Array && value.length > 0) {
let v = enableModules(...value);
if (!v) {
el.parentNode && el.parentNode.removeChild(el);
}
}
}
export function enableModules(...modules) {
for (let module of modules) {
let moduleStatus = localStorage.getItem('module_' + module);
if (moduleStatus === 'DISABLE') {
return false;
}
}
return true;
}