hasPermi.ts 823 B

123456789101112131415161718192021222324252627
  1. import type { App } from 'vue'
  2. import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
  3. const { t } = useI18n() // 国际化
  4. export function hasPermi(app: App<Element>) {
  5. app.directive('hasPermi', (el, binding) => {
  6. const { wsCache } = useCache()
  7. const { value } = binding
  8. const all_permission = '*:*:*'
  9. const permissions = wsCache.get(CACHE_KEY.USER).permissions
  10. if (value && value instanceof Array && value.length > 0) {
  11. const permissionFlag = value
  12. const hasPermissions = permissions.some((permission: string) => {
  13. return all_permission === permission || permissionFlag.includes(permission)
  14. })
  15. if (!hasPermissions) {
  16. el.parentNode && el.parentNode.removeChild(el)
  17. }
  18. } else {
  19. throw new Error(t('permission.hasPermission'))
  20. }
  21. })
  22. }