hasPermi.ts 871 B

12345678910111213141516171819202122232425262728293031
  1. import type { App } from 'vue'
  2. import { useUserStore } from '@/store/modules/user'
  3. const { t } = useI18n() // 国际化
  4. /** 判断权限的指令 directive */
  5. export function hasPermi(app: App<Element>) {
  6. app.directive('hasPermi', (el, binding) => {
  7. const { value } = binding
  8. if (value && value instanceof Array && value.length > 0) {
  9. const hasPermissions = hasPermission(value)
  10. if (!hasPermissions) {
  11. el.parentNode && el.parentNode.removeChild(el)
  12. }
  13. } else {
  14. throw new Error(t('permission.hasPermission'))
  15. }
  16. })
  17. }
  18. /** 判断权限的方法 function */
  19. const userStore = useUserStore()
  20. const all_permission = '*:*:*'
  21. export const hasPermission = (permission: string[]) => {
  22. return (
  23. userStore.permissions.has(all_permission) ||
  24. permission.some((permission) => userStore.permissions.has(permission))
  25. )
  26. }