index.js 1.02 KB
import store from "@/store";
// 权限渲染
export const permission = {
  inserted: function (el, binding) {
    const { value } = binding;
    const roles = store.getters["userInfo"].id;
    if (value && value instanceof Array && value.length > 0) {
      const permissionRoles = value;
      const hasPermission = permissionRoles.includes(roles);
      if (!hasPermission) {
        el.remove();
      }
    } else {
      throw new Error(`need roles! Like v-permission="['admin','editor']"`);
    }
  },
};

// 按钮鉴权
export const hasPermi = {
  inserted: function (el, binding) {
    const { value } = binding;
    const permissions = store.getters["permissions"];

    if (value && value instanceof Array && value.length > 0) {
      const permissionFlag = value;

      const hasPermissions = permissions.some((permission) => {
        return permissionFlag.includes(permission);
      });

      if (!hasPermissions) {
        el.remove();
      }
    } else {
      throw new Error(`请设置操作权限标签值`);
    }
  },
};