Skip to content

Commit

Permalink
refactor!: 重构鉴权指令、鉴权组件
Browse files Browse the repository at this point in the history
  • Loading branch information
hooray committed Oct 8, 2024
1 parent 1598324 commit c34c2d1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 41 deletions.
13 changes: 8 additions & 5 deletions src/components/Auth/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ defineOptions({
const props = defineProps<{
value: string | string[]
all?: boolean
}>()
const auth = useAuth()
function check() {
return useAuth().auth(props.value)
return props.all
? auth.authAll(typeof props.value === 'string' ? [props.value] : props.value)
: auth.auth(props.value)
}
</script>

<template>
<div>
<slot v-if="check()" />
<slot v-else name="no-auth" />
</div>
<slot v-if="check()" />
<slot v-else name="no-auth" />
</template>
20 changes: 0 additions & 20 deletions src/components/AuthAll/index.vue

This file was deleted.

1 change: 0 additions & 1 deletion src/types/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export {}
declare module 'vue' {
export interface GlobalComponents {
Auth: typeof import('./../components/Auth/index.vue')['default']
AuthAll: typeof import('./../components/AuthAll/index.vue')['default']
FileUpload: typeof import('./../components/FileUpload/index.vue')['default']
FixedActionBar: typeof import('./../components/FixedActionBar/index.vue')['default']
HButton: typeof import('./../layouts/ui-kit/HButton.vue')['default']
Expand Down
23 changes: 8 additions & 15 deletions src/utils/directive.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import type { App } from 'vue'
import type { App, DirectiveBinding } from 'vue'

export default function directive(app: App) {
// 注册 v-auth 和 v-auth-all 指令
app.directive('auth', {
mounted: (el, binding) => {
if (!useAuth().auth(binding.value)) {
el.remove()
}
},
})
app.directive('auth-all', {
mounted: (el, binding) => {
if (!useAuth().authAll(binding.value)) {
el.remove()
}
},
app.directive('auth', (el: HTMLElement, binding: DirectiveBinding) => {
if (binding.modifiers.all ? useAuth().authAll(binding.value) : useAuth().auth(binding.value)) {
el.style.display = ''
}
else {
el.style.display = 'none'
}
})
}

0 comments on commit c34c2d1

Please sign in to comment.