File tree Expand file tree Collapse file tree 3 files changed +8
-15
lines changed
views/demo/permission/permission Expand file tree Collapse file tree 3 files changed +8
-15
lines changed Original file line number Diff line number Diff line change 11import { useAuthStore } from '@/store'
2- import { isArray , isString } from 'radash'
32
43/** 权限判断 */
54export function usePermission ( ) {
65 const authStore = useAuthStore ( )
76
87 function hasPermission (
9- permission : Entity . RoleType | Entity . RoleType [ ] | undefined ,
8+ permission ? : Entity . RoleType [ ] ,
109 ) {
1110 if ( ! permission )
1211 return true
@@ -15,13 +14,9 @@ export function usePermission() {
1514 return false
1615 const { role } = authStore . userInfo
1716
18- let has = role === 'super'
17+ let has = role . includes ( 'super' )
1918 if ( ! has ) {
20- if ( isArray ( permission ) )
21- has = permission . includes ( role )
22-
23- if ( isString ( permission ) )
24- has = permission === role
19+ has = permission . every ( i => role . includes ( i ) )
2520 }
2621 return has
2722 }
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ namespace Api {
77 /** 用户id */
88 id : number
99 /** 用户角色类型 */
10- role : Entity . RoleType
10+ role : Entity . RoleType [ ]
1111 /** 访问toekn */
1212 accessToken : string
1313 /** 刷新toekn */
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import { useAuthStore } from '@/store'
44
55const authStore = useAuthStore ()
66const { hasPermission } = usePermission ()
7- const { role } = authStore .userInfo !
7+ const { role } = authStore .userInfo
88
99const roleList: Entity .RoleType [] = [' super' , ' admin' , ' user' ]
1010
@@ -23,7 +23,7 @@ function toggleUserRole(role: Entity.RoleType) {
2323 </n-button-group >
2424 <n-h2 >v-permission 指令用法</n-h2 >
2525 <n-space >
26- <n-button v-permission =" 'super'" >
26+ <n-button v-permission =" [ 'super'] " >
2727 仅super可见
2828 </n-button >
2929 <n-button v-permission =" ['admin']" >
@@ -33,10 +33,10 @@ function toggleUserRole(role: Entity.RoleType) {
3333
3434 <n-h2 >usePermission 函数用法</n-h2 >
3535 <n-space >
36- <n-button v-if =" hasPermission('super')" >
36+ <n-button v-if =" hasPermission([ 'super'] )" >
3737 super可见
3838 </n-button >
39- <n-button v-if =" hasPermission('admin')" >
39+ <n-button v-if =" hasPermission([ 'admin'] )" >
4040 admin可见
4141 </n-button >
4242 <n-button v-if =" hasPermission(['admin', 'user'])" >
@@ -45,5 +45,3 @@ function toggleUserRole(role: Entity.RoleType) {
4545 </n-space >
4646 </n-card >
4747</template >
48-
49- <style scoped></style >
You can’t perform that action at this time.
0 commit comments