Skip to content

Commit 335cae1

Browse files
committed
feat: 完善 README 文档,添加示例和功能特性,更新 playground 配置以支持新插件。
1 parent 1e3bde7 commit 335cae1

9 files changed

Lines changed: 412 additions & 60 deletions

File tree

README.md

Lines changed: 246 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,270 @@
1-
# winjs-plugin-example
1+
# @winner-fed/plugin-access
22

3-
Example plugin for WinJS.
3+
适配 access(权限)的 WinJS 插件,适用于 Vue3。
44

55
<p>
6-
<a href="https://npmjs.com/package/winjs-plugin-example">
7-
<img src="https://img.shields.io/npm/v/winjs-plugin-example?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
6+
<a href="https://npmjs.com/package/@winner-fed/plugin-access">
7+
<img src="https://img.shields.io/npm/v/@winner-fed/plugin-access?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
88
</a>
99
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
10-
<a href="https://npmcharts.com/compare/winjs-plugin-example?minimal=true"><img src="https://img.shields.io/npm/dm/winjs-plugin-example.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" /></a>
10+
<a href="https://npmcharts.com/compare/@winner-fed/plugin-access?minimal=true"><img src="https://img.shields.io/npm/dm/@winner-fed/plugin-access.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" /></a>
1111
</p>
1212

13-
## Usage
13+
## 功能特性
1414

15-
Install:
15+
- 🔐 基于角色的权限管理系统 (RBAC)
16+
- 🚀 支持动态设置角色和权限
17+
- 🛡️ 提供路由级别的权限控制
18+
- 🎯 提供组件级别的权限控制(指令和组件)
19+
- ⚡ 支持同步和异步权限检查
20+
- 🔍 支持路径模式匹配(支持通配符)
21+
- 📦 开箱即用,零配置启动
22+
- 🔧 可配置的权限控制处理器
23+
24+
## 安装
1625

1726
```bash
18-
npm add winjs-plugin-example -D
27+
npm install @winner-fed/plugin-access
1928
```
2029

21-
Add plugin to your `.winrc.ts`:
30+
## 基本配置
2231

23-
```ts
24-
// .winrc.ts
25-
export default {
26-
plugins: ['winjs-plugin-example'],
27-
// 开启配置
28-
example: {}
29-
};
32+
`.winrc.ts` 中配置插件:
33+
34+
```typescript
35+
import { defineConfig } from 'win';
36+
37+
export default defineConfig({
38+
plugins: [require.resolve('@winner-fed/plugin-access')],
39+
access: {
40+
roles: {
41+
admin: ['/', '/admin', '/users/*'],
42+
normal: ['/normal', '/profile'],
43+
guest: ['/login', '/register']
44+
}
45+
}
46+
});
3047
```
3148

32-
## Options
49+
## 使用方法
3350

34-
### foo
51+
### 1. 路由配置
3552

36-
Some description.
53+
```typescript
54+
// src/app.ts
55+
import { access as accessApi } from 'winjs';
3756

38-
- Type: `string`
39-
- Default: `undefined`
40-
- Example:
57+
// 设置默认角色
58+
accessApi.setRole('admin');
4159

42-
```js
43-
export default {
44-
plugins: ['winjs-plugin-example'],
45-
// 开启配置
46-
example: {
47-
foo: 'bar'
48-
}
60+
export const access = {
61+
noFoundHandler({ next }) {
62+
// 处理404页面
63+
const accessIds = accessApi.getAccess();
64+
if (!accessIds.includes('/404')) {
65+
accessApi.setAccess(accessIds.concat(['/404']));
66+
}
67+
next('/404');
68+
},
69+
unAccessHandler({ next }) {
70+
// 处理无权限访问
71+
next('/403');
72+
},
73+
ignoreAccess: ['/login', '/register'] // 忽略权限检查的路由
4974
};
5075
```
5176

