sysmgr-admin/src/directives/permission.ts
liuyun 2f7f83b8b1 feat(rbac): 按钮级权限控制(v-permission + 权限 store + 用户页导出)
- 新增 permission store + v-permission 指令(响应式,异步权限加载后可恢复)
- ty-crud 支持 createPermission 与动作 permission 过滤
- 用户页:分配角色/重置密码按权限码显示,新增导出按钮 + CSV 导出

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-11 16:19:53 +08:00

26 lines
987 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { watchEffect, type Directive } from 'vue'
import { usePermissionStore } from '@/stores/modules/permission'
const stops = new WeakMap<HTMLElement, () => void>()
/**
* v-permission 指令:当前用户不具备指定权限码(且非超管)时隐藏元素。
* 用法:v-permission="'user:create'" 或 v-permission="['user:create', 'user:update']"(任一满足即可)。
* 权限异步加载:用 watchEffect 响应式隐藏/恢复,权限加载完成后自动显示。
*/
export const vPermission: Directive<HTMLElement, string | string[]> = {
mounted(el, binding) {
const store = usePermissionStore()
const required = (Array.isArray(binding.value) ? binding.value : [binding.value]).filter(Boolean) as string[]
if (required.length === 0) return
const stop = watchEffect(() => {
el.style.display = store.hasAny(required) ? '' : 'none'
})
stops.set(el, stop)
},
unmounted(el) {
stops.get(el)?.()
stops.delete(el)
},
}