permission.ts 1.3 KB

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