52-
## License
77+
### 2. 组件中使用
78+
79+
#### 使用 v-access 指令
80+
81+
```vue
82+
<template>
83+
<div>
84+
<!-- 只有有权限的用户才能看到 -->
85+
<div v-access="'/admin'">管理员内容</div>
86+
87+
<!-- 支持动态权限 -->
88+
<div v-access="dynamicPath">动态内容</div>
89+
90+
<!-- 支持通配符 -->
91+
<div v-access="'/users/*'">用户管理</div>
92+
</div>
93+
</template>
94+
95+
<script setup>
96+
import { ref } from 'vue';
97+
98+
const dynamicPath = ref('/profile');
99+
</script>
100+
```
101+
102+
#### 使用 Access 组件
103+
104+
```vue
105+
<template>
106+
<div>
107+
<Access id="/admin">
108+
<template #default>
109+
<div>管理员专用功能</div>
110+
</template>
111+
</Access>
112+
</div>
113+
</template>
114+
```
115+
116+
#### 使用 useAccess Hook
117+
118+
```vue
119+
<template>
120+
<div>
121+
<div v-if="hasAdminAccess">管理员功能</div>
122+
<div v-if="hasUserAccess">用户功能</div>
123+
</div>
124+
</template>
125+
126+
<script setup>
127+
import { useAccess } from 'winjs';
128+
129+
const hasAdminAccess = useAccess('/admin');
130+
const hasUserAccess = useAccess('/users/*');
131+
</script>
132+
```
133+
134+
### 3. 编程式权限控制
135+
136+
```typescript
137+
import { access } from 'winjs';
138+
139+
// 设置角色
140+
access.setRole('admin');
141+
142+
// 设置权限
143+
access.setAccess(['/admin', '/users']);
144+
145+
// 检查权限(异步)
146+
const hasAccess = await access.hasAccess('/admin');
147+
148+
// 检查权限(同步)
149+
const hasAccessSync = access.hasAccessSync('/admin');
150+
151+
// 获取当前角色
152+
const currentRole = access.getRole();
153+
154+
// 获取当前权限列表
155+
const currentAccess = access.getAccess();
156+
157+
// 路径匹配
158+
const isMatch = access.match('/users/profile', ['/users/*']);
159+
160+
// 设置预设权限
161+
access.setPresetAccess(['/public', '/common']);
162+
```
163+
164+
## API 文档
165+
166+
### 配置项
167+
168+
#### access.roles
169+
- 类型:`Record<string, string[]>`
170+
- 描述:角色与权限的映射关系
171+
172+
#### access.noFoundHandler
173+
- 类型:`(params: { router, to, from, next }) => void`
174+
- 描述:404页面处理函数
175+
176+
#### access.unAccessHandler
177+
- 类型:`(params: { router, to, from, next }) => void`
178+
- 描述:无权限访问处理函数
179+
180+
#### access.ignoreAccess
181+
- 类型:`string[]`
182+
- 描述:忽略权限检查的路由列表
183+
184+
### Access 对象方法
185+
186+
#### setRole(roleId: string | Promise<string>)
187+
设置当前用户角色。
188+
189+
#### getRole(): string
190+
获取当前用户角色。
191+
192+
#### setAccess(accessIds: string[] | Promise<string[]>)
193+
设置当前用户权限列表。
194+
195+
#### getAccess(): string[]
196+
获取当前用户权限列表。
197+
198+
#### hasAccess(path: string): Promise<boolean>
199+
异步检查是否有指定路径的权限。
200+
201+
#### hasAccessSync(path: string): boolean
202+
同步检查是否有指定路径的权限。
203+
204+
#### match(path: string, accessIds: string[]): boolean
205+
检查路径是否匹配权限列表。
206+
207+
#### setPresetAccess(accessIds: string | string[])
208+
设置预设权限。
209+
210+
#### isDataReady(): boolean
211+
检查权限数据是否准备就绪。
212+
213+
### useAccess Hook
214+
215+
```typescript
216+
const hasAccess = useAccess(path: string | Ref<string>): Ref<boolean>
217+
```
218+
219+
返回一个响应式的权限状态。
220+
221+
## 高级用法
222+
223+
### 1. 异步权限设置
224+
225+
```typescript
226+
import { access } from 'winjs';
227+
228+
// 异步设置角色
229+
access.setRole(fetch('/api/user/role').then(res => res.json()));
230+
231+
// 异步设置权限
232+
access.setAccess(fetch('/api/user/permissions').then(res => res.json()));
233+
```
234+
235+
### 2. 动态权限更新
236+
237+
```typescript
238+
import { access } from 'winjs';
239+
240+
// 用户登录后更新权限
241+
function onLogin(userInfo) {
242+
access.setRole(userInfo.role);
243+
access.setAccess(userInfo.permissions);
244+
}
245+
246+
// 用户登出后清空权限
247+
function onLogout() {
248+
access.setRole('guest');
249+
access.setAccess([]);
250+
}
251+
```
252+
253+
### 3. 权限通配符
254+
255+
支持以下通配符模式:
256+
- `/users/*` - 匹配 `/users/` 下的所有路径
257+
- `/admin/*/edit` - 匹配 `/admin/任意内容/edit` 格式的路径
258+
259+
### 4. 权限组合
260+
261+
```typescript
262+
// 预设权限 + 角色权限 + 动态权限
263+
access.setPresetAccess(['/public', '/common']);
264+
access.setRole('admin'); // 自动获取 admin 对应的权限
265+
access.setAccess(['/special']); // 额外的动态权限
266+
```
267+
268+
## 许可证
53269

