feat: 新增权限测试页
parent
c4576bd57b
commit
3fe40ba62d
@ -0,0 +1,4 @@
|
|||||||
|
import Permission from './src/Permission.vue'
|
||||||
|
import { hasPermi } from './src/utils'
|
||||||
|
|
||||||
|
export { Permission, hasPermi }
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { propTypes } from '@/utils/propTypes'
|
||||||
|
import { computed, unref } from 'vue'
|
||||||
|
import { useRouter } from 'vue-router'
|
||||||
|
|
||||||
|
const { currentRoute } = useRouter()
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
permission: propTypes.string.def()
|
||||||
|
})
|
||||||
|
|
||||||
|
const currentPermission = computed(() => {
|
||||||
|
return unref(currentRoute)?.meta?.permission || []
|
||||||
|
})
|
||||||
|
|
||||||
|
const hasPermission = computed(() => {
|
||||||
|
const permission = unref(props.permission)
|
||||||
|
if (!permission) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return unref(currentPermission).includes(permission)
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<template v-if="hasPermission">
|
||||||
|
<slot></slot>
|
||||||
|
</template>
|
||||||
|
</template>
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
import { useI18n } from '@/hooks/web/useI18n'
|
||||||
|
import router from '@/router'
|
||||||
|
|
||||||
|
export const hasPermi = (value: string) => {
|
||||||
|
const { t } = useI18n()
|
||||||
|
const permission = (router.currentRoute.value.meta.permission || []) as string[]
|
||||||
|
if (!value) {
|
||||||
|
throw new Error(t('permission.hasPermission'))
|
||||||
|
}
|
||||||
|
if (permission.includes(value)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@ -1,6 +1,8 @@
|
|||||||
import type { App } from 'vue'
|
import type { App } from 'vue'
|
||||||
import { Icon } from './Icon'
|
import { Icon } from './Icon'
|
||||||
|
import { Permission } from './Permission'
|
||||||
|
|
||||||
export const setupGlobCom = (app: App<Element>): void => {
|
export const setupGlobCom = (app: App<Element>): void => {
|
||||||
app.component('Icon', Icon)
|
app.component('Icon', Icon)
|
||||||
|
app.component('Permission', Permission)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue