permission.ts 953 B

123456789101112131415161718192021222324252627282930313233343536
  1. import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
  2. import {hasPermission} from "@/directives/permission/hasPermi";
  3. const { t } = useI18n() // 国际化
  4. /**
  5. * 字符权限校验
  6. * @param {Array} value 校验值
  7. * @returns {Boolean}
  8. */
  9. export function checkPermi(permission: string[]) {
  10. return hasPermission(permission)
  11. }
  12. /**
  13. * 角色权限校验
  14. * @param {string[]} value 校验值
  15. * @returns {Boolean}
  16. */
  17. export function checkRole(value: string[]) {
  18. if (value && value instanceof Array && value.length > 0) {
  19. const { wsCache } = useCache()
  20. const permissionRoles = value
  21. const super_admin = 'super_admin'
  22. const userInfo = wsCache.get(CACHE_KEY.USER)
  23. const roles = userInfo?.roles || []
  24. const hasRole = roles.some((role: string) => {
  25. return super_admin === role || permissionRoles.includes(role)
  26. })
  27. return !!hasRole
  28. } else {
  29. console.error(t('permission.hasRole'))
  30. return false
  31. }
  32. }