hasPermi.ts 867 B

12345678910111213141516171819202122232425262728293031
  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 { value } = binding
  7. if (value && value instanceof Array && value.length > 0) {
  8. const hasPermissions = hasPermission(value)
  9. if (!hasPermissions) {
  10. el.parentNode && el.parentNode.removeChild(el)
  11. }
  12. } else {
  13. throw new Error(t('permission.hasPermission'))
  14. }
  15. })
  16. }
  17. export const hasPermission = (permission: string[]) => {
  18. const { wsCache } = useCache()
  19. const all_permission = '*:*:*'
  20. const userInfo = wsCache.get(CACHE_KEY.USER)
  21. const permissions = userInfo?.permissions || []
  22. return permissions.some((p: string) => {
  23. return all_permission === p || permission.includes(p)
  24. })
  25. }