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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@dronephon/access

v1.0.0

Published

基于 @fesjs/plugin-access 改造的权限插件

Readme

@dronephon/access

基于 @fesjs/plugin-access 改造的权限插件

介绍

对于前端应用来说,权限就是页面、页面元素是否可见。

资源

把页面、页面元素统一叫做资源,用资源 ID 来识别区分他们:

  • 页面的资源 ID 默认是页面的路由 path 。比如页面 pages/a.vue 的路由 path/a。当页面访问 /a 时会渲染当前页面,/a 也就是页面的 accessId
  • 页面元素的资源 ID 没有默认值,需要自定义。
<template>
    <access :id="accessId"> accessOnepicess1 </access>
    <div v-access="accessId">accessOnepicess2</div>
</template>
<script>
export default {
    setup() {
        return {
            accessId: 'accessOnepicess',
        };
    },
};
</script>

匹配规则

全等匹配

资源的匹配规则默认是使用全等匹配,比如页面 pages/a.vue 对应路由 path/a,则 /a 就是页面的资源 ID。如果我们设置:

access.setAccess(['/a']);

由于权限列表中包含/a,则表示拥有此页面权限。

模糊匹配

页面@id.vue会映射为动态路由/:id,想匹配此页面有两种办法:

  • access.setAccess(['/:id'])
  • access.setAccess(['/*'])

第二种是模糊匹配,*表示任意路径。比如角色admin需要全部权限,则可以:

export default {
    roles: {
        admin: ['*'],
    },
};

角色

通常我们会用角色来控制权限,相应的用角色定义一组资源。当访问应用时,使用插件提供的 API 设置用户的角色,角色对应的资源才可见,非角色对应的资源不可见。

当然有时候业务比较复杂,角色对应的权限是动态的。不要怕!插件提供粒度更细的 API 来设置当前用户能访问的资源。

使用方式

package.json 中引入依赖:

{
    "dependencies": {
        "@fronephon/access": "^0.1.0"
    }
}

roles

  • 类型:对象

  • 默认值{}

  • 详情

    角色预定义列表。key 是角色 Id ,value是角色 Id 对应的资源列表。

运行时配置

unAccessHandler

  • 类型Function

  • 默认值null

  • 详情

    当进入某个路由时,如果路由对应的页面不属于可见资源列表,则会暂停进入,调用 unAccessHandler 函数。

  • 参数

    • router:createRouter 创建的路由实例
    • to: 准备进入的路由
    • from:离开的路由
    • next: next 函数

比如:

export const access = {
    unAccessHandler({ to, next }) {
        const accesssIds = accessApi.getAccess();
        if (to.path === '/404') {
            accessApi.setAccess(accesssIds.concat(['/404']));
            return next('/404');
        }
        if (!accesssIds.includes('/403')) {
            accessApi.setAccess(accesssIds.concat(['/403']));
        }
        next('/403');
    },
};

noFoundHandler

  • 类型Function

  • 默认值null

  • 详情

    当进入某个路由时,如果路由对应的页面不存在,则会调用 noFoundHandler 函数。

  • 参数

    • router:createRouter 创建的路由实例
    • to: 准备进入的路由
    • from:离开的路由
    • next: next 函数

比如:

export const access = {
    noFoundHandler({ next }) {
        const accesssIds = accessApi.getAccess();
        if (!accesssIds.includes('/404')) {
            accessApi.setAccess(accesssIds.concat(['/404']));
        }
        next('/404');
    },
};

ignoreAccess

  • 类型Array<string>

  • 默认值null

  • 详情

    配置需要忽略权限校验的页面。

比如:

export const access = {
    ignoreAccess: ['/login'],
};

API

access

import { access } from '@dronephon/access';

access.hasAccess

  • 类型:( accessId: string | number ) => Promise<boolean>
  • 详情: 判断某个资源是否可见。
  • 参数
    • accessId,资源 Id
  • 返回值:是否有权限

access.isDataReady

  • 类型:() => boolean
  • 详情:可以用异步数据来设置权限,isDataReady 用来判断异步数据是否已经加载完毕。
  • 参数:null
  • 返回值:Boolean
import { access } from '@dronephon/access';
console.log(access.isDataReady());

access.setRole

  • 类型:函数
  • 详情:设置当前的角色。
  • 参数
    • roleId,角色 Id,有两种类型:
      • String,对应着 roles 配置对象中的 key
      • Promise,Promise resolve 的结果应对应着 roles 配置对象中的 key
import { access } from '@dronephon/access';
access.setRole('admin');

access.getRole

  • 类型:函数
  • 详情:获取当前的角色。
import { access } from '@dronephon/access';
access.getRole();

access.setAccess

  • 类型:函数
  • 详情:设置当前的角色。
  • 参数
    • accessIds,资源 Id 数组,有两种类型:
      • Array,数组项对应着 roles 配置对象中的 key
      • Promise,Promise resolve 的结果应该是Array<accessId>
import { access } from '@dronephon/access';
access.setAccess(['/a', '/b', '/c']);

access.getAccess

  • 类型:函数
  • 详情:返回当前可见的资源列表。
  • 参数:null
import { access } from '@dronephon/access';
access.getAccess();

useAccess

  • 类型composition 函数
  • 详情:判断某个资源是否可见。
  • 参数
    • accessId,资源 Id
  • 返回值ref
<template>
    <div v-if="accessOnepicess">accessOnepicess</div>
</template>
<script>
import { access } from '@dronephon/access';
export default {
    setup() {
        const accessOnepicess = useAccess('/onepiece1');
        return {
            accessOnepicess,
        };
    },
};
</script>

v-access

在指令 v-access 中传入 accessId,则当 accessId 拥有权限时显示 DOM,当没有权限时隐藏此 DOM。

<template>
    <div v-access="accessId">accessOnepicess</div>
</template>
<script>
export default {
    setup() {
        return {
            accessId: 'accessOnepicess',
        };
    },
};
</script>

组件 Access

组件 Access 中传入 accessId,则当 accessId 拥有权限时渲染此组件,当没有权限时隐藏此组件。

<template>
    <access :id="accessId"> accessOnepicess </access>
</template>
<script>
export default {
    setup() {
        return {
            accessId: 'accessOnepicess',
        };
    },
};
</script>