mirror of
https://github.com/chansee97/nova-admin.git
synced 2025-04-05 04:22:49 +08:00
feat: remove support single role permission
This commit is contained in:
parent
e5222bbbc6
commit
30f0ac0904
@ -1,12 +1,11 @@
|
||||
import { useAuthStore } from '@/store'
|
||||
import { isArray, isString } from 'radash'
|
||||
|
||||
/** 权限判断 */
|
||||
export function usePermission() {
|
||||
const authStore = useAuthStore()
|
||||
|
||||
function hasPermission(
|
||||
permission: Entity.RoleType | Entity.RoleType[] | undefined,
|
||||
permission?: Entity.RoleType[],
|
||||
) {
|
||||
if (!permission)
|
||||
return true
|
||||
@ -15,13 +14,9 @@ export function usePermission() {
|
||||
return false
|
||||
const { role } = authStore.userInfo
|
||||
|
||||
let has = role === 'super'
|
||||
let has = role.includes('super')
|
||||
if (!has) {
|
||||
if (isArray(permission))
|
||||
has = permission.includes(role)
|
||||
|
||||
if (isString(permission))
|
||||
has = permission === role
|
||||
has = permission.every(i => role.includes(i))
|
||||
}
|
||||
return has
|
||||
}
|
||||
|
2
src/typings/api/login.d.ts
vendored
2
src/typings/api/login.d.ts
vendored
@ -7,7 +7,7 @@ namespace Api {
|
||||
/** 用户id */
|
||||
id: number
|
||||
/** 用户角色类型 */
|
||||
role: Entity.RoleType
|
||||
role: Entity.RoleType[]
|
||||
/** 访问toekn */
|
||||
accessToken: string
|
||||
/** 刷新toekn */
|
||||
|
@ -4,7 +4,7 @@ import { useAuthStore } from '@/store'
|
||||
|
||||
const authStore = useAuthStore()
|
||||
const { hasPermission } = usePermission()
|
||||
const { role } = authStore.userInfo!
|
||||
const { role } = authStore.userInfo
|
||||
|
||||
const roleList: Entity.RoleType[] = ['super', 'admin', 'user']
|
||||
|
||||
@ -23,7 +23,7 @@ function toggleUserRole(role: Entity.RoleType) {
|
||||
</n-button-group>
|
||||
<n-h2>v-permission 指令用法</n-h2>
|
||||
<n-space>
|
||||
<n-button v-permission="'super'">
|
||||
<n-button v-permission="['super']">
|
||||
仅super可见
|
||||
</n-button>
|
||||
<n-button v-permission="['admin']">
|
||||
@ -33,10 +33,10 @@ function toggleUserRole(role: Entity.RoleType) {
|
||||
|
||||
<n-h2>usePermission 函数用法</n-h2>
|
||||
<n-space>
|
||||
<n-button v-if="hasPermission('super')">
|
||||
<n-button v-if="hasPermission(['super'])">
|
||||
super可见
|
||||
</n-button>
|
||||
<n-button v-if="hasPermission('admin')">
|
||||
<n-button v-if="hasPermission(['admin'])">
|
||||
admin可见
|
||||
</n-button>
|
||||
<n-button v-if="hasPermission(['admin', 'user'])">
|
||||
@ -45,5 +45,3 @@ function toggleUserRole(role: Entity.RoleType) {
|
||||
</n-space>
|
||||
</n-card>
|
||||
</template>
|
||||
|
||||
<style scoped></style>
|
||||
|
Loading…
x
Reference in New Issue
Block a user