54270
[MIT](./LICENSE).

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "winjs-plugin-template",
2+
"name": "@winner-fed/plugin-access",
33
"keywords": [
44
"winjs",
55
"plugin",
@@ -11,10 +11,10 @@
1111
"typescript",
1212
"biome"
1313
],
14-
"version": "0.0.0",
14+
"version": "1.0.0",
1515
"repository": {
1616
"type": "git",
17-
"url": "git+https://github.com/winjs-dev/winjs-plugin-template.git"
17+
"url": "git+https://github.com/winjs-dev/winjs-plugin-access.git"
1818
},
1919
"license": "MIT",
2020
"type": "module",
@@ -29,7 +29,8 @@
2929
"module": "./dist/index.js",
3030
"types": "./dist/index.d.ts",
3131
"files": [
32-
"dist"
32+
"dist",
33+
"templates"
3334
],
3435
"scripts": {
3536
"build": "rslib build",

playground/.winrc.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
import { defineConfig } from 'win';
22

33
export default defineConfig({
4-
plugins: ['../src'],
5-
example: {
6-
foo: 'bar',
7-
},
4+
plugins: ['@winner-fed/plugin-access'],
5+
routes: [{
6+
path: '/',
7+
name: 'Home',
8+
component: 'index',
9+
routes: [
10+
{
11+
path: 'admin',
12+
name: 'Admin',
13+
component: '@/components/admin'
14+
},
15+
{
16+
path: 'normal',
17+
name: 'Normal',
18+
component: '@/components/normal'
19+
}
20+
]
21+
}],
22+
access: {
23+
roles: {
24+
admin: ['/', '/admin'],
25+
normal: ['/normal']
26+
}
27+
}
828
});

playground/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@winner-fed/winjs": "*"
1515
},
1616
"devDependencies": {
17+
"@winner-fed/plugin-access": "workspace:*",
1718
"typescript": "^5.0.3"
1819
}
1920
}

playground/src/app.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/**
2-
*
3-
* @authors liwb (you@example.org)
4-
* @date 2024/8/21 13:35
5-
* @version $ IIFE
6-
*/
7-
81
import { useAccess, access as accessApi } from 'winjs';
92

103
// 设置默认角色权限

playground/src/layouts/index.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<template>
22
<div class="pages">
3-
<router-view />
3+
<keep-alive v-if="$route.meta.keepAlive">
4+
<router-view />
5+
</keep-alive>
6+
<router-view v-if="!$route.meta.keepAlive" />
47
</div>
58
</template>
69

0 commit comments

Comments
 (0)