hasPermi.ts 860 B

12345678910111213141516171819202122232425262728
  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 userInfo = wsCache.get(CACHE_KEY.USER)
  10. const permissions = userInfo?.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. }