npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@bams-app/permission

v0.2.5

Published

角色权限管理插件包,提供指令、组件和函数三种方式进行权限控制。权限数据统一从 `@bams-app/store` 的用户 store 中获取。

Readme

@bams-app/permission

角色权限管理插件包,提供指令、组件和函数三种方式进行权限控制。权限数据统一从 @bams-app/store 的用户 store 中获取。

安装

在项目入口文件(如 main.js)中安装插件:

import { createApp } from "vue";
import permission from "@bams-app/permission";

const app = createApp(App);

// 全局安装指令 v-permission 和组件 <AuthPermission />
app.use(permission);

app.mount("#app");

功能特性

1. 指令方式 (推荐)

通过 v-permission 指令控制元素的显示与隐藏。如果用户没有指定权限,该元素将从 DOM 中移除。

<!-- 单个权限码 -->
<el-button v-permission="'meter:export'">导出</el-button>

<!-- 多个权限码(满足其中一个即可) -->
<el-button v-permission="['meter:export', 'meter:delete']">操作</el-button>

2. 组件方式

使用 <AuthPermission /> 组件包裹需要权限控制的内容。

<AuthPermission code="meter:delete">
  <el-button>删除</el-button>
</AuthPermission>

<!-- 多个权限码 -->
<AuthPermission :code="['meter:delete', 'meter:edit']">
  <el-button>编辑</el-button>
</AuthPermission>

3. JS 函数判断

在逻辑代码中使用 hasPerm 函数进行判断。

<script setup>
  import { hasPerm } from "@bams-app/permission";

  if (hasPerm("meter:config")) {
    // 执行有权限时的逻辑
    console.log("用户拥有配置权限");
  }
</script>

权限数据来源

本插件依赖于 @bams-app/store 中的 permission store 和 user store。

  • 权限字段: permissionStore.permissionMap (对象类型,按菜单 menuCode 分组,值为后端返回的 authCode 列表,如 { organizationManagement: ["bxjc-bd:organizationManagement:editor_code"] })
  • 缓存隔离: permission store 按应用编码持久化,key 为 [bams-app]-permission-${appCode},同源 iframe 应用之间不会互相覆盖
  • 超级管理员: 如果 userStore.role === 'admin',hasPerm 将始终返回 true。

权限码层级说明

权限码支持多级嵌套结构,采用冒号(:)分隔,格式为 模块:子模块:操作,支持通配符(*)匹配。

层级规则

  • 模块级授权:meter - 匹配 meter 模块下的所有权限
  • 操作级授权:meter:export - 可匹配 meter:export,也可兼容更细粒度权限如 meter:list:export
  • 细粒度权限:meter:list:export - 表示 meter 模块下 list 场景的 export 操作
  • 通配符授权:meter:*、system:user:* - 匹配对应前缀下的所有权限

权限匹配逻辑

权限判断支持以下几类规则:

  1. 精确匹配:meter:export 命中 meter:export
  2. 模块级匹配:meter 可匹配 meter:view、meter:list:export
  3. 向上兼容:meter:export 可匹配 meter:list:export
  4. 通配符匹配:meter:*、system:user:* 可匹配对应前缀下所有权限

示例

以下为权限码层级匹配的通用示例。实际 permissionMap 的 key 是菜单 menuCode,value 是后端返回的 authCode(如 bxjc-bd:meter:view)。

// permissionMap 配置示例
{
  "meter": [
    "meter:view",
    "meter:export",
    "meter:delete"
  ],
  "systemUser": [
    "system:user:*",       // 匹配 system:user 下所有操作
    "system:role:view"     // 仅匹配 system:role:view
  ]
}

// hasPerm 判断示例
hasPerm("meter:view")           // true - 精确匹配
hasPerm("meter:list:view")      // true - 向上兼容匹配到 meter:view
hasPerm("meter:list:export")    // true - 可被 meter 或 meter:export 命中
hasPerm("system:user:add")      // true - 通配符匹配 system:user:*
hasPerm("system:user:delete")   // true - 通配符匹配 system:user:*
hasPerm("system:role:edit")     // false - 仅配置了 system:role:view

API

AuthPermission Props

| 参数 | 说明 | 类型 | 默认值 | | ---- | ------------------ | -------------------- | ------ | | code | 权限码或权限码数组 | string \| string[] | - |

hasPerm Function

/**
 * @param {string | string[]} value - 权限码或权限码数组
 * @returns {boolean} - 是否拥有权限
 */
function hasPerm(value): boolean