hasPermi.ts 868 B

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