-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4576bd
commit 3fe40ba
Showing
11 changed files
with
207 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import Permission from './src/Permission.vue' | ||
import { hasPermi } from './src/utils' | ||
|
||
export { Permission, hasPermi } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import type { App } from 'vue' | ||
import { Icon } from './Icon' | ||
import { Permission } from './Permission' | ||
|
||
export const setupGlobCom = (app: App<Element>): void => { | ||
app.component('Icon', Icon) | ||
app.component('Permission', Permission) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<script setup lang="tsx"> | ||
import { ContentWrap } from '@/components/ContentWrap' | ||
import { ref, unref } from 'vue' | ||
import { ElButton, ElDivider, ElRow, ElCol } from 'element-plus' | ||
import { hasPermi } from '@/components/Permission' | ||
const permission = ref('add') | ||
setTimeout(() => { | ||
permission.value = 'view' | ||
}, 3000) | ||
</script> | ||
|
||
<template> | ||
<ContentWrap> | ||
<ElDivider>组件方式判断(已经全局注册,支持动态修改)</ElDivider> | ||
<ElRow :gutter="20"> | ||
<ElCol :span="8"> | ||
新增权限: | ||
<Permission permission="add"> | ||
<ElButton type="primary"> Add </ElButton> | ||
</Permission> | ||
</ElCol> | ||
<ElCol :span="8"> | ||
删除权限: | ||
<Permission permission="delete"> | ||
<ElButton type="danger"> Delete </ElButton> | ||
</Permission> | ||
</ElCol> | ||
<ElCol :span="8"> | ||
3秒后切换查看权限: | ||
<Permission :permission="permission"> | ||
<ElButton type="primary"> View </ElButton> | ||
</Permission> | ||
</ElCol> | ||
</ElRow> | ||
|
||
<ElDivider>指令方式判断(已经全局注册,不支持动态修改)</ElDivider> | ||
<ElRow :gutter="20"> | ||
<ElCol :span="8"> | ||
新增权限: | ||
<ElButton v-hasPermi="'add'" type="primary"> Add </ElButton> | ||
</ElCol> | ||
<ElCol :span="8"> | ||
删除权限: | ||
<ElButton v-hasPermi="'delete'" type="danger"> Delete </ElButton> | ||
</ElCol> | ||
<ElCol :span="8"> | ||
3秒后切换查看权限(无法动态修改): | ||
<ElButton v-hasPermi="permission" type="primary"> View </ElButton> | ||
</ElCol> | ||
</ElRow> | ||
|
||
<ElDivider>函数方式判断</ElDivider> | ||
<ElRow :gutter="20"> | ||
<ElCol :span="8"> | ||
新增权限: | ||
<ElButton v-if="hasPermi('add')" type="primary"> Add </ElButton> | ||
</ElCol> | ||
<ElCol :span="8"> | ||
删除权限: | ||
<ElButton v-if="hasPermi('delete')" type="danger"> Delete </ElButton> | ||
</ElCol> | ||
<ElCol :span="8"> | ||
3秒后切换查看权限: | ||
<ElButton v-if="hasPermi(unref(permission))" type="primary"> View </ElButton> | ||
</ElCol> | ||
</ElRow> | ||
</ContentWrap> